【问题标题】:Elasticsearch Array Intersect QueryElasticsearch 数组相交查询
【发布时间】:2020-09-02 18:20:22
【问题描述】:

我正在尝试根据数组字段是否至少包含 另一个集合中的一个元素。

我有一个包含字符串数组的字段,例如:

_source: {
...
keywords: [
"Data Science",
"Chemistry",
"Biology",
"Computer Science",
"Economics"
]
}

我想过滤结果,使关键字至少包含以下一项 值列表,例如:

> ["History","Biology"] # would return the above document
> ["History","Geography"] # would not

它包含多少并不重要,只要它匹配 至少其中一个。

我尝试了条款查询,但没有给出任何结果

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl


    【解决方案1】:

    terms query 默认为 OR 查询,因此以下内容将匹配

    {
      "query": {
        "terms": {
          "keywords": [
            "Chemistry",
            "Geography"
          ]
        }
      }
    }
    

    如果要满足您的条件such that keywords contain at least one


    现在,如果要求使用 AND 查询(如果还有一个 not 匹配,则返回 null),您可以使用 bool-must 查询

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "keywords": {
                  "value": "Chemistry"
                }
              }
            },
            {
              "term": {
                "keywords": {
                  "value": "Geography"
                }
              }
            }
          ]
        }
      }
    }
    

    Depending on the mapping 您可能需要将.keyword 附加到相关字段。

    【讨论】:

    • 这有帮助,但它也会返回关键字包含我的关键字搜索词的子字符串的文档。使用你的第一个例子,我什至得到了“有机化学”的文档
    • 您能搜索一下您的索引映射吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多