一、弁言 (Introduction)
在信息爆炸的期间,快速准确地找到所需信息至关重要。全文检索技术应运而生,它答应我们对文本内容进行深入搜索,而不仅仅是简单的关键词匹配。Elasticsearch 作为一个强盛的分布式搜索和分析引擎,提供了丰富的全文检索功能。
本文将重点先容 Elasticsearch 7.10 版本中四种焦点的全文检索查询:match、match_all、multi_match 和 match_phrase。通过本文,你将把握这些查询的基本概念、语法和利用场景,并通过丰富的示例学会怎样在实践中应用它们。
版本说明: 本文所有示例均基于 Elasticsearch 7.10 版本。
二、准备工作:创建索引和添加示例数据
在开始学习查询之前,我们需要先创建一个索引并添加一些示例数据。请确保你已经安装并启动了 Elasticsearch 7.10。保举利用 Kibana 的 Dev Tools 来执行以下操作。
- 创建索引 movies:
我们创建一个名为 movies 的索引,其中包含两个字段:title (电影标题) 和 description (电影描述)。这两个字段都利用 text 范例,以便进行全文检索。
- PUT movies
- {
- "mappings": {
- "properties": {
- "title": {
- "type": "text"
- },
- "description": {
- "type": "text"
- }
- }
- }
- }
复制代码
- 添加示例数据:
我们添加几条电影数据,以便后续进行查询演示。
- POST movies/_bulk
- {"index":{"_index": "movies"}}
- {"title": "The Dark Knight", "description": "A dark and gritty superhero film."}
- {"index":{"_index": "movies"}}
- {"title": "The Dark Knight Rises", "description": "The epic conclusion to the Dark Knight trilogy."}
- {"index":{"_index": "movies"}}
- {"title": "Batman Begins", "description": "The origin story of the Dark Knight."}
- {"index":{"_index": "movies"}}
- {"title": "Inception", "description": "A mind-bending science fiction thriller about dream sharing."}
- {"index":{"_index": "movies"}}
- {"title": "Interstellar", "description": "A visually stunning science fiction film about space travel."}
复制代码 三、match 查询
match 查询是 Elasticsearch 中执行全文搜索的标准查询。它的工作原理是:
- 分析 (Analysis): match 查询起首会对你提供的查询字符串进行 分析。分析过程会将文本分解成一系列的 词项 (terms)。这个过程通常包罗:
- 字符过滤 (Character Filtering): 去除 HTML 标签等。
- 分词 (Tokenization): 将文本分割成单词。
- 词项过滤 (Token Filtering): 将单词转换为小写、移除停用词(如 “a”, “the”, “is” 等)、进行词干提取(stemming)等。
- 匹配 (Matching): 然后,match 查询会在指定的字段中查找包含 至少一个 分词结果的文档。
基本概念:
- match 查询执行全文搜索,会对查询字符串进行分词。
- 默认情况下,只要文档中包含 任意一个 分词结果,就会被认为是匹配的(or 逻辑)。
- 可以通过 operator 参数将匹配逻辑改为 and,要求文档包含 所有 分词结果。
语法:
- GET index/_search
- {
- "query": {
- "match": {
- "field_name": {
- "query": "your search text",
- "operator": "or"
- }
- }
- }
- }
复制代码 参数说明:
- field_name: 要搜索的字段名。
- query: 要搜索的文本。
- operator (可选): 默认为 or,可以设置为 and。
示例:
- 示例 1:搜索包含 “dark” 或 “knight” 的电影
- GET movies/_search
- {
- "query": {
- "match": {
- "title": {
- "query": "Knight Rises"
- }
- }
- }
- }
复制代码 结果表明: 这个查询会返回所有标题中包含 “Rises” 或 “knight” 或两者都包含的电影(由于默认 operator 是 or)。根据我们之前添加的数据,会返回以下两条结果:
- “The Dark Knight”
- “The Dark Knight Rises” 。
- 示例 2:搜索同时包含 “dark” 和 “knight” 的电影
- GET movies/_search
- {
- "query": {
- "match": {
- "title": {
- "query": "Knight Rises",
- "operator": "and"
- }
- }
- }
- }
复制代码 结果表明: 通过设置 "operator": "and",这个查询要求标题中 同时 包含 “Knight” 和 “Rises” 分词后的结果。根据我们之前添加的数据,“The Dark Knight Rises” 会被匹配,由于它的标题分词后包含 “Knight” 和 “Rises”。
四、match_all 查询
match_all 查询非常简单,它会返回索引中的 所有 文档。这相当于没有任何查询条件,就像 SQL 中的 SELECT * FROM table。
基本概念:
- match_all 匹配所有文档。
- 通常用于与其他查询或过滤器结合利用。
语法:
- GET index/_search
- {
- "query": {
- "match_all": {}
- }
- }
复制代码 示例:
- GET movies/_search
- {
- "query": {
- "match_all": {}
- }
- }
复制代码 这个查询会返回 movies 索引中的所有文档,也就是我们之前添加的五条电影数据。
五、multi_match 查询
multi_match 查询答应你在 多个 字段中搜索相同的文本。这对于需要在多个字段中查找关键词的场景非常有效。
基本概念:
- multi_match 在多个字段中搜索相同的查询字符串。
- 默认情况下,它利用 best_fields 策略,即找到匹配度最高的字段。
语法:
- GET index/_search
- {
- "query": {
- "multi_match": {
- "query": "your search text",
- "fields": ["field1", "field2"]
- }
- }
- }
复制代码 参数说明:
- query: 要搜索的文本。
- fields: 一个数组,包含要搜索的字段名。
- type (可选): 确定怎样组合多个字段的匹配结果。默认为 best_fields。其他选项包罗 most_fields、cross_fields 等(这里不深入睁开)。
示例:
- GET movies/_search
- {
- "query": {
- "multi_match": {
- "query": "dark knight",
- "fields": ["title", "description"]
- }
- }
- }
复制代码 结果表明: 这个查询会在 title 和 description 两个字段中搜索 “dark knight”。它会返回在这两个字段的任意一个中匹配到该文本(分词后的词项)的电影。根据示例数据和match的分析,它应该会返回以下结果:
- “The Dark Knight”
- “The Dark Knight Rises”
- “Batman Begins” (由于其 description 包含 “the Dark Knight”)
- // 部分json
- {
- "hits" : [
- {
- "_index" : "movies",
- "_type" : "_doc",
- "_id" : "mmjpI5UBFTEr5wdTXFgU",
- "_score" : 1.5241971,
- "_source" : {
- "title" : "The Dark Knight",
- "description" : "A dark and gritty superhero film."
- }
- },
- {
- "_index" : "movies",
- "_type" : "_doc",
- "_id" : "nGjpI5UBFTEr5wdTXFgU",
- "_score" : 1.4764125,
- "_source" : {
- "title" : "Batman Begins",
- "description" : "The origin story of the Dark Knight."
- }
- },
- {
- "_index" : "movies",
- "_type" : "_doc",
- "_id" : "m2jpI5UBFTEr5wdTXFgU",
- "_score" : 1.3997822,
- "_source" : {
- "title" : "The Dark Knight Rises",
- "description" : "The epic conclusion to the Dark Knight trilogy."
- }
- }
- ]
- }
复制代码 六、match_phrase 查询
match_phrase 查询用于执行 短语匹配。它要求:
- 所有 查询词项都必须出现在文档中。
- 查询词项的 次序 必须与文档中的次序完全一致。
- 查询词项在文档中必须是 相邻 的(默认情况下)。
基本概念:
- match_phrase 用于准确的短语匹配。
- 它要求词项的次序和邻近度与查询字符串完全一致。
- slop 参数答应短语中的词项之间存在一定隔断。
语法:
- GET index/_search
- {
- "query": {
- "match_phrase": {
- "field_name": {
- "query": "your exact phrase"
- }
- }
- }
- }
复制代码
- 参数说明:
- field_name:字段名称
- query:短语内容
示例:
- GET movies/_search
- {
- "query": {
- "match_phrase": {
- "title": {
- "query": "Dark Knight Rises"
- }
- }
- }
- }
复制代码 结果表明: 根据数据,只有 title 为"The Dark Knight Rises"的数据会被搜索到。假如一部电影的标题是 “The Dark Knight”,则不会被匹配到,由于词项不完全一致。
七、总结 (Conclusion)
下表总结了本文先容的四种全文检索查询:
查询范例描述实用场景match标准的全文搜索查询,对查询字符串进行分词,匹配包含任意一个或多个分词的文档。最常用的查询范例,实用于大多数全文搜索场景。match_all返回索引中的所有文档。通常与其他查询或过滤器结合利用。multi_match在多个字段中搜索相同的文本。当需要在多个字段中查找关键词时非常有效。match_phrase准确短语匹配,要求词项的次序和邻近度与查询字符串完全一致。当需要准确匹配一个短语,而且对词项的次序和邻近度有严酷要求时利用。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |