【问题标题】:Elasticsearch query words spread across multiple fieldsElasticsearch 查询词分布在多个字段中
【发布时间】:2021-04-21 22:03:00
【问题描述】:

我需要在 Elasticsearch 中进行查询,该查询只有在搜索文本中的所有单词都被找到时才返回文档,无论在哪个字段中。例如:

searchText = "term1 term2 term3 term4"
fields = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6']

如果 searchText 中的所有单词都在从 1 到 6 的任何字段中,则返回文档。否则什么都不返回。谢谢!

这种文件应该被退回:

searchText = 'We need to change ourselves'
fields = [
  field1: "We are happy.",
  field2: "change ourselves",
  field3: "You are the one I need"
  field4: "to be",
  field5: "irrelevant",
  field6: ""
]

但不是这个(未找到:Weourserves):

searchText = 'We need to change ourselves'
fields = [
  field1: "I am happy.",
  field2: "change",
  field3: "You are the one I need"
  field4: "to be",
  field5: "irrelevant",
  field6: "something"
]

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以将multi_match 与 AND 运算符一起使用

    {
      "query": {
        "multi_match": {
          "query": "term1 term2 term3 term4",
          "fields": [
            "field1",
            "field2",
            "field3",
            "field4",
            "field5",
            "field5"
          ],
          "operator": "AND"
        }
      }
    }
    

    更新 1:

    正如@Razvan Theodor 已经回答的那样,cross_fields 将支持搜索跨多个字段的查询词

    修改后的查询将是

    {
      "query": {
        "multi_match": {
          "query": "term1 term2 term3 term4",
          "fields": [
            "field1",
            "field2",
            "field3",
            "field4",
            "field5",
            "field5"
          ],
          "operator": "AND",
          "type": "cross_fields" // note this
        }
      }
    }
    

    【讨论】:

    • 是的,operator: "and"type: "cross_fields" 会变魔术 :) 你为什么不更新你的答案插入 type: "cross_fields"
    • 如果您将type: "cross_fields" 放入其中,我会赞成并接受它是准确的:)
    • 如果我只需要查询部分单词怎么办?让我们这样说:Mich Jack 应该返回如下文件:{ "first_name": "Jackson", "last_name": "Michael"}
    【解决方案2】:

    找到解决办法:

    "query": {
        "multi_match": {
           "query": "term1 term2 term3 term4",
           "type": "cross_fields",
           "operator": "and",
           "fields" => {
                "field1",
                "field2",
                "field3",
                "field4",
                "field5",
                "field6"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2022-06-29
      • 2016-08-18
      • 2022-12-04
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多