Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 637 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Elasticsearch QueryBuilder Match Multiple Terms

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

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

The following gets the document matching the `id` field:

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

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

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


Tried few alternatives but none of it seems to work, eg:

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();
Reply

#2
Check out the [bool query][1] in ElasticSearch, you should be able to specify `must`, `should` or `should_not` to get the appropriate mixture of and/or for your query.


[1]:

[To see links please register here]

Reply

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

You can turn on indexing of latitude and longitude by turning on the ```lat_lon``` [mapping option][3]. However, in your example, the values for latitude and longitude are too large. So, they are normalized, converted to double and indexed as -84.0 and -69.0 instead of 456 and -789. So, if you will enable ```lat_lon``` and replace the value in the queries, you should be able to get the results.

Please note that values for latitude and longitude are converted to double before indexing. So using term queries might not be very practical in the long term since you will have to always take rounding errors into consideration. It might be more useful to use [range queries][1] or elasticsearch [geospatial queries][2] instead.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#4
You can use QueryBuilders.multiMatchQuery instead of QueryBuilders.termQuery
Reply

#5
QueryBuilder qb = boolQuery()
.must(termsQuery("content", Arrays.asList("test1", "test4")));

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through