如何使query_string在ElasticSearch中搜索完全匹配的词组 [英] How to make query_string search exact phrase in ElasticSearch

查看:563
本文介绍了如何使query_string在ElasticSearch中搜索完全匹配的词组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Elasticsearch中放了2个文件:

  curl -XPUThttp:// localhost:9200 / vehicles / vehicle / 1-d'
{
model:Classe A
}'

curl -XPUThttp:// localhost:9200 / vehicles /车辆/ 2-d'
{
型号:Classe B
}'

为什么这个查询返回2个文件:

  curl -XPOSThttp:// localhost:9200 / vehicles / _search-d'
{
query:{
query_string:{
query:model:\Classe A \
}
}
}'

这一个,只有第二个文件:

  curl -XPOSThttp:// localhost:9200 / vehicles / _search-d '
{
query:{
query_string:{
query:model:\Classe B\
}
}
}'

我想要弹性搜索来匹配确切的短语I传递给查询参数,WI这个空格,我该怎么做?

解决方案

你需要看的是分析器您正在使用。如果您没有指定弹性搜索将使用标准分析器。对于大多数具有纯文本输入的情况,这是非常好的,但对于您提到的用例无效。



标准分析器将做什么分割在您的字符串中,然后将其转换为小写。



如果要匹配整个字符串Classe A,并将其与Classe B区分开,您可以使用关键字分析器。这将使整个字段保持为一个字符串。



然后,您可以使用匹配查询返回您期望的结果。



创建映射:

  PUT车辆
{
mappings:{
vehicle:{
properties:{
model:{
type:string,
analyzer:keyword
}
}
}
}
}

执行查询:

  POST车辆/ _search 
{
查询:{
match:{
model:Classe A
}
}
}

如果要使用 query_string 查询,则可以将运算符设置为 AND

  POST车辆/车辆/ _search 
{
查询: {
query_string:{
query:Classe B,
default_operator:AND
}
}
}


I put 2 documents in Elasticsearch :

curl -XPUT "http://localhost:9200/vehicles/vehicle/1" -d'
{
    "model": "Classe A"
}'

curl -XPUT "http://localhost:9200/vehicles/vehicle/2" -d'
{
    "model": "Classe B"
}'

Why is this query returns the 2 documents :

curl -XPOST "http://localhost:9200/vehicles/_search" -d'
{
  "query": {
    "query_string": {
      "query": "model:\"Classe A\""
    }
  }
}'

And this one, only the second document :

curl -XPOST "http://localhost:9200/vehicles/_search" -d'
{
  "query": {
    "query_string": {
      "query": "model:\"Classe B\""
    }
  }
}'

I want elastic search to match on the exact phrase I pass to the query parameter, WITH the whitespace, how can I do that ?

解决方案

What you need to look at is the analyzer you're using. If you don't specify one Elasticsearch will use the Standard Analyzer. It is great for the majority of cases with plain text input, but doesn't work for the use case you mention.

What the standard analyzer will do is split the words in your string and then converts them to lowercase.

If you want to match the whole string "Classe A" and distinguish this from "Classe B", you can use the Keyword Analyzer. This will keep the entire field as one string.

Then you can use the match query which will return the results you expect.

Create the mapping:

PUT vehicles
{
  "mappings": {
    "vehicle": {
      "properties": {
        "model": {
          "type": "string",
          "analyzer": "keyword"
        }
      }
    }
  }
}

Perform the query:

POST vehicles/_search
{
  "query": {
    "match": {
      "model": "Classe A"
    }
  }
}

If you wanted to use the query_string query, then you could set the operator to AND

POST vehicles/vehicle/_search
{
  "query": {
    "query_string": {
      "query": "Classe B",
      "default_operator": "AND"
    }
  }
}

这篇关于如何使query_string在ElasticSearch中搜索完全匹配的词组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆