首页 > 解决方案 > 如何找到与不同查询中的所有单词匹配的文档?

问题描述

例如,使用此映射:

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_matchwith"operator": "and"因为landlords是嵌套的。

标签: elasticsearchelasticsearch-dslelasticsearch-7

解决方案


一个想法是将copy_to映射参数设置为nameandlandlords.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"
      }
    }
  }
}

推荐阅读