且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

弹性搜索:“术语”,“匹配短语”和“查询字符串”之间的区别

更新时间:2023-02-05 12:53:30

p> 术语查询符合单个术语:该值为未分析
所以,根据你所索引的内容,它不一定要更低。

term query matches a single term as it is : the value is not analyzed. So, it doesn't have to be lowercased depending on what you have indexed.

如果你提供了 Bennett 在索引时间,值不分析,以下查询不会返回任何内容:

If you provided Bennett at index time and the value is not analyzed, the following query won't return anything :

{
  "query": {
    "term" : { "user" : "bennett" }
  }
}

match_phrase 查询将分析输入,如果为查询字段定义分析器,并查找符合以下标准的文档:

match_phrase query will analyze the input if analyzers are defined for the queried field and find documents matching the following criterias :


  • 所有条款必须出现在字段

  • 中,他们必须具有相同的订单作为输入值

  • all the terms must appear in the field
  • they must have the same order as the input value

例如,如果您索引以下文档(使用标准分析器的字段 foo ):

For example, if you index the following documents (using standard analyzer for the field foo):

{ "foo":"I just said hello world" }

{ "foo":"Hello world" }

{ "foo":"World Hello" }

这个 match_phrase 查询只会返回第一个和第二个文档:

This match_phrase query will only return the first and second documents :

{
  "query": {
    "match_phrase": {
      "foo": "Hello World"
    }
  }
}

query_string 查询搜索,默认情况下,一个href =http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-all-field.html#mapping-all-field =noreferrer> _ all 字段它同时包含几个文本字段的文本。除此之外,它已被解析并支持一些运算符(AND / OR ...),通配符等(参见相关语法)。

query_string query search, by default, on a _all field which contains the text of several text fields at once. On top of that, it's parsed and supports some operators (AND/OR...), wildcards and so on (see related syntax).

作为 match_phrase 查询,根据查询字段上设置的分析器对输入进行分析。

As the match_phrase queries, the input is analyzed according to the analyzer set on the queried field.

match_phrase 不同,分析后获得的条款不必是相同的顺序,除非用户在输入周围使用引号。

Unlike the match_phrase, the terms obtained after analysis don't have to be in the same order, unless the user has used quotes around the input.

例如,使用与以前相同的文档,此查询将返回所有文档:

For example, using the same documents as before, this query will return all the documents :

{
  "query": {
    "query_string": {
      "query": "hello World"
    }
  }
}

但是这个查询将返回相同的2个文档 match_phrase 查询:

But this query will return the same 2 documents as the match_phrase query :

{
  "query": {
    "query_string": {
      "query": "\"Hello World\""
    }
  }
}

有更多关于这些查询的不同选项,请查看相关文档:

There is much more to say about the different options for those queries, please take a look at the related documentation :

  • term
  • match_phrase
  • query_string

希望这很清楚,这将有所帮助。

Hope this is clear enough and it will help.