Creating a nested elasticsearch query to match all fields of a nested object

The main question is: Is there a convenient way to specify a multi-field match in all fields for a subquery? For a regular query, { match : { _all : "query string" }} works. This does not work in a nested query, perhaps because the nested object does not have _all?

More detailed question below:

I have a subdocument called "Parent" as follows:

 { "children" : [ { "field_a": "value_a_1", "field_b" : "value_b_1", "field_c" : [ { "field_c_a" : "value_c_a_1", "field_c_b" : "value_c_b_1" } ] }, { "field_a": "value_a_2", "field_b" : "value_b_2", "field_c" : [ { "field_c_a" : "value_c_a_2", "field_c_b" : "value_c_b_2" } ] } ] } 

This is the mapping I used to create child objects:

 "Parent" : { "properties" : { "children" : { "type" : "nested", "include_in_parent" : true } } } 

And here is a query in which I want to select several terms, using a match on the request of all child fields, and a query for terms:

 "query" : { "nested": { "path" : "children", "query" : { "bool" : { "must" : [ {"multi_match" : {"query": "value_c_a_1", "fields" : ["children.*"]}}, {"term" : {children.field_a : "value_a_1" }} ] } } } } 

The above request does not work because I cannot select all the fields in the multimatch request for a nested object.

 "query" : { "nested": { "path" : "children", "query" : { "bool" : { "must" : [ {"multi_match" : {"query": "value_c_a_1", "fields" : ["*_c_a"]}} ] } } } } 

The executed query works because pattern matching allows you to put * in front of the line, but not for any reason (?)

Is there a good shortcut way to select all fields of a nested object?

It would also be nice to know why the expected children. * wildcard is not working properly.

+6
source share
1 answer

Worked fine for me (note: Elasticsearch 1.7). Pay attention to different names, although (aparent, somechildren) - I did not test it with the original names, but he could do something about it.

Scheme:

 curl -XPUT localhost:9200/test -d '{ "mappings": { "aparent": { "properties": { "somechildren": { "type": "nested", "include_in_parent": true } } } } }' 

Document

 curl -XPUT localhost:9200/test/aparent/1 -d '{ "somechildren" : [ { "field_a": "value_a_1", "field_b" : "value_b_1", "field_c" : [ { "field_c_a" : "value_c_a_1", "field_c_b" : "value_c_b_1" } ] }, { "field_a": "value_a_2", "field_b" : "value_b_2", "field_c" : [ { "field_c_a" : "value_c_a_2", "field_c_b" : "value_c_b_2" } ] } ] }' 

Query:

 GET test/_search { "query": { "nested": { "path": "somechildren", "query": { "bool": { "must": [ { "multi_match": { "query": "value_c_a_1", "fields": [ "somechildren.*" ] } }, { "term": { "somechildren.field_a": { "value": "value_a_1" } } } ] } } } } } 

Result:

  { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.8603305, "hits": [ { "_index": "test", "_type": "aparent", "_id": "1", "_score": 0.8603305, "_source": { "somechildren": [ { "field_a": "value_a_1", "field_b": "value_b_1", "field_c": [ { "field_c_a": "value_c_a_1", "field_c_b": "value_c_b_1" } ] }, { "field_a": "value_a_2", "field_b": "value_b_2", "field_c": [ { "field_c_a": "value_c_a_2", "field_c_b": "value_c_b_2" } ] } ] } } ] } } 
0
source

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


All Articles