掌握ElasticSearch四种match查询的原理与应用
- 软件开发
- 2025-08-25 02:39:02

文章目录 一、引言 (Introduction)二、准备工作:创建索引和添加示例数据三、`match` 查询四、`match_all` 查询五、`multi_match` 查询六、`match_phrase` 查询七、总结 (Conclusion) 一、引言 (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精确短语匹配,要求词项的顺序和邻近度与查询字符串完全一致。当需要精确匹配一个短语,并且对词项的顺序和邻近度有严格要求时使用。掌握ElasticSearch四种match查询的原理与应用由讯客互联软件开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“掌握ElasticSearch四种match查询的原理与应用”