How to use `gt` and` fields` in the same request in Elasticsearch

In the previous question , I was introduced to fields in the query_string query and how it can help me search for nested document fields.

 { "query": { "query_string": { "fields": ["*.id","id"], "query": "2" } } } 

But it only works to match, what if I want to do some kind of comparison? After some reading and testing, it seems that queries like range do not support fields . Is there a way I can execute a range query, for example. on a date, above a field that can be scattered anywhere in the document hierarchy?

i.e. given the following document:

 { "id" : 1, "Comment" : "Comment 1", "date" : "2016-08-16T15:22:36.967489", "Reply" : [ { "id" : 2, "Comment" : "Inner comment", "date" : "2016-08-16T16:22:36.967489" } ] } 

Does the query search in the field date (for example, date > '2016-08-16T16:00:00.000000' ), which corresponds to this document, because of the attached field without explicitly indicating the address on Reply.date ? Something like this (I know the following query is incorrect):

 { "query": { "range" : { "date" : { "gte" : "2016-08-16T16:00:00.000000", }, "fields": ["date", "*.date"] } } } 
+5
source share
1 answer

The range query itself does not support it, however you can use the query_string query (again) and the fact that you can use wildcards and that it supports range queries in order to achieve what you need:

 { "query": { "query_string": { "query": "\*date:[2016-08-16T16:00:00.000Z TO *]" } } } 

The above request will return your document because Reply.date matches *date

+3
source

All Articles