假设我有3个文档

doc_1 = {
    "citedIn": [
        "Bar Councils Act, 1926 - Section 15",
        "Contract Act, 1872 - Section 23"
    ]
}

doc_2 = {
    "citedIn":[
        "15 C. B 400",
        "Contract Act, 1872 - Section 55"
    ]
}

doc_3 = {
    "citedIn":[
        "15 C. B 400",
        "Contract Act, 1872 - Section 15"
    ]
}

这里citedIn字段是一个数组对象。现在我想运行一个标准match查询
{
    "query":
    {
        "match": {"citedIn":{"query": "Contract act 15" , "operator":"and" }}
    }

}

上面的查询返回了所有3个文档,但是它假定返回doc_3,因为仅doc_3在单个数组元素中一起包含Contractact15
我将如何实现呢?
任何建议/解决方案都是可取的
嵌套数据类型更新:
我确实尝试过嵌套字段。
这是我的映射
{
    "mappings": {
        "properties": {
            "citedIn": {
                "type": "nested",
                "include_in_parent": true,
                "properties": {
                    "someFiled": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}
这是我的资料
doc_1 = {
    "citedIn": [
        {"someFiled" : "Bar Councils Act, 1926 - Section 15"},
        {"someFiled" : "Contract Act, 1872 - Section 23"}
    ]
}

doc_2 = {
    "citedIn":[
        {"someFiled" : "15 C. B 400"}
        {"someFiled" : "Contract Act, 1872 - Section 55"}
    ]
}

doc_3 = {
    "citedIn":[
        {"someFiled" : "15 C. B 400"},
        {"someFiled" : "Contract Act, 1872 - Section 15"}
    ]
}

这是我的查询
{
    "query":
    {

        "match": {"citedIn.someFiled":{"query": "Contract act 15" , "operator":"and" }}


    }
}
但仍然得到相同的结果

最佳答案

添加带有索引数据,映射,搜索查询和搜索结果的工作示例。
您需要使用nested query来搜索嵌套字段
索引映射

{
    "mappings": {
        "properties": {
            "citedIn": {
                "type": "nested"
            }
        }
    }
}
索引数据:
 {
        "citedIn": [
            {
                "someFiled": "Bar Councils Act, 1926 - Section 15"
            },
            {
                "someFiled": "Contract Act, 1872 - Section 23"
            }
        ]
    }
    {
        "citedIn": [
            {
                "someFiled": "15 C. B 400"
            },
            {
                "someFiled": "Contract Act, 1872 - Section 55"
            }
        ]
    }
    {
        "citedIn": [
            {
                "someFiled": "15 C. B 400"
            },
            {
                "someFiled": "Contract Act, 1872 - Section 15"
            }
        ]
    }
搜索查询:
{
    "query": {
        "nested": {
            "path": "citedIn",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "citedIn.someFiled": "contract"
                            }
                        },
                        {
                            "match": {
                                "citedIn.someFiled": "act"
                            }
                        },
                        {
                            "match": {
                                "citedIn.someFiled": 15
                            }
                        }
                    ]
                }
            },
            "inner_hits": {}
        }
    }
}
搜索结果:
"inner_hits": {
          "citedIn": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.620718,
              "hits": [
                {
                  "_index": "stof_64170705",
                  "_type": "_doc",
                  "_id": "3",
                  "_nested": {
                    "field": "citedIn",
                    "offset": 1
                  },
                  "_score": 1.620718,
                  "_source": {
                    "someFiled": "Contract Act, 1872 - Section 15"
                  }
                }
              ]
            }
          }
        }
      }

关于elasticsearch - 数组对象上的 Elasticsearch 匹配查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64170705/

10-13 07:48
查看更多