【问题标题】:Elasticsearch multi match exact phrase on all fieldsElasticsearch 在所有字段上多匹配精确短语
【发布时间】:2021-02-10 01:33:29
【问题描述】:

我们目前有这样的查询:

                    "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",
    }
}

提前致谢。

【问题讨论】:

  • 你能解释一下吗它不会在字段中搜索确切的短语“john doe”。
  • @ESCoder 查询返回同一字段中包含“john”和“doe”的所有文档,而不是“john doe”的完全匹配

标签: elasticsearch match phrase


【解决方案1】:

添加一个工作示例

索引数据:

{
  "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"
        }
      }
    ]

【讨论】:

  • 我正在使用完全相同的查询,但它对我不起作用。我的数据返回了一堆结果,其中只有一个完全是“john doe”。请问有什么想法吗?
  • @AllenH。能否请您分享您的索引映射、示例索引数据?
  • 请问如何找到我的索引映射?
  • @AllenH。你可以通过GET /<your-index-name>/_mapping获取你的索引映射参考这个文档elastic.co/guide/en/elasticsearch/reference/current/…
  • 谢谢,我用索引信息更新了 OP。它主要是香草字符串和日期对象,我认为没有自定义。数据也超标准。
猜你喜欢
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多