【问题标题】:How can I find documents which match all words in differents queries?如何找到与不同查询中的所有单词匹配的文档?
【发布时间】:2021-06-13 20:41:28
【问题描述】:

例如这个映射:

PUT /unit-test
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "landlords": {
        "type": "nested", 
        "properties": {
          "name": { "type": "text" }
        }
      }
    }
  }
}

如果我有这份文件:

{ 
  "name": "T2 - Boulevard Haussmann - P429",
  "landlords": [
    { "name": "John Doe" }
  ] 
} 

我希望 "boulevard hausmann" 和 "boulevard haussman doe" 匹配,但不匹配 "rue haussman" 或 "haussman jeanne"。

我不能将multi_match"operator": "and" 一起使用,因为landlords 是嵌套的。

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl elasticsearch-7


    【解决方案1】:

    一个想法是将copy_to 映射参数设置为namelandlords.name 字段,以便将这两个字段的值复制到另一个字段(例如names),您将用于您的搜索。

    所以您的映射可能如下所示:

    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "copy_to": "names"
          },
          "landlords": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "copy_to": "names"
              }
            }
          },
          "names": {
            "type": "text"
          }
        }
      }
    }
    

    和你的搜索

    {
      "query": {
        "match": {
          "names": {
            "query": "boulevard haussman doe",
            "operator": "AND"
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2010-10-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2021-10-30
      • 2019-07-27
      相关资源
      最近更新 更多