simpleQueryStringQuery("+John -Doe OR Janette") Īs you can probably guess, we can use the Lucene's Query Parser syntax to build simple, yet powerful queries. If you are more familiar with the Lucene queries syntax, you can use the simpleQueryStringQuery() method to customize search queries: QueryBuilder simpleStringQuery = QueryBuilders The coefficient of importance is used to order the result set of hits returned after executing the s earch() method. Note that it's possible to use wildcards and regex queries, but performance-wise, beware of memory consumption and response-time delay when dealing with wildcards, because something like *_apples may cause a huge impact on performance. In our example the field_2 has boost value set to three, making it more important than the other fields. We can use the caret symbol (^) to boost specific fields. "Text I am looking for", "field_1", "field_2^3", "*_field_wildcard") We can as well use the multiMatchQuery() method to build a multi-fields version of the match query: QueryBuilder matchSpecificFieldQuery= QueryBuilders.matchQuery( John Doe, The matchQuery() method matches all document with these exact field's value: QueryBuilder matchSpecificFieldQuery= QueryBuilders fullName, and the corresponding value – e.g. The rangeQuery() matches documents where a field's value is within a certain range: QueryBuilder matchDocumentsWithinRange = QueryBuilders The matchAllQuery() method returns a QueryBuilder object that matches all documents in the cluster: QueryBuilder matchAllQuery = QueryBuilders.matchAllQuery() Here's a list of the most common uses of the QueryBuilders API. While using the search() method to look for specific JSON documents in the cluster, we can use query builders to customize the search results. The QueryBuilders class provides a variety of static methods used as dynamic matchers to find specific entries in the cluster. SearchResponse response = arch(searchRequest, RequestOptions.DEFAULT) 4.3. archType(SearchType.DFS_QUERY_THEN_FETCH) SearchRequest searchRequest = new SearchRequest() postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) We can enhance the request by adding additional parameters in order to customize the query using the QueryBuilders methods: SearchSourceBuilder builder = new SearchSourceBuilder() Note that in this example we're using the FastJson library in order to convert JSON Strings to Java objects. In this case, the results list contains all the data stored in the cluster. The results returned by the search() method are called Hits, each Hit refers to a JSON document matching a search request. map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) SearchHit searchHits = response.getHits().getHits() SearchResponse response = arch(searchRequest, RequestOptions.DEFAULT) Now that we have a typed searchable JSON document indexed, we can proceed and search using the search() method: SearchRequest searchRequest = new SearchRequest()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |