数组对象上的弹性搜索匹配查询 [英] elastic search match query over array object

查看:62
本文介绍了数组对象上的弹性搜索匹配查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有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 查询

Here citedIn field is a array object.Now i want run a stander match query

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

}

上面的查询返回了所有3个doc,但是它假设仅 doc_3 包含 Contract act,因此返回了 doc_3 15 一起放在单个数组元素中.

The above query return all of the 3 doc, but it suppose to return doc_3 as only doc_3 contain Contract, act and 15 together in a single array element .

我将如何实现?

任何建议/解决方案都更可取

Any suggestion/Solution would be preferable

嵌套的数据类型更新:

我确实尝试过嵌套字段.这是我的映射

i did try nested field. This Is my mapping

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

但仍然得到相同的结果

推荐答案

添加包含索引数据,映射,搜索查询和搜索结果的有效示例.

Adding a working example with index data, mapping,search query, and search result.

您需要使用

You need to use nested query to search on nested fields

索引映射

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

这篇关于数组对象上的弹性搜索匹配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆