【问题标题】:ElasticSearch - Phrase match on whole document? Not just one specific fieldElasticSearch - 整个文档的短语匹配?不仅仅是一个特定的领域
【发布时间】:2020-10-25 02:45:49
【问题描述】:

有没有办法可以在整个文档上使用弹性 match_phrase?不仅仅是一个特定的领域。

我们希望用户能够输入带引号的搜索词,并在文档中的任何位置进行词组匹配。

{
    "size": 20,
    "from": 0,
    "query": {
        "match_phrase": {
            "my_column_name": "I want to search for this exact phrase"
        }
    }
}

目前,我只找到特定字段的词组匹配。我必须指定要在其中进行短语匹配的字段。

我们的文档有数百个字段,因此我认为在每个 match_phrase 查询中手动输入 600 多个字段是不可行的。生成的 JSON 会很大。

【问题讨论】:

    标签: elasticsearch match quote phrase match-phrase


    【解决方案1】:

    您可以使用带有类型短语的multi-match query,对每个字段运行 match_phrase 查询并使用最佳字段中的 _score。请参阅短语和短语前缀。

    如果未提供任何字段,则 multi_match 查询默认为 index.query.default_field 索引设置,默认为 *. 这会提取映射中符合术语查询条件的所有字段并过滤元数据字段。然后所有提取的字段 结合起来构建一个查询。

    添加一个包含索引数据、搜索查询和搜索结果的工作示例

    索引数据:

    {
        "name":"John",
        "cost":55,
        "title":"Will Smith"
    }
    {
        "name":"Will Smith",
        "cost":55,
        "title":"book"
    }
    

    搜索查询:

    {
      "query": {
        "multi_match": {
          "query": "Will Smith",
          "type": "phrase"
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "64519840",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.2199391,
            "_source": {
              "name": "Will Smith",
              "cost": 55,
              "title": "book"
            }
          },
          {
            "_index": "64519840",
            "_type": "_doc",
            "_id": "2",
            "_score": 1.2199391,
            "_source": {
              "name": "John",
              "cost": 55,
              "title": "Will Smith"
            }
          }
        ]
    

    【讨论】:

    • @AllenH。很高兴这对你有用,感谢你接受我的回答?
    【解决方案2】:

    您可以在匹配查询字段参数中使用 *,它将搜索文档中的所有可用字段。但它会降低您的查询速度,因为您正在搜索整个文档

    【讨论】:

    • 嗨,我试过了,但它不起作用。我做对了吗? { "size": 20, "from": 0, "query": { "match_phrase": { "*": "我的短语 123" } } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多