Elasticsearch QueryBuilder meets several conditions

Given the JSON in the ES index in the following format:

{ "pin": { "id": 123, "location": { "lat": 456, "lon":-789 } } } 

The following gets the document corresponding to the id field:

 client.prepareSearch("index_name") .setTypes("pin") .setQuery(QueryBuilders.termQuery("id", 123)) .execute() .actionGet(); 

Instead, I'm trying to match multiple fields, i.e. ( location.lat , location.lon ).

 QueryBuilders.termQuery(); // accepts only a single term 

I tried several alternatives, but none of them work, for example:

 QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("location.lat", 456)) .must(QueryBuilders.termQuery("location.lon", -789)); client.prepareSearch("index_name") .setTypes("pin") .setQuery(queryBuilder) .execute() .actionGet(); 
+6
source share
4 answers

By default, the geo_point field is not indexed as two fields (location.lat and location.lon), it is indexed as a single field that contains both latitude and longitude.

You can enable latitude and longitude indexing by turning on the lat_lon option. However, in your example, the values ​​for latitude and longitude are too large. Thus, they are normalized, converted to doubles and indexed as -84.0 and -69.0 instead of 456 and -789. So, if you enable lat_lon and replace the value in the queries, you can get the results.

Note that the values ​​for latitude and longitude are converted to double before indexing. Therefore, the use of terminal queries may not be very practical in the long run, since you will always have to consider rounding errors. It might be more useful to use a range of queries or elasticsearch geospatial queries instead.

+4
source

Check the bool query in ElasticSearch, you must specify must , should or should_not to get the appropriate mix and / or for your query.

+1
source

You can use QueryBuilders.multiMatchQuery instead of QueryBuilders.termQuery

0
source
 QueryBuilder qb = boolQuery() .must(termQuery("content", "test1")) .must(termQuery("content", "test4")); 

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-compound-queries.html

-1
source

Source: https://habr.com/ru/post/916782/


All Articles