基础概念
索引Index:相当于MYSQL的数据库
文档Document:相当于MYSQL的一行记录
字段Field:相当于MYSQL中的列
映射Mapping:相当于MYSQL的表结构
索引
添加索引
PUT 方法
127.0.0.1:9200/test_index重复添加会报错。
删除索引
DELETE 方法
127.0.0.1:9200/test_index查询索引
文档
添加文档
127.0.0.1:9200/test_index/_doc/1删除文档
全量修改
增量修改
映射
类型
text:文本,默认分词。
keyword:关键词,该类型只能整体搜索。
long、integer、short、byte、double、float 数值
date:日期
boolean:布尔值
object:对象
nested:嵌套对象数组
除了 binary 类型默认不索引,其他数据类型都是默认索引的。
动态映射:在插入文档的时候,会根据插入的数据自动推断字段类型,可能会导致字段类型不符合预期。
显式映射:手动指定
在创建索引的时候添加映射
dynamic 控制动态字段的行为:
true:默认,自动生成字段
false:忽略新字段
strict:插入新字段报错
index 控制是否能被索引
PUT方法
127.0.0.1:9200/索引名{
"mappings": {
"properties": {
"id": {
"type": "long",
"index": "false"
},
"archive": {
"type": "integer",
"index": "false"
},
"title": {
"type": "text",
"analyzer": "ik_smart"
},
"cover": {
"type": "keyword",
"index": "false"
},
"summary": {
"type": "text",
"analyzer": "ik_smart"
},
"content_text": {
"type": "text",
"analyzer": "ik_smart"
},
"content": {
"type": "text",
"index": "false"
},
"source": {
"type": "integer",
"index": "false"
},
"status": {
"type": "integer",
"index": "false"
},
"like": {
"type": "integer",
"index": "false"
},
"view": {
"type": "integer",
"index": "false"
},
"reply": {
"type": "integer",
"index": "false"
},
"tags": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
}
}
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "long",
"index": "false"
},
"name": {
"type": "keyword"
}
}
},
"column": {
"type": "object",
"properties": {
"id": {
"type": "long",
"index": "false"
},
"name": {
"type": "keyword"
}
}
},
"createdAt": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"updatedAt": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
}1. 通用字段参数
这些参数适用于大多数字段类型。
2. 针对 text 类型的参数
用于控制分词行为和文本搜索特性。
3. 针对 keyword 类型的参数
用于精确匹配字段。
4. 针对数值类型的参数
适用于 integer、float 等数值类型。
5. 针对 date 类型的参数
用于日期字段的配置。
6. 针对 geo_point 和 geo_shape 类型的参数
用于地理位置和形状。
在已创建的索引上增加映射
索引一旦创建,映射的字段是无法删除的。
IK分词器
查询
基本搜索
一、查询所有,即无条件查询。
1、GET方法,不带请求体
127.0.0.1:9200/test_index/_search1、GET方法,带请求体
127.0.0.1:9200/test_index/_search
{
"query": {
"match_all": {
}
}
}这两个效果一样。
二、条件查询
查询 title 字段中包含的
GET /posts/_search{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}多字段查询
有一个满足就行了
{
"query": {
"multi_match": {
"query": "Elasticsearch tutorial",
"fields": ["title", "summary", "plainText"]
}
}
}布尔查询
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Elasticsearch" }},
{ "match": { "content": "search" }}
],
"should": [
{ "match": { "tags": "tutorial" }}
],
"must_not": [
{ "match": { "status": 2 }}
]
}
}
}must:and,查询的条件,必须都满足
should:or,只满足一个就行了,满足的越多得分越高
must_not:not,根据条件排除
词项查询
用于精准匹配
{
"query": {
"term": {
"status": 1
}
}
}范围查询
{
"query": {
"range": {
"createdAt": {
"gte": "2025-01-01",
"lte": "2025-12-31"
}
}
}
}过滤查询
只对文档进行筛选,不打分排序,比must布尔查询高效。
{
"query": {
"bool": {
"filter": [
{ "term": { "status": 1 }},
{ "range": { "createdAt": { "gte": "2025-01-01", "lte": "2025-01-31" }}}
]
}
}
}聚合查询
聚合用于对查询结果进行统计分析,如计数、平均值、最大值等。常用于获取统计数据。
1、计数聚合:统计 status 字段的不同值及其出现的次数。
{
"size": 0,
"aggs": {
"status_count": {
"terms": {
"field": "status"
}
}
}
}2、日期直方图聚合
{
"size": 0,
"aggs": {
"date_histogram": {
"field": "createdAt",
"calendar_interval": "month"
}
}
}这个查询会按月份对 createdAt 字段进行直方图聚合,统计每个月的文档数量。
排序查询
排序用于对查询结果按照指定字段进行升序或降序排序。
GET /posts/_search
{
"query": {
"match_all": {}
},
"sort": [
{ "createdAt": { "order": "desc" }}
]
}分页查询
{
"from": 10,
"size": 10,
"query": {
"match_all": {}
}
}高亮显示
高亮显示用于突出显示查询匹配的部分,适用于展示查询结果时显示高亮。
{
"query": {
"match": {
"title": "Elasticsearch"
}
},
"highlight": {
"fields": {
"title": {
"pre_tags": ["<span class='highlight'>"],
"post_tags": ["</span>"]
}
}
}
}