且构网

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

Elasticsearch:“术语"、“匹配短语"和“查询字符串"之间的区别

更新时间:2021-10-30 23:24:20

term 查询匹配单个术语,因为它的值是未分析.因此,它不必小写,具体取决于您编入索引的内容.

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 criteria:

  • 所有术语必须出现在字段中
  • 它们必须与输入值具有相同的顺序
  • 不能有任何中间词,即是连续的(可能不包括停用词,但这可能很复杂)

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

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" }

{ "foo":"Hello dear world" }

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

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

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

query_string 查询搜索,默认在 _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"
    }
  }
}

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

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 :

希望这足够清楚并且会有所帮助.

Hope this is clear enough and it will help.