|
| 1 | +/** |
| 2 | + * Copyright 2015 OpenSearchServer Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.opensearchserver.client.test.v1; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URISyntaxException; |
| 20 | + |
| 21 | +import com.opensearchserver.client.JsonClient1; |
| 22 | +import com.opensearchserver.client.common.search.query.SearchField; |
| 23 | +import com.opensearchserver.client.common.search.query.SearchField.SearchFieldMode; |
| 24 | +import com.opensearchserver.client.common.search.query.SearchFieldQuery; |
| 25 | +import com.opensearchserver.client.common.search.query.SearchQueryAbstract.OperatorEnum; |
| 26 | +import com.opensearchserver.client.v1.SearchApi1; |
| 27 | +import com.opensearchserver.client.v1.search.SearchResult1; |
| 28 | +import com.opensearchserver.utils.json.JsonMapper; |
| 29 | + |
| 30 | +public class SearchFieldTest { |
| 31 | + |
| 32 | + public void testSearchBooleanGroup(String[] args) throws IOException, |
| 33 | + URISyntaxException { |
| 34 | + JsonClient1 client = new JsonClient1("http://localhost:8080", null, |
| 35 | + null, 50000); |
| 36 | + String queryGroup1 = "open search server"; |
| 37 | + String queryGroup2 = "en"; |
| 38 | + SearchApi1 searchApi = new SearchApi1(client); |
| 39 | + SearchFieldQuery searchFieldQuery = (SearchFieldQuery) new SearchFieldQuery() |
| 40 | + .setEmptyReturnsAll(false).setOperator(OperatorEnum.AND); |
| 41 | + |
| 42 | + // If the queryGroup1 is not empty, we add the required fields |
| 43 | + if (queryGroup1 != null && !queryGroup1.isEmpty()) { |
| 44 | + |
| 45 | + // Create the search fields, affected to the first boolean group |
| 46 | + searchFieldQuery.addSearchField(new SearchField() |
| 47 | + .setField("content").setMode(SearchFieldMode.PATTERN) |
| 48 | + .setBooleanGroup(1)); |
| 49 | + |
| 50 | + searchFieldQuery.addSearchField(new SearchField() |
| 51 | + .setField("contentExact").setMode(SearchFieldMode.PATTERN) |
| 52 | + .setBooleanGroup(1)); |
| 53 | + |
| 54 | + // We set the text query |
| 55 | + searchFieldQuery.setQueryStringMap("content", queryGroup1); |
| 56 | + searchFieldQuery.setQueryStringMap("contentExact", queryGroup1); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + // If the queryGroup2 is not empty, we add the required fields |
| 61 | + if (queryGroup2 != null && !queryGroup2.isEmpty()) { |
| 62 | + |
| 63 | + // Create the search fields, affected to the first boolean group |
| 64 | + searchFieldQuery.addSearchField(new SearchField().setField("lang") |
| 65 | + .setMode(SearchFieldMode.PATTERN).setBooleanGroup(2)); |
| 66 | + |
| 67 | + searchFieldQuery.setQueryStringMap("lang", queryGroup2); |
| 68 | + } |
| 69 | + |
| 70 | + System.out.println(JsonMapper.MAPPER |
| 71 | + .writeValueAsString(searchFieldQuery)); |
| 72 | + SearchResult1 searchResult = searchApi.executeSearchField("web", |
| 73 | + searchFieldQuery); |
| 74 | + |
| 75 | + System.out.println(JsonMapper.MAPPER.writeValueAsString(searchResult)); |
| 76 | + } |
| 77 | +} |
0 commit comments