基本问题如下:
是否有方便的方法在嵌套查询的所有字段上指定多字段匹配?
对于正常查询{ match : { _all : "query string" }}
有效。
这在嵌套查询中不起作用,可能是因为嵌套对象没有_all?
下面更详细的问题:
我有一个名为“Parent”的嵌套文档,如下所示:
{
"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"
} ]
}
]
}
这是我用来制作子嵌套对象的映射:
"Parent" : {
"properties" : {
"children" : {
"type" : "nested",
"include_in_parent" : true
}
}
}
这是一个查询,我想在所有子字段查询和术语查询中使用匹配选择几个术语:
"query" : {
"nested": {
"path" : "children",
"query" : {
"bool" : {
"must" : [
{"multi_match" : {"query": "value_c_a_1", "fields" : ["children.*"]}},
{"term" : {children.field_a : "value_a_1" }}
]
}
}
}
}
上述查询不起作用,因为我无法在嵌套对象的多匹配查询中选择所有字段。
"query" : {
"nested": {
"path" : "children",
"query" : {
"bool" : {
"must" : [
{"multi_match" : {"query": "value_c_a_1", "fields" : ["*_c_a"]}}
]
}
}
}
}
上面的查询有效,因为模式匹配允许*放在字符串之前,但由于某种原因不能放在后面(?)
有没有一种很好的速记方式可以选择嵌套对象的所有字段?
知道为什么期望的孩子。*通配符不能按预期工作也很好。
答案 0 :(得分:0)
为我工作正常(注意:Elasticsearch 1.7)。请注意不同的名称(显然,有些孩子) - 我没有用原始名称测试它,但它可能与它有关。
架构:
curl -XPUT localhost:9200/test -d '{
"mappings": {
"aparent": {
"properties": {
"somechildren": {
"type": "nested",
"include_in_parent": true
}
}
}
}
}'
文件:
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"
} ]
}
]
}'
查询:
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"
}
}
}
]
}
}
}
}
}
结果:
{
"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"
}
]
}
]
}
}
]
}
}