创建索引
手动创建索引
1
2
3
4
5
6
7
8
9PUT /pai_search
{
"mappings": {
"properties": {
"age": { "type": "long" },
"name": { "type": "keyword" },
}
}
}Tip: 这里的
keyword
要注意,如果字段类型为keyword,那么这个字段在查询的时候不会被分词器解析添加索引字段
1
2
3
4
5
6
7
8
9
10
11PUT /pai_search/_mapping
{
"properties": {
"tag": {
"type": "text"
},
"score": {
"type": "text"
}
}
}查看索引
1
GET /my-index-000001/_mapping
查看索引指定字段
1
2## 这里没有复现出来,执行结果为空
GET /pai_search/_mapping/field/age
添加数据
新增一条数据
1
2
3
4
5
6
7PUT /pai_search/_doc/9
{
"age": 90,
"name":"xiaoxiao",
"score": 10,
"tag":["篮球","学习","跑步"]
}批量新增数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34POST /pai_search/_bulk
{
"index": {
"_id": "6"
}
}
{
"name": "Alice",
"age": 30,
"score": 85,
"tag": ["developer"]
}
{
"index": {
"_id": "7"
}
}
{
"name": "Bob",
"age": 25,
"score": 90,
"tag": ["designer"]
}
{
"index": {
"_id": "8"
}
}
{
"name": "Charlie",
"age": 35,
"score": 88,
"tag": ["manager"]
}
查询
官网api:https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html
查询所有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37GET /pai_search/_search
# 索引和文档信息
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9, # 查询结果总数
"relation": "eq"
},
"max_score": 1,
"hits": [ # 查询结果
{
"_index": "pai_search",
"_id": "1",
"_score": 1,
"_source": {
"age": 10,
"name": "xiaoxiao",
"score": 10, # 每个查询结果都有一个分数,表示结果匹配度,越大匹配度越高
"tag": [
"篮球",
"学习",
"跑步"
]
}
}
]
}
}根据id查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21GET /pai_search/_doc/1
# 索引和文档信息
{
"_index": "pai_search", # 索引
"_id": "1",
"_version": 1, # 修改的次数
"_seq_no": 0,
"_primary_term": 3,
"found": true,
"_source": {
"age": 10,
"name": "xiaoxiao",
"score": 10,
"tag": [
"篮球",
"学习",
"跑步"
]
}
}条件匹配查询、排序、过滤字段、分页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16GET /pai_search/_search
{
"query": {
"match": {"name": "xiaoxiao"},
},
"_source": ["age","name","tag"],
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 10,
}上面的
query
是用于匹配查询结果,_source
用于保留显示指定的字段,sort
用于指定字段排序,from
指从0开始,保留size
大小的个数多个条件must查询(and)、should查询(or)、must_not查询(not)、filter根据字段过滤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15GET /pai_search/_search
{
"query": {
"bool": {
"must": [
{"match":{"name":"xiaoxiao1"},}
],
"filter": {
"range":{
"age" : {"gt": 20,"lt": 28,}
}
},
},
},
}上面是查询
name = "xiaoxiao1" and age > 20 and age < 28
的数据; 这里的must可以替换为should、must_not等数组字段匹配查询
1
2
3
4
5
6
7
8
9
10GET /pai_search/_search
{
"query": {
"bool": {
"must": [
{"match":{"tag":"篮球3 跑步"},}
],
},
},
}3 25 xiaoxiao1 30 [“篮球3”,”学习”,”跑步”] 1 10 xiaoxiao 10 [“篮球”,”学习”,”跑步”] 9 90 xiaoxiao 10 [“篮球”,”学习”,”跑步”] 2 20 xiaoxiao1 20 [“篮球2”,”学习”,”跑步”] 4 25 xiaoxiao1 40 [“篮球4”,”学习”,”跑步”] 5 30 xiaoxiao1 50 [“篮球5”,”学习”,”跑步”] 这里的匹配结果会出现很多权重很低的数据,因为不是精确匹配所以包含篮球3、跑步这两个关键词的都出现了
字段精确匹配查询
- term : 直接精确查询
- match: 会使用分词器解析,再查询
1
2
3
4
5
6
7
8
9
10
11GET /pai_search/_search
{
"query": {
"bool": {
"should": [
{ "term": {"score" : "20"} },
{ "term": {"score" : "30"} },
]
}
},
}上面这个查询可以通过term精确查询分值为20或30的数据;
高亮查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60GET /pai_search/_search
{
"query": {
"bool": {
"must": [
{
"match":{
"name":"xiaoxiao1"
},
}
],
},
},
"highlight":{
"fields": {
"name": {
}
}
}
}
## 查询结果
{
"took": 26,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 0.7985077,
"hits": [
{
"_index": "pai_search",
"_id": "2",
"_score": 0.7985077,
"_source": {
"age": 20,
"name": "xiaoxiao1",
"score": 20,
"tag": [
"篮球2",
"学习",
"跑步"
]
},
"highlight": {
"name": [
"<em>xiaoxiao1</em>" ## 这里是高亮的结果
]
}
}
]
}
}上面这个查询语句时查询name为xiaoxiao1的数据,并把查询中的name进行高亮