Elasticsearch 精确匹配与全文搜索
Elasticsearch 精确匹配与全文搜索,文字内容来自 B 站中华石杉 Elasticsearch 高手进阶课程,英文内容来自官方文档。
精确匹配 exact value
上一篇文章中的例子,查询“2017-01-01”,exact value,搜索的时候,必须输入 2017-01-01,才能搜索出来;如果你输入一个 01,是搜索不出来的
全文搜索 full text
缩写 vs. 全称:cn vs. china
格式转化:like liked likes
大小写:Tom vs tom
同义词:like vs love
对于“2017-01-01”,拆解为“2017”、“01”、“01”,搜索“2017”,或者“01”,都可以搜索出来。
注意,这里是把“2017-01-01”当做一个字符串,而不是日期类型。
相应的,在全文搜索的情况下:
china,搜索 cn,也可以将 china 搜索出来
likes,搜索 like,也可以将 likes 搜索出来
Tom,搜索 tom,也可以将 Tom 搜索出来
like,搜索 love,同义词,也可以将 like 搜索出来
就不是说单纯的只是匹配完整的一个值,而是可以对值进行拆分词语后(分词)进行匹配,也可以通过缩写、时态、大小写、同义词等进行匹配
其实我觉得这个也在某种程度上回答了上一篇中的问题,因为是精确查找,所以每次只能找到一条数据。
在 Elasticsearch 中 term query 应该属于精确查找,而 match query 属于全文搜索。
Term query
Returns documents that contain an exact term in a provided field.
You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.
Avoid using the term query for text fields.
By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.
To search text field values, use the match query instead.
Avoid using the term query for text fields
By default, Elasticsearch changes the values of text fields during analysis. For example, the default standard analyzer changes text field values as follows:
Removes most punctuation
Divides the remaining content into individual words, called tokens
Lowercases the tokens
To better search text fields, the match query also analyzes your provided search term before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term.
The term query does not analyze the search term. The term query only searches for the exact term you provide. This means the term query may return poor or no results when searching text fields.
the match query analyzes your provided search term, Quick Brown Foxes!, before performing a search. The match query then returns any documents containing the quick, brown, or fox tokens in the full_text field.
版权声明: 本文为 InfoQ 作者【escray】的原创文章。
原文链接:【http://xie.infoq.cn/article/f6ee52a40a288810ba6cf4c69】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
评论