首页 > 解决方案 > Elasticsearch 在所有字段上多匹配精确短语

问题描述

我们目前有这样的查询:

                    "query": {
                        "multi_match": {
                            "query": "john doe",
                             analyzer: "whitespace",
                             type: "most_fields",

                        }
                    }

基本上,这会在文档的所有字段中搜索“john”或“doe”。

我们如何才能在所有领域准确地搜索“john doe”?

这是迄今为止我能做的最好的:

                    "query": {
                        "multi_match": {
                            "query": "john doe",
                            "type": "phrase", 
                        }
                    }

不幸的是,这会在一个字段中搜索“john”和“doe”。它不会在字段中搜索确切的短语“john doe”。

除了将所有字段复制到一个包含所有值的大字段中之外,没有其他办法吗?然后在该字段上运行标准 match_phrase 查询?

以下是具有虚拟字段名称的索引示例:

{
    "my_index": {
        "mappings": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "date1": {
                    "type": "date"
                },
                "date2": {
                    "type": "date"
                },
                "text1": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "text2": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "text3": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }

    }
}

样本数据(真实数据是超标准的扁平化字符串)

{
    "_index": "myIndex",
    "_type": "_doc",
    "_id": "013345",
    "_score": 7.414526,
    "_source": {
        "date1": "2017-05220T03:59:59.000Z",
        "text1": "Available ",
        "text2": "Hello i am john doe",
        "text3": "Ministry",
        "text4": "Decision",
    }
}

提前致谢。

标签: elasticsearchmatchphrase

解决方案


添加一个工作示例

指数数据:

{
  "name": "b",
  "user": "john doe is great"
}
{
  "name": "john doe",
  "user": "a"
}
{
  "name": "b",
  "user": "john is doe great"
}

搜索查询:

"john doe"此查询在所有字段中搜索确切的短语。

{
  "query": {
    "multi_match": {
      "query": "john doe",
      "type": "phrase"
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "66129712",
        "_type": "_doc",
        "_id": "1",
        "_score": 2.1784627,
        "_source": {
          "name": "john doe",
          "user": "a"
        }
      },
      {
        "_index": "66129712",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.50632054,
        "_source": {
          "name": "b",
          "user": "john doe is great"
        }
      }
    ]

推荐阅读