基础查询
1. 全部匹配查询
GET _search
{
"query": {
"match_all": {}
}
}
这个查询返回索引中的所有文档。
2. 获取 Elasticsearch 根目录信息
GET /
获取 Elasticsearch 实例的根信息,包括版本、集群名称等。
分析文本
3. 使用 ik_smart
分析器进行文本分析
POST /_analyze
{
"text": "is a dd ",
"analyzer": "ik_smart"
}
这个命令使用 ik_smart
分析器对给定的文本进行分词。
4. 使用 ik_max_word
分析器进行复杂文本分析
POST /_analyze
{
"text": "熟练使用Redis 白嫖,可灵活运用其数据类型e芜湖可灵活运用其数据类白嫖奥里,并对数据类型,持久化机制等了解其一定的原理奥里给",
"analyzer": "ik_max_word"
}
这个命令使用 ik_max_word
分析器对复杂文本进行详细的分词。
5. 使用 ik_smart
分析器进行中文文本分析
POST /_analyze
{
"text": "北国风光,千里冰封,万里雪飘。望长城内外,惟余莽莽;大河上下,顿失滔滔。山舞银蛇,原驰蜡象,",
"analyzer": "ik_smart"
}
使用 ik_smart
分析器对中文文本进行分词。
6. 使用 ik_max_word
分析器进行混合文本分析
POST /_analyze
{
"text": "1111112222223333334444444就的啦是ui回复是哦胡i",
"analyzer": "ik_max_word"
}
使用 ik_max_word
分析器对混合文本进行详细的分词。
索引操作
7. 创建或更新索引 heima1
的映射
PUT /heima1
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"properties": {
"fistname": {
"type": "keyword"
},
"lastname": {
"type": "keyword"
}
}
}
}
}
}
创建或更新索引 heima1
的映射,定义字段 info
使用 ik_smart
分析器,email
不创建索引,name
包含 fistname
和 lastname
子字段。
8. 修改索引 heima
的映射
PUT /heima/_mapping
{
"properties": {
"name": {
"properties": {
"fistname": {
"type": "keyword"
},
"lastname": {
"type": "keyword"
}
}
}
}
}
修改索引 heima
的映射,添加或更新 name
字段的子字段 fistname
和 lastname
。
9. 获取索引 heima
的信息
GET /heima
获取索引 heima
的信息,包括映射、设置等。
10. 获取索引 heima1
的信息
GET /heima1
获取索引 heima1
的信息。
11. 删除索引 heima1
DELETE /heima1
删除索引 heima1
。
文档操作
12. 在索引 heima
中创建或更新文档
POST /heima/_doc/1
{
"info": "11111",
"email": "2222",
"name": {
"fistname": "3",
"lastname": "4"
}
}
在索引 heima
中创建或更新文档,ID 为 1,包含字段 info
、email
和 name
。
13. 获取索引 heima
中的文档
GET /heima/_doc/1
获取索引 heima
中 ID 为 1 的文档。
创建索引和查询
14. 创建 hotel
索引并定义映射
PUT /hotel
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address": {
"type": "keyword",
"index": false
},
"price": {
"type": "integer"
},
"score": {
"type": "integer"
},
"brand": {
"type": "keyword",
"copy_to": "all"
},
"city": {
"type": "keyword"
},
"starName": {
"type": "keyword"
},
"business": {
"type": "keyword",
"copy_to": "all"
},
"pic": {
"type": "keyword",
"index": false
},
"location": {
"type": "geo_point"
},
"all": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
创建 hotel
索引并定义字段映射。
15. 获取索引 hotel
的信息
GET /hotel
获取索引 hotel
的信息。
16. 获取索引 hotel
中 ID 为 61083
的文档
GET /hotel/_doc/61083
获取索引 hotel
中 ID 为 61083
的文档。
17. 搜索索引 hotel
中的文档(简单查询)
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
搜索索引 hotel
中的所有文档。
18. 搜索索引 hotel
中价格为 689 的文档
GET /hotel/_search
{
"query": {
"match": {
"price": "689"
}
}
}
搜索索引 hotel
中价格为 689 的文档。
19. 多字段查询
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩",
"fields": ["name", "all"]
}
}
}
在 name
和 all
字段中搜索包含 "外滩" 的文档。
20. 精确查询
GET /hotel/_search
{
"query": {
"term": {
"price": {
"value": "689"
}
}
}
}
精确查询价格为 689 的文档。
21. 范围查询
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 500
}
}
}
}
查询价格在 100 到 500 之间的文档。
22. 地理边界框查询
GET /hotel/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.1,
"lon": 121.5
},
"bottom_right": {
"lat": 30.9,
"lon": 121.7
}
}
}
}
}
查询位于指定地理边界框内的文档。
23. 地理距离查询
GET /hotel/_search
{
"query": {
"geo_distance": {
"distance": "15km",
"location": "31.21, 121.5"
}
}
}
查询距离指定点 15 公里内的文档。
24. 基于评分的搜索
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
],
"boost_mode": "sum"
}
}
}
``
`
对匹配 "外滩" 的文档进行评分,其中 `brand` 为 "如家" 的文档权重加 10。
### 25. 布尔查询
```bash
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"name": {
"value": "如家"
}
}
}
],
"should": [
{
"term": {
"brand": {
"value": "VALUE"
}
}
},
{
"term": {
"brand": {
"value": "VALUE"
}
}
}
],
"must_not": [
{
"match_all": {},
"match": {
"all": "外滩"
},
"multi_match": {
"query": "外滩",
"fields": ["all", "name"]
},
"geo_bounding_box": {
"location": {
"top_left": {
"lat": "",
"lon": ""
},
"bottom_right": {
"lat": "",
"lon": ""
}
}
},
"geo_distance": {
"distance": "10km",
"location": {
"lat": "121.1",
"lon": "12.1"
}
},
"range": {
"price": {
"gte": 10,
"lte": 20
}
},
"term": {
"brand": {
"value": "如家"
}
}
}
],
"filter": [
{
"term": {
"FIELD": "VALUE"
}
}
]
}
}
}
这个布尔查询包含了多个条件,必须匹配 name
为 "如家",可以匹配多个 brand
,但排除了多个条件,同时对 FIELD
为 VALUE
的文档进行过滤。
26. 结果排序
GET /hotel/_search
{
"query": {
"match": {
"all": "外滩"
}
},
"sort": [
{
"score": {
"order": "desc"
}
},
{
"price": {
"order": "asc"
}
}
]
}
根据 all
字段匹配 "外滩" 的文档进行搜索,结果按评分降序、价格升序排序。
27. 地理位置排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 40,
"lon": -70
},
"order": "asc",
"unit": "km"
}
}
]
}
查询所有文档,结果按离指定经纬度最近的顺序排序。
28. 分页和排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 40,
"lon": -70
},
"order": "asc",
"unit": "km"
}
}
],
"from": 0,
"size": 5
}
查询所有文档,结果按离指定经纬度最近的顺序排序,并只返回前 5 条结果。
29. 高亮显示
GET /hotel/_search
{
"query": {
"match": {
"all": "外滩"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}
查询包含 "外滩" 的文档,并对 name
字段进行高亮显示,即使 name
字段没有匹配查询条件。
30. 综合查询
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 5,
"sort": [
{
"price": {
"order": "desc"
}
}
],
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}
查询所有文档,结果按价格降序排序,只返回前 5 条,并对 name
字段进行高亮显示。