简体   繁体   English

在 Elasticsearch 查询字符串查询的短语中使用通配符

[英]Using wildcards in phrases with Elasticsearch Query String Query

Using the wildcard operator, I can match terms starting with some value:使用通配符运算符,我可以匹配以某个值开头的术语:

{
    "query": {
        "query_string" : {
            "query" : "subject:cell*"
        }
    }
}

The subject field here is a keyword field (non-analyzed).这里的subject字段是keyword字段(未分析)。 This works fine, but I cannot figure out how to find terms starting with, say, "cellular contr".这工作正常,但我不知道如何找到以“蜂窝控制”开头的术语。 Trying double quotes did not yield the expected results:尝试双引号没有产生预期的结果:

{
    "query": {
        "query_string" : {
            "query" : "subject:\"cellular contr*\""
        }
    }
}

Note: phrase search works fine with exact matches, just not with the wildcard.注意:短语搜索适用于完全匹配,但不适用于通配符。 My guess is that the star is not interpreted as a wildcard operator inside the double quotes.我的猜测是星号不会被解释为双引号内的通配符运算符。 Is that correct?那是对的吗? And is there any other way to use the wildcard operator with a phrase?还有其他方法可以在短语中使用通配符运算符吗?

Note: I have to use Query String Query , since the query is coming from user input.注意:我必须使用Query String Query ,因为查询来自用户输入。

(I know I could resort to regexp, but would prefer not to) (我知道我可以求助于正则表达式,但不想这样做)

In addition to the custom analyzer as pointed by Hemed, you need to do search as below -除了 Hemed 指出的自定义分析器之外,您还需要进行如下搜索 -

{
    "query": {
        "query_string" : {
            "query" : "subject:cellular\\ contr*"
        }
    }
}

Found it after a lot of research and tries!经过大量研究和尝试后找到了它!

EDIT: Define custom analyzer for searching:-编辑:定义用于搜索的自定义分析器:-

settings:
   index:
     analysis:
       analyzer:
         keyword_analyzer:
           type: custom
           tokenizer: keyword
           filter:
             - lowercase

Found out that you need to use Prefix Query in this case, because Query String Query always segments on spaces during parsing.发现在这种情况下需要使用Prefix Query ,因为Query String Query在解析时总是在空格上进行分段。

But since you are using lowecase filter in this field and Prefix Query does not support analyzer, you would have to lowercase user input before appending it to the query.但是由于您在此字段中使用小lowecase filter并且前缀查询不支持分析器,因此您必须在将用户输入附加到查询之前将其小写。

New query becomes:-新查询变为:-

   {
        "query": {
            "prefix" : {
                "subject" : "cellular contr"
            }
        }
    }

Alternatively, you can use Match Phrase Query which supports analyzer.或者,您可以使用支持分析器的匹配短语查询

{
    "query": {
        "match_phrase_prefix" : {
            "subject" : {
                 "query" : "Cellular contr",
                  "analyzer" : "keyword_analyzer",
                  "max_expansions" : 100
                 }
              }
         }
    }

Try this:试试这个:

{
    "query": {
        "query_string" : {
            "query" : "subject:"cellular contr*",
            "split_on_whitespace" : false
        }
    }
}

Does this work for ES 6.x?这适用于 ES 6.x 吗? I tried this in ES 6.x cluster but search doesnt return correct hits .我在 ES 6.x 集群中尝试过这个,但搜索没有返回正确的命中。 The problem occurs while using wildcard * within quotes.在引号内使用通配符 * 时会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM