gpt4 book ai didi

elasticsearch - 对嵌套对象的所有字段进行匹配的 Elasticsearch 嵌套查询

转载 作者:行者123 更新时间:2023-11-29 02:47:46 24 4
gpt4 key购买 nike

基本问题如下:有没有一种方便的方法可以在嵌套查询的所有字段上指定多字段匹配?对于普通查询 { 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"]}}
]
}
}
}
}

上面的查询之所以有效,是因为模式匹配允许 * 放在字符串之前,但出于某种原因不能放在字符串之后 (?)

是否有一种很好的速记方式来选择嵌套对象的所有字段?

也很高兴知道为什么预期的 child 。* 通配符不能按预期工作。

最佳答案

对我来说工作正常(注意:Elasticsearch 1.7)。请注意不同的名称(aparent,somechildren)——我没有用原始名称对其进行测试,但它可能与它有关。

架构:

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

关于elasticsearch - 对嵌套对象的所有字段进行匹配的 Elasticsearch 嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26721804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com