【问题标题】:Word and phrase search on multiple fields in ElasticSearch在 ElasticSearch 中的多个字段上进行单词和短语搜索
【发布时间】:2020-07-06 09:00:55
【问题描述】:

我想通过 ElasticSearch 使用 Python 搜索文档。我正在寻找在三个字段中的任何一个字段中包含单词和/或短语的文档。

GET /my_docs/_search
{
  "query": {
    "multi_match": {
      "query": "Ford \"lone star\"",
      "fields": [
        "title",
        "description",
        "news_content"
      ],
      "minimum_should_match": "-1",
      "operator": "AND"
    }
  }
}

在上述查询中,我想获取标题、描述或 news_content 包含“Ford”和“lone star”(作为短语)的文档。

但是,它似乎没有将“孤星”视为一个短语。它返回带有“Ford”、“lone”和“star”的文档。

【问题讨论】:

  • 能否请您看一下我的回答,如果您有进一步的说明,请告诉我
  • 您好,感谢您接受答案,很高兴我能提供帮助,如果您也可以对答案投赞成票,请多多支持。

标签: elasticsearch elasticsearch-dsl elasticsearch-query


【解决方案1】:

因此,我能够重现您的问题并使用 Elasticsearch 的 REST API 解决了它,因为我不熟悉 Python 语法,很高兴您以 JSON 格式提供搜索查询,我在此基础上构建了我的解决方案.

索引定义

{
    "mappings": {
        "properties": {
            "title": {
                "type": "text"
            },
            "description" :{
                "type" : "text"
            },
            "news_content" : {
                "type" : "text"
            }
        }
    }
}

示例文档

{
  "title" : "Ford",
  "news_content" : "lone star", --> note this matches your criteria
  "description" : "foo bar"
}

{
  "title" : "Ford",
  "news_content" : "lone",
  "description" : "star"
}

您正在寻找的搜索查询

{
    "query": {
        "bool": {
            "must": [ --> note this, both clause must match
                {
                    "multi_match": {
                        "query": "ford",
                        "fields": [
                            "title",
                            "description",
                            "news_content"
                        ]
                    }
                },
                {
                    "multi_match": {
                        "query": "lone star",
                        "fields": [
                            "title",
                            "description",
                            "news_content"
                        ],
                        "type": "phrase" --> note `lone star` must be phrase
                    }
                }
            ]
        }
    }
}

结果仅包含样本中的一个文档

"hits": [
      {
        "_index": "so_phrase",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.9527341,
        "_source": {
          "title": "Ford",
          "news_content": "lone star",
          "description": "foo bar"
        }
      }
    ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多