Elasticsearch学习(第一阶段:基础入门)保姆级!!!
目录
前言
一、简要定义
Elasticsearch和传统数据库查询的区别
二、核心概念
结构示例
倒排索引
什么是倒排索引?
倒排索引构建机制
分词
标准化处理
建立映射关系
查询原理分析
分词机制
什么是分词机制(Tokenization)
分析链组成
1. 字符过滤(Character Filters)
2. 分词器(Tokenizer)
3. 词元过滤器(Token Filters)
查看分析器效果(_analyze API)
1. standard 分词器(默认)
2.whitespace 分词器(仅按空格切,不小写)
3. keyword 分词器(整体作为一个词)
4.pattern 分词器(基于正则表达式)
常见 Token Filter 示例
1. lowercase 过滤器(统一小写)
2.stop 过滤器(去除停用词,如 \"is\", \"the\")
3.stemmer(词干提取,如 \"running\" → \"run\")
4.asciifolding(去除重音符号)
5.synonym 同义词过滤器(需要配置)
四、查询语法入门
1.创建索引
2.插入文档
3.删除文档
4.查询文档
1.查询索引中的所有文档
2.match 查询(全文检索,会分词)
3.term查询 (精确匹配,不分词)
4.bool查询 组合条件
5.range查询 范围条件
6.wildcard查询 通配符 查找以“小米”开头的所有 title。
7.fuzzy查询(模糊查询,拼写容错)
8._source 字段控制(只返回部分字段)
9.match_phrase(短语查询,顺序匹配)
10. 分页查询 + 排序
完结!
前言
想要学习一个技能,我们就需要知道它是什么,可以做什么这样个问题,这样我们才能够有自己的概念,知道我们的起点和目标,简单的了解一下Elasticserarch是什么?适合做什么?
一、简要定义
Elasticsearch是一个基于Lucene 构建的分布式搜索引擎,而Lucene适合做的就是 全文搜索(如商品、文章、问答系统),结构化数据分析(如日志分析、统计聚合),实时推荐、自动补全、模糊匹配。
Elasticsearch和传统数据库查询的区别
二、核心概念
说明
结构示例
三、倒排索引 & 分词机制
倒排索引
什么是倒排索引?
倒排索引是一种将关键词到文档的映射结构,主要用于信息检索和搜索引擎中,假设我们现在有两个文档,分别是:
// doc1{ \"title\": \"Elasticsearch is a powerful search engine\"}// doc2{ \"title\": \"Elasticsearch uses inverted index\"}// doc3{ \"title\":\"Search engines use indexes\"}
通过以上两个文档我们来解释一下正排索引和倒排索引:
- 正排索引:文档 → 包含的词语(如:doc1 → [\"Elasticsearch\", \"is\", \"powerful\",\"search\"])
- 倒排索引:词语 → 出现在哪些文档中(如:\"Elasticsearch\" → [doc1, doc2])
倒排索引构建机制
分词
首先Elasticsearch会对字段进行分析,然后把字段拆分成单词。
例子:
根据以上提供的文档分词结果如下
[\"elasticsearch\", \"is\", \"a\", \"powerful\", \"search\", \"engine\",\"uses\",\"inverted\",\"index\"]
标准化处理
-
小写处理(Elasticsearch → elasticsearch)
-
去除停用词(如 \"is\", \"a\" 可选去掉)
-
词干提取(如 \"running\" → \"run\")
建立映射关系
每个词项会记录:
-
在哪些文档中出现(docID)
-
在文档中的位置(position)
-
词频(term frequency)
-
偏移量(offset)
注*真实的 Elasticsearch 索引还包括 offset(字符位置)、payloads(可能的额外信息),这里为简化不展示辣,所谓的offset就是没文档中每个词的开始到结束的下表位置。
比如:Elasticsearch就是[(0,13)]
查询原理分析
假设我们搜索 \"powerful search\"
- 分词器分析并分词:得到的结果是[\"powerful\",\"search\"];
- 查询倒排索引:倒排列表的结果为\"powerful\"->[doc1],\"search\"->[doc1,doc2];
- 交集或计算相关性:找出包含所有关键词的文档就是doc1
- 计算评分:使用 TF-IDF、BM25 等打分算法,根据词频、倒排文档频率等综合计算得分最终的结果就是doc1
分词机制
什么是分词机制(Tokenization)
分词是将一段文本拆解成一个个关键字的过程,这个过程包括:
-
字符过滤(Character Filters)
-
分词器(Tokenizer)
-
词元过滤器(Token Filters)
这是 Elasticsearch 中的 分析链(Analysis Chain) 的三个阶段。
分析链组成
1. 字符过滤(Character Filters)
作用:在电商平台中,我的商品描述往往是通过富文本的方式存入到数据库中,为了保证分词的准确性,在分词前预处理文本,比如删除 HTML 标签、替换字符。
例子:
\"Elasticsearch
\" => \"Elasticsearch\"
2. 分词器(Tokenizer)
作用:将文本分成一个个的“关键词”或“Token”。
\"Powerful search engine\" => [\"Powerful\", \"search\", \"engine\"]
常用的分词器:
standard
(默认):
基于 Unicode 文本分割规则,效果较通用。
-
whitespace
:
按空格切词,不小写。
keyword
:
不切词,整体作为一个词项。
pattern
:
基于正则表达式切词。
-
ngram
/edge_ngram
:
用于模糊匹配和自动补全。
3. 词元过滤器(Token Filters)
作用:对分词结果进一步处理,如小写化、去停用词、词干还原。
常见的 token filter:
-
lowercase
:统一转小写 -
stop
:去除停用词(如 is, the, of) -
stemmer
:词干提取(running → run) -
synonym
:同义词替换 -
asciifolding
:处理重音符号(café → cafe)
查看分析器效果(_analyze API)
Elasticsearch 提供 _analyze
接口来查看具体分词效果。
1. standard
分词器(默认)
请求:
POST /_analyze{ \"analyzer\": \"standard\", \"text\": \"Elasticsearch is a powerful search engine\"}
响应:
{ \"tokens\": [ { \"token\": \"elasticsearch\", \"start_offset\": 0, \"end_offset\": 13, \"type\": \"\", \"position\": 0 }, { \"token\": \"is\", \"start_offset\": 14, \"end_offset\": 16, \"type\": \"\", \"position\": 1 }, { \"token\": \"a\", \"start_offset\": 17, \"end_offset\": 18, \"type\": \"\", \"position\": 2 }, { \"token\": \"powerful\", \"start_offset\": 19, \"end_offset\": 27, \"type\": \"\", \"position\": 3 }, { \"token\": \"search\", \"start_offset\": 28, \"end_offset\": 34, \"type\": \"\", \"position\": 4 }, { \"token\": \"engine\", \"start_offset\": 35, \"end_offset\": 41, \"type\": \"\", \"position\": 5 } ]}
2.whitespace
分词器(仅按空格切,不小写)
请求:
POST /_analyze{ \"tokenizer\":\"whitespace\", \"text\":\"Elasticsearch is Powerful and Flexible!\"}
响应:
{ \"tokens\": [ { \"token\": \"Elasticsearch\", \"start_offset\": 0, \"end_offset\": 13, \"type\": \"word\", \"position\": 0 }, { \"token\": \"is\", \"start_offset\": 14, \"end_offset\": 16, \"type\": \"word\", \"position\": 1 }, { \"token\": \"Powerful\", \"start_offset\": 17, \"end_offset\": 25, \"type\": \"word\", \"position\": 2 }, { \"token\": \"and\", \"start_offset\": 26, \"end_offset\": 29, \"type\": \"word\", \"position\": 3 }, { \"token\": \"Flexible!\", \"start_offset\": 30, \"end_offset\": 39, \"type\": \"word\", \"position\": 4 } ]}
3. keyword 分词器(整体作为一个词)
请求:
POST /_analyze{ \"tokenizer\": \"keyword\", \"text\": \"Elasticsearch is Powerful\"}
响应:
{ \"tokens\": [ { \"token\": \"Elasticsearch is Powerful\", \"start_offset\": 0, \"end_offset\": 25, \"type\": \"word\", \"position\": 0 } ]}
4.pattern
分词器(基于正则表达式)
请求:
POST /_analyze{ \"tokenizer\": { \"type\": \"pattern\", \"pattern\": \"[,\\\\s]+\" # 按空格或逗号分割 }, \"text\": \"Elasticsearch,is powerful\"}
响应:
{ \"tokens\": [ { \"token\": \"Elasticsearch\", \"start_offset\": 0, \"end_offset\": 13, \"type\": \"word\", \"position\": 0 }, { \"token\": \"is\", \"start_offset\": 14, \"end_offset\": 16, \"type\": \"word\", \"position\": 1 }, { \"token\": \"powerful\", \"start_offset\": 17, \"end_offset\": 25, \"type\": \"word\", \"position\": 2 } ]}
常见 Token Filter 示例
1. lowercase
过滤器(统一小写)
请求:
POST /_analyze{ \"tokenizer\":\"whitespace\", \"filter\": [\"lowercase\"], \"text\": \"Elasticsearch Is POWERFUL\"}
响应:
{ \"tokens\": [ { \"token\": \"elasticsearch\", \"start_offset\": 0, \"end_offset\": 13, \"type\": \"word\", \"position\": 0 }, { \"token\": \"is\", \"start_offset\": 14, \"end_offset\": 16, \"type\": \"word\", \"position\": 1 }, { \"token\": \"powerful\", \"start_offset\": 17, \"end_offset\": 25, \"type\": \"word\", \"position\": 2 } ]}
2.stop 过滤器(去除停用词,如 \"is\", \"the\")
请求:
POST /_analyze{ \"tokenizer\":\"standard\", \"filter\":[\"stop\"], \"text\": \"Elasticsearch is the best search engine\"}
响应:
{ \"tokens\": [ { \"token\": \"Elasticsearch\", \"start_offset\": 0, \"end_offset\": 13, \"type\": \"\", \"position\": 0 }, { \"token\": \"best\", \"start_offset\": 21, \"end_offset\": 25, \"type\": \"\", \"position\": 3 }, { \"token\": \"search\", \"start_offset\": 26, \"end_offset\": 32, \"type\": \"\", \"position\": 4 }, { \"token\": \"engine\", \"start_offset\": 33, \"end_offset\": 39, \"type\": \"\", \"position\": 5 } ]}
3.stemmer(词干提取,如 \"running\" → \"run\")
请求:
POST /_analyze{ \"tokenizer\":\"standard\", \"filter\":[\"lowercase\",\"stemmer\"], \"text\": \"Running Runs Runner Easily\"}
响应:
{ \"tokens\": [ { \"token\": \"run\", \"start_offset\": 0, \"end_offset\": 7, \"type\": \"\", \"position\": 0 }, { \"token\": \"run\", \"start_offset\": 8, \"end_offset\": 12, \"type\": \"\", \"position\": 1 }, { \"token\": \"runner\", \"start_offset\": 13, \"end_offset\": 19, \"type\": \"\", \"position\": 2 }, { \"token\": \"easili\", \"start_offset\": 20, \"end_offset\": 26, \"type\": \"\", \"position\": 3 } ]}
4.asciifolding(去除重音符号)
请求:
POST /_analyze{ \"tokenizer\": \"standard\", \"filter\": [\"lowercase\",\"asciifolding\"], \"text\":\"Café naïve élève\"}
响应:
{ \"tokens\": [ { \"token\": \"cafe\", \"start_offset\": 0, \"end_offset\": 4, \"type\": \"\", \"position\": 0 }, { \"token\": \"naive\", \"start_offset\": 5, \"end_offset\": 10, \"type\": \"\", \"position\": 1 }, { \"token\": \"eleve\", \"start_offset\": 11, \"end_offset\": 16, \"type\": \"\", \"position\": 2 } ]}
5.synonym 同义词过滤器(需要配置)
synonym必须放在索引 settings 中定义 analyzer,所以不能直接在 _analyze 中使用,需要自定义索引配置。如下:
# 创建索引 # settings中主要配置了同义单词和在分析器中设置过滤条件和分词规则PUT /my_index{ \"settings\": { \"analysis\": { \"filter\": { \"synonym_filter\": { \"type\": \"synonym\", \"synonyms\": [ \"quick,fast,speedy\", \"jumps,leaps\" ] } } }, \"analyzer\": { \"my_synonym_analyzer\": { \"tokenizer\": \"standard\", \"filter\": [ \"lowercase\", \"synonym_filter\" ] } } }}
请求:
POST /my_index/_analyze{ \"analyzer\":\"my_synonym_analyzer\", \"text\":\"fast jumps\"}
响应:
{ \"tokens\": [ { \"token\": \"fast\", \"start_offset\": 0, \"end_offset\": 4, \"type\": \"\", \"position\": 0 }, { \"token\": \"quick\", \"start_offset\": 0, \"end_offset\": 4, \"type\": \"SYNONYM\", \"position\": 0 }, { \"token\": \"speedy\", \"start_offset\": 0, \"end_offset\": 4, \"type\": \"SYNONYM\", \"position\": 0 }, { \"token\": \"jumps\", \"start_offset\": 5, \"end_offset\": 10, \"type\": \"\", \"position\": 1 }, { \"token\": \"leaps\", \"start_offset\": 5, \"end_offset\": 10, \"type\": \"SYNONYM\", \"position\": 1 } ]}
四、查询语法入门
1.创建索引
在实操练习之前我们需要先创建索引然后插入文档,如下:
# 创建索引时指定使用 ik_max_word 分词器和keywordPUT /products_ik_keyword{ \"settings\": { \"analysis\": { \"analyzer\": { \"ik_max\":{ \"type\": \"custom\", \"tokenizer\":\"ik_max_word\" } } } }, \"mappings\": { \"properties\": { \"title\":{ \"fields\": { \"keyword\":{ \"type\":\"keyword\", \"ignore_above\":552 } }, \"type\": \"text\", \"analyzer\":\"ik_max_word\", \"search_analyzer\":\"ik_smart\" }, \"brand\":{ \"type\":\"text\", \"analyzer\": \"ik_max_word\" }, \"discription\":{ \"type\": \"text\", \"analyzer\": \"ik_max_word\" }, \"price\":{ \"type\": \"integer\" }, \"note\":{ \"type\": \"text\", \"analyzer\": \"ik_max_word\" }, \"crdated_at\":{ \"type\": \"date\" } } }}
响应:
{ \"acknowledged\": true, \"shards_acknowledged\": true, \"index\": \"products_ik_keyword\"}
2.插入文档
请求:
POST /products_ik_keyword/_doc/1{ \"title\":\"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\":\"超长续航版\", \"created_at\": \"2025-07-15\"}POST /products_ik_keyword/_doc/2{ \"title\":\"红米手机11\", \"description\": \"骁龙7处理器,4000mAh大电池\", \"price\": 2299, \"note\":\"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\"}POST /products_ik_keyword/_doc/3{ \"title\":\"苹果手机15 PRO MAX\", \"description\": \"A18处理器,AI识别,超大电池容量5000mAh\", \"price\": 9999, \"note\":\"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\"}POST /products_ik_keyword/_doc/4{ \"title\":\"【限时立减700元 赠两年全面保】红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"description\": \"红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"price\": 3799, \"note\":\"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\"}POST /products_ik_keyword/_doc/5{ \"title\":\"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ultra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6999, \"note\":\"超长续航版,超具性价比\", \"created_at\": \"2025-07-22\"}POST /products_ik_keyword/_doc/6{ \"title\":\"【官方正品】OPPO Find X18 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ul1tra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6919, \"note\":\"超长续航版,超具性价比1\", \"created_at\": \"2025-07-23\"}
响应:
{ \"_index\": \"products_ik_keyword\", \"_id\": \"6\", \"_version\": 1, \"result\": \"created\", \"_shards\": { \"total\": 2, \"successful\": 1, \"failed\": 0 }, \"_seq_no\": 6, \"_primary_term\": 1}
3.删除文档
DELETE /products_ik_keyword/_doc/6
4.查询文档
1.查询索引中的所有文档
# 查询索引中的所有文档GET /products_ik_keyword/_search
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 5, \"relation\": \"eq\" }, \"max_score\": 1, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"2\", \"_score\": 1, \"_source\": { \"title\": \"红米手机11\", \"description\": \"骁龙7处理器,4000mAh大电池\", \"price\": 2299, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"3\", \"_score\": 1, \"_source\": { \"title\": \"苹果手机15 PRO MAX\", \"description\": \"A18处理器,AI识别,超大电池容量5000mAh\", \"price\": 9999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"4\", \"_score\": 1, \"_source\": { \"title\": \"【限时立减700元 赠两年全面保】红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"description\": \"红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"price\": 3799, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"5\", \"_score\": 1, \"_source\": { \"title\": \"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ultra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-22\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"1\", \"_score\": 1, \"_source\": { \"title\": \"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\": \"超长续航版\", \"created_at\": \"2025-07-15\" } } ] }}
2.match
查询(全文检索,会分词)
说明:会将“小米手机”分词成 “小米”和“手机”,只要 title 中包含任意词就可能命中。
请求:
GET /products_ik_keyword/_search{ \"query\": { \"match\": { \"title\": \"小米\" } }}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 2.0906212, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"1\", \"_score\": 2.0906212, \"_source\": { \"title\": \"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\": \"超长续航版\", \"created_at\": \"2025-07-15\" } } ] }}
3.term查询 (精确匹配,不分词)
说明:完全匹配“苹果手机15 PRO MAX”字符串,需使用 .keyword
字段。
请求:
GET /products_ik_keyword/_search{ \"query\": { \"term\": { \"title.keyword\": \"苹果手机15 PRO MAX\" } }}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 1.3862942, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"3\", \"_score\": 1.3862942, \"_source\": { \"title\": \"苹果手机15 PRO MAX\", \"description\": \"A18处理器,AI识别,超大电池容量5000mAh\", \"price\": 9999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } } ] }}
4.bool查询 组合条件
must
= 必须满足,must_not
= 排除条件。常用于多条件搜索。
请求:
GET /products_ik_keyword/_search{ \"query\": { \"bool\": { \"must\": [ { \"match\": { \"title\": \"红魔\" } }, { \"range\": { \"price\": { \"gte\": \"3000\" } } } ], \"must_not\": [ { \"match\": { \"description\": \"不知到\" } } ] } }}
响应:
{ \"took\": 407, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 2.3709877, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"4\", \"_score\": 2.3709877, \"_source\": { \"title\": \"【限时立减700元 赠两年全面保】红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"description\": \"红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"price\": 3799, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } } ] }}
5.range查询 范围条件
支持 gt
、gte
、lt
、lte
,用于时间、数值等字段。
请求:
GET /products_ik_keyword/_search{ \"query\": { \"range\":{ \"price\":{ \"gte\":3000, \"lte\":9000 } } }}
响应:
{ \"took\": 13, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 3, \"relation\": \"eq\" }, \"max_score\": 1, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"4\", \"_score\": 1, \"_source\": { \"title\": \"【限时立减700元 赠两年全面保】红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"description\": \"红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"price\": 3799, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-15\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"5\", \"_score\": 1, \"_source\": { \"title\": \"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ultra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-22\" } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"1\", \"_score\": 1, \"_source\": { \"title\": \"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\": \"超长续航版\", \"created_at\": \"2025-07-15\" } } ] }}
6.wildcard查询 通配符 查找以“小米”开头的所有 title。
说明:查找以“小米”开头的所有 title。
请求:
GET /products_ik_keyword/_search{ \"query\": { \"wildcard\": { \"title.keyword\": { \"value\": \"小米*\" } } }}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 1, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"1\", \"_score\": 1, \"_source\": { \"title\": \"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\": \"超长续航版\", \"created_at\": \"2025-07-15\" } } ] }}
7.fuzzy查询(模糊查询,拼写容错)
fuzzy查询参数详解
value
\"小米\"
fuzziness
0=完全匹配
1=1个字符的错误
2=最多两个字符错误
prefix_length
2
,那么\"小\"
=> \"小米\"
可以匹配,但 \"肖\"
就不行了max_expansions
transpositions
\"ab\"
和 \"ba\"
Ultra => Ultar (交换了 r 和 a)
false
可禁止字符交换请求:
GET /products_ik_keyword/_search{ \"query\": { \"fuzzy\": { \"title\": { \"value\": \"官方症拼\", # 要查询的词 \"fuzziness\":2, # 最大编辑距离:0=完全匹配 1=1个字符的错误 2=最多两个字符错误 \"prefix_length\": 1, # 保证前缀部分必须完全匹配的字符数 \"max_expansions\": 50, # Elasticsearch 会从倒排索引中扩展多少个可能的匹配词来尝试匹配,默认 50 \"transpositions\": true # 是否允许字符串交换位置查询 如 \"ab\" 和 \"ba\" } } }}
响应:
{ \"took\": 1, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 0, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"5\", \"_score\": 0, \"_source\": { \"title\": \"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ultra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-22\" } } ] }}
说明:可以匹配像 \"iphone\" 这样的词,允许 1 个字符的模糊。
8._source 字段控制(只返回部分字段)
请求:
GET /products_ik_keyword/_search{ \"_source\": [\"title\",\"price\"], \"query\": { \"range\": { \"price\":{ \"gte\":6000 } } }}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 2, \"relation\": \"eq\" }, \"max_score\": 1, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"3\", \"_score\": 1, \"_source\": { \"title\": \"苹果手机15 PRO MAX\", \"price\": 9999 } }, { \"_index\": \"products_ik_keyword\", \"_id\": \"5\", \"_score\": 1, \"_source\": { \"title\": \"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"price\": 6999 } } ] }}
9.match_phrase(短语查询,顺序匹配)
只会匹配出现完整短语 \"
骁龙8处理器,5000mAh大电池\"
的字段,如果中间少了一个文字比如说说\"骁龙8处理器,5000mAh电池\"就会匹配不到;
请求:
GET /products_ik_keyword/_search{ \"query\": { \"match_phrase\": { \"description\": \"骁龙8处理器,5000mAh大电池\" } }}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 1, \"relation\": \"eq\" }, \"max_score\": 7.5062494, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"1\", \"_score\": 7.5062494, \"_source\": { \"title\": \"小米手机12\", \"description\": \"骁龙8处理器,5000mAh大电池\", \"price\": 3299, \"note\": \"超长续航版\", \"created_at\": \"2025-07-15\" } } ] }}
10. 分页查询 + 排序
请求:
GET /products_ik_keyword/_search{ \"from\": 1, # 当前页 \"size\": 2, # 每页条数 \"sort\": [ { \"price\": \"desc\" } #根据价格降序 ]}
响应:
{ \"took\": 0, \"timed_out\": false, \"_shards\": { \"total\": 1, \"successful\": 1, \"skipped\": 0, \"failed\": 0 }, \"hits\": { \"total\": { \"value\": 5, \"relation\": \"eq\" }, \"max_score\": null, \"hits\": [ { \"_index\": \"products_ik_keyword\", \"_id\": \"5\", \"_score\": null, \"_source\": { \"title\": \"【官方正品】OPPO Find X8 Ultra oppofindx8ultra手机新款上市findx8s+0ppo官方旗舰店官网新品\", \"description\": \"OPPO Find X8 Ultra oppofindx8ultra手机新款上市 电池容量6100mAh,超广角像素5000万像素,有线充电功率100W,解锁方式面部识别/屏下指纹\", \"price\": 6999, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-22\" }, \"sort\": [ 6999 ] }, { \"_index\": \"products_ik_keyword\", \"_id\": \"4\", \"_score\": null, \"_source\": { \"title\": \"【限时立减700元 赠两年全面保】红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"description\": \"红魔9S Pro/Pro+骁龙8GEN3领先版165W第六代全面屏全功能NFC魔方AI 电竞手机\", \"price\": 3799, \"note\": \"超长续航版,超具性价比\", \"created_at\": \"2025-07-18\" }, \"sort\": [ 3799 ] } ] }}
完结!
第一阶段的内容就到此为止啦,接下来该着手第二阶段的内容了,还请大家耐心等待噢!
预告 第二阶段《构建一个 Spring Boot + Elasticsearch Demo 项目》