在弹性搜索中匹配整个语句

2024-05-29 06:02:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在弹性搜索中查询时匹配整个语句。但无法正确地实现它

"query": {
            "match_phrase": {"description": query_tokens}
        }

我尝试使用Air Conditioning, Air Conditioner,但它提供了类似于常规匹配查询的结果。 如何实现完整的语句获取


Tags: matchdescription语句airquery常规弹性tokens
1条回答
网友
1楼 · 发布于 2024-05-29 06:02:29

解决方案

将这些语句(描述字段)存储在keyword field

不工作的原因

对匹配短语查询进行分析,这意味着在索引时使用相同的分析器,用作创建搜索标记的查询时,默认情况下,text字段的standard analyzer在空间和,id特殊字符上断开标记

Elasticsearch文档中的声明

The match_phrase query analyzes the text and creates a phrase query out of the analyzed text. For example:

索引定义

{
    "mappings": {
        "properties": {
            "description": 
            {
                "type" : "keyword"
            }
        }
    }
}

索引样本文档

{
  "description" :   "Air Conditioning, Air Conditioner"
}

搜索查询

{
    "query": {
        "match_phrase" : {
            "description" : {
                "query" : "Air Conditioning, Air Conditioner"
            }
        }
    }
}

搜索结果

"hits": [
      {
        "_index": "match",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "description": "Air Conditioning, Air Conditioner"
        }
      }
    ]

相关问题 更多 >

    热门问题