Making an elasticsearch nested query for match on all fields of nested object -



Making an elasticsearch nested query for match on all fields of nested object -

the basic question follows: there convenient way specify multi-field match on fields nested query? normal query { match : { _all : "query string" }} works. doesn't work in nested query perhaps because nested object doesn't have _all?

the more detailed question below:

i have nested document called "parent" 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 mapping used making children nested objects:

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

and here query, want select few terms using match on children fields query, , term query:

"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 query doesn't work because can't select fields in multimatch query nested object.

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

the query above works because pattern matching allows * placed before string not after reason (?)

is there nice shorthand way select fields of nested object?

it nice know why expected children.* wildcard doesn't work expected.

worked me (note: elasticsearch 1.7). notice different names though (aparent, somechildren) - haven't tested original names, have it.

schema:

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" } ] } ] } } ] } }

elasticsearch

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -