Nested queries elasticsearch query_string

I am trying to write a query using query_string to retrieve data requests from nested objects.

An example of the query I would like to make is the following:

{ "query": { "query_string": { "query": "a.id:2" } } } 

Where "a" is the nested object, and "id" is the field "a".

I know that I can successfully complete this task with a subquery by writing a query like:

 { "nested": { "path": "a" "query_string": { "query": "a.id:2" } } } 

However, I would like to avoid this. I do not want to find out on my own that the user is looking for a nested field and modifies the request. I tried using the "fields" parameter, but it does not seem to work with nested objects.

Is it possible to write this query directly using query_string queries? What semantics can I get? (for example, if I write "a.id:2 AND ab: 10", do I match two fields in the same object or in different objects?)

+6
source share
1 answer

I did more research and found that this could be achieved by setting the include_in_parent parameter to true in the mapping.

Now you can execute a query like

 { "query": { "query_string": { "query": "fields.fieldvalue:sometext" } } } 

For instance: -

 "mappings": { "documentmetadatavalue": { "properties": { "id": { "type": "string" }, "fields": { "type": "nested", "include_in_parent": true, "properties": { "fieldId": {"type": "string"}, "fieldvalue": {"type": "string"} } } } } } 

Let me know if this helps.

+7
source

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


All Articles