1. 服务相关

1.1. analyzer 分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//指定索引分词
GET users/_analyze
{
"analyzer": "analyzer1", //官方的分词器或自建在索引的分词器
"text": ["i am :)"]
}

//自定义分词
GET users/_analyze
{
"text": [" <br>今天很 :) <br>"],
"explain": false, //是否分析
"char_filter": [
{
"type":"html_strip" //处理html
},{
"type":"mapping",
"mappings":["- => _",":) => happy"] //处理自定义映射
}
],
"tokenizer": "standard", //分词器
"filter":["lowercase","stop"] //过滤器
}

2. 索引相关

2.1. 创建索引

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
60
61
62
PUT users
{
"aliase":"persons",
"mappings": {
"dynamic":"true", //动态索引 "true"是,"false"否,"strict"报错
"properties": {
"name":{
"type": "text", //字段类型 keyword,text
"index": true, //是否索引,不索引查询会报错
"analyzer": "standard", //分词器
"index_options": "docs", //倒排索引记录的内容 docs,freqs,positions,offsets
"null_value": "null", //空值设置,方便搜索,source中不变
"copy_to": "full_name", //将name 拷贝到fullName中,其他的也可拷贝至fullName
"fields":[ //子字段
"sub1":{
"type":"keyword"
}
]
}
}
},
"settings": {
"number_of_shard":2,
"number_of_repliced":1
"analysis": {
"analyzer": {
"myenglish":{ //自定义过滤器
"type":"english",
"stem_exclusion":["orginazation","organizations"],
"stopwords":["a","an","and","are","as","at","the","..."] //停止符
},
"analyzer1":{ //自定义过滤器方式2
"type":"custom",
"char_filter":["char_filter_2"],//选择字符过滤器
"tokenizer":"tokenizer1", //选择分词器
"filter":["lowercase","english_stop"] //选择过滤器
}
},
"char_filter": { //自定义字符过滤器
"char_filter_1":{ //过滤器名
"type":"html_strip" //类型
},
"char_filter_2":{
"type":"mapping",
"mappings":["- => _",":) => happy"] //不同type字段不同
}
},
"tokenizer": { //自定义分词器
"tokenizer1":{ //分词器名
"type":"pattern",
"pattren":"[ .,!?]"
}
},
"filter": {
"filter1":{
"type":"stop",
"stopwords":"_english_"
}
}
}
}
}

3. 文档修改语法

3.1. Index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#INDEX方式创建文档,已有则更新
PUT users/_doc/1
{
"id":1,
"name":"小明",
"sex":"男"
}
# 创建文档,可以不指定文档ID,不指定则自动生成
# 若已有文档,则会全量更新文档
POST users/_doc/1
{
"id":1,
"name":"小明",
"sex":"男"
}

3.2. Create

1
2
3
4
5
6
7
8
9
# 创建文档,已有则报错
PUT users/_create/1
{
"id":1,
"name":"小明",
"sex":"男"
}


3.3. 更新文档

1
2
3
4
5
6
7
8
9
10
# 局部更新文档,若更新的字段没有发生变化则版本不变
POST users/_update/1
{
"doc":{
"id":1,
"name":"小明",
"sex":"男",
"age":18
}
}

3.4. 删除文档

1
2
3
# 删除文档 删除文档后版本号会+1
DELETE users/_doc/1

4. 批量操作Bulk API

bulk api比较特殊,两行为一组,第一行为action及指定的id,文档信息等,第二行为数据内容

1
2
3
4
5
第一行,以action开始,必须是一下之一:
create:如果文档不存在,那么创建它。存在的话会报错但不影响其它操作
index:创建一个新文档或者替换一个现有的文档
update:部分更新一个文档
delete:删除一个文档
1
2
3
4
5
6
7
8
9
//批量操作所有索引
POST _bulk
{"index":{"_index":"users","_type":"_doc","_id":4}}
{"id":4,"name":"小明","age":18}
{"create":{"_index":"users"}}
{"id":5,"name":"name5","gender":"男"}
{"update":{"_index":"users","_doc":7,"_id":7}
{"doc":{"id":7,"name":"haha"}}

5. 查询语法

5.1. 简单查询语法

1
GET users/_doc/1

5.2. mget批量查询

1
2
3
4
5
6
7
8
9
10
11
12
13
GET _mget
{
"docs": [ //docs必须有
{
"_index": "users",
"_id": "7"
},
{
"_index":"kibana_sample_data_ecommerce",
"_id":"giuCdIABg_nl5G6RGcTJ"
}
]
}

5.3. 搜索语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
GET users/_search?q=name:小明
GET users/_search?q=小明&df=name&from=1&size=10
GET users/_search?q=name:xiao ming //后者会在全部索引查
//分组概念
GET users/_search?q=name:(xiao ming) //或
GET users/_search?q=name:"xiao ming" //且
GET users/_search?q=name:(xiao AND ming)
GET users/_search?q=name:(xiao OR ming)
GET users/_search?q=name:(xiao - ming) //包含前不包含后
GET users/_search?q=name:(xiao*)模糊匹配
GET users/_search?q=name:builtfy~1 //纠错
GET users/_search?q=name:"xiao Lang"~2 //匹配分离


5.3.2. request body查询

外层参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
GET kibana_sample_data_ecommerce/_search
{
"explain": true,
"profile": true, //底层执行参数
"_source": ["order_id","products._id"], //返回字段
"fields": ["category"],
"script_fields": {}, //执行脚本
"query": {"match_all": {}}, //查询条件
"sort": [], //排序
"from": 0,
"size": 20,
"aggs": {}, //分组
"highlight": {}
}
1
2
3
4
5
6
7
8
9
10
11
12
//
//GET _search 不带索引查全部索引
//GET index1,users/_search 指定多个索引搜索
//GET index*/_search 通配符匹配搜索
GET users/_search
{
"query": {
"match": {
"name": "小明"
}
}
}
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
GET /index1/_search
{
"query":{
"match":{
"userName":"黄"
"userName":{
"query":"黄 燕 梅", //指定匹配的值
"operator":"and"//连接符号
"minimum_should_match":"68%", //匹配度
"boost":3 //评分比重
}
},
"match_phrase": { //短语匹配 也可以写成type为phrase的match查询
"customer_full_name": "Eddie",
"customer_full_name": {
"query": "Eddie HH",
"slop": 1 //中间可以插入指定数量的单词
}
},
"multi_match": {
"query": "刘 员工",
"type": "cross_fields", //best_fields(最佳匹配,底层为dis_max,默认),"most_fields "(多数字段,评分相加), cross_fields(跨字段)
"operator":"and", // multi_match 查询中避免使用 not_analyzed 字段,因为全匹配的查询是查询不到结果的
"tie_breaker": 0.3,
"fields": [
"name^2","desc","address.city" //^2为权重boost
]
},
"query_string": { //字符串关键字分词
"default_field": "products.product_name", //默认查找字段,和fields二选一
"fields": ["customer_last_name","customer_full_name"],
"query": "Sweatshirt AND (grey OR multicolor) ", //可指定AND 或者 OR 默认or
"default_operator": "OR"
},
"simple_query_string": { //
"query": "", //字符连接被禁止
"fields": [],
"default_operator": "OR"
},
"nested": {
"path": "address",
"query": {
"match": {
"address.city": "成都"
}
}
},
"term":{
"userName":"黄燕梅"
},
"dis_max":{ //分离最大化查询
"queries":[
{ "match": { "title": "Brown fox" } }, // (将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回)
{ "match": { "body": "Brown fox" } } // (也就是不求平均分,只返回最大评分作为评分结果)
}],
"tie_breaker": 0.3 //tie_breaker 参数在返回最大评分的基础上将其余的次要匹配语句乘以此系数再相加
},
"bool":{
"should":[
{
"match":{
"userName":"黄"
}
},{
"match":{
"userName":"燕"
}
},{
"match":{
"userName":"梅"
}
},{
"nested": {
"path": "address",
"query": {
"match": {
"address.city": "成都"
}
}
}
}
],
"minimum_should_match": 2 //匹配度
"must":[
{
"match":{
"userName":"黄"
}
},{
"match":{
"userName":"燕"
}
}
],
"filter":[
{
"term":{ //全匹配查询
age:{
"value":25
},
},
"range":{ //范围
"price":{
"from":0,
"to":"50000"
}
}
}
]
},
"match_phrase":{ //位置匹配(按照位置次序匹配, position)
"userName":"黄 燕 梅"
},
"prefix":{ //前缀匹配,(keyword类型,从第一个开始,区分大小写;text 类型,从分词前缀开始,不区分大小写)
"en_name":{
"value":"hu"
}
},
"wildcard":{ //通配符匹配 ?* 效率低下
"en_name":{
"value":"h*ng"
}
},
"match_phrase_prefix":{ //推荐搜索,返回推荐相关性
"en_name":{
"query":"java s ",
"slop":10, //移动次数(s可移动次数)
"max_expansions":10 //
}
},
"fuzzy":{ //模糊搜索,可容错
"en_name":{
"value":"huaa",
"fuziness":2 //错误字母数量
}
}
},
"sort":{
"saleCount":{
"order":"asc"
}
},
"aggs":{ //分组
"brang_aggs":{ //聚合名
"terms":{
"field":"brandId",
"size":50,
"min_doc_count":1
},
"aggs":{
"brand_name_agg":{
"terms":{
"field":"brandName"
}
}
}
},
"attr_aggs":{
"nestd":{
"path":"attrs"
},
"aggs":{
"attr_name_agg":{
"terms":{
"field":"attrs.attrId"
},
"aggs":{
"attr_name_aggs":{
"terms":{
"field":"attr.attrName"
}
},
"attr_value_agg":{
"terms":{
"field":"attr.attrValue"
}
}
}
}
}
}
},

"highlight":{
"fields":{
"userName":{}
}
}
}

5.3.3. 批量搜索

1
2
3
4
5
POST _msearch
{"index":"users"}
{"query":{"match_all":{}}}
{"index":"kibana_sample_data_ecommerce"}
{"query":{"match_all":{}}}

5.3.3.1. bulk

bulk批量操作

5.3.3.2. 无mapping语法

1
PUT /index1/

创建索引的时候可带mapping、setting、aliases参数

1
PUT /index2/

body

参数解释

字段 说明 说明 备注
aliases 别名
mappings 索引映射
..dynamic 动态索引(新增字段时的处理) true false strict
…… true 默认值,表示允许选自动新增字段
…… false 不允许自动新增字段,但是文档可以正常写入,无法对字段进行查询等操作
…… strict 严格模式,文档不能写入,报错
..properties 动态索引(新增字段时的处理) true false strict
….userId 存的字段
….type 类型 keyword text
…… 文本型 keyword 关键字,不分词,可聚合、排序
…… 文本型 text 文本,可分词,不能聚合、排序
…… 数值型 long\integer\float\double\byte\short
…… 时间型 date
…… 布尔型 boolean
….index 是否索引 true false
….store 是否存储 true false
….analyzer 分词器 analyzer 、 standard analyzer
….search_analyzer 分词器 用来指定搜索阶段时使用的分词器,默认使用analyzer的设置
….search_quote_analyzer 分词器 搜索遇到短语时使用的分词器,默认使用search_analyzer的设置
….fields 子字段 用来做子检索的,例如userId为keyword类型,不支持聚合检索,fields中增加 userId1 通过userId.userId1 则可聚合
settings 索引设置

5.3.3.3. 创建文档

创建文档

1
2
3
4
5
6
7
8
PUT /index1/_doc/id1

body
{
"userId":"33133190",
"userName":"黄燕梅",
"age":38
}

5.3.3.4. 查询文档

查询文档

GET /index1/_doc/id1

5.3.3.5. 修改文档

修改文档

5.3.3.6. put方式创建

put方式创建

_create方式若有数据则不创建

1
2
3
4
5
6
7
PUT /index1/_doc/id1/_create
body
{
"userId":"33133190",
"userName":"黄燕梅",
"age":38
}

参数解释

字段 说明 说明 备注
query 查询参数
..match 分词查询
..term 全匹配查询 对于分词的数据查不出来
mappings 索引映射

5.3.3.7. 近似匹配

近似匹配

1
2
3
4
5
6
7
8
GET /index1/_search
body
{
"query":{
"prefix":{
}
}
}

5.3.3.8. 搜索分析

搜索分析

1
2
3
4
5
6
GET /_analyze
body
{
"text":"黄燕梅",
"analyze":"standard"
}

5.3.3.9. 解释query

解释query

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
GET /my_index/my_type/_validate/query?explain
{
"query": {
"bool": {
"should": [
{ "match": { "title": "Foxes"}},
{ "match": { "english_title": "Foxes"}}
]
}
}
}

GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "Poland Street W1V",
"type": "most_fields",
"fields": [ "street", "city", "country", "postcode" ]
}
}
}

5.3.3.10. 重建索引

重建索引

  • 新建索引并配置好mapping信息
1
2
3
4
5
6
7
8
9
10
  PUT /index1new

body

{

"mappings":{...}

}

  • 迁移索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  POST /_reindex

body

{

"source":{

"index":"index1"

},

"dest":{

"index":"index1new"

}

}
  • 删除原索引

DELETE /index1

  • 别名新索引 d

PUT /index1new/_alias/index1

5.3.3.11. 搜索

搜索

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

GET /index1/_search

{
"query":{
"match":{
"userName":"黄"
"userName":{
"query":"黄 燕 梅", //指定匹配的值
"operator":"and"//连接符号
"minimum_should_match":"68%", //-匹配度
"boost":3 //评分比重
}
},
"nested": {
"path": "address",
"query": {
"match": {
"address.city": "成都"
}
}
},
"term":{
"userName":"黄燕梅"
},
"multi_match": {
"query": "刘 员工",
"type": "cross_fields", // best_fields(最佳匹配,底层为dis_max,默认),most_fields(多数字段,评分相加),cross_fields(跨字段)
"operator":"and",// multi_match 查询中避免使用 not_analyzed 字段,因为全匹配的查询是查询不到结果的
"tie_breaker": 0.3,
"fields": [
"name^2","desc","address.city" //^2为权重boost
]

},

"dis_max":{ // 分离最大化查询
"queries":[
{ "match": { "title": "Brown fox" } }, // (将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回)
{ "match": { "body": "Brown fox" } } //(也就是不求平均分,只返回最大评分作为评分结果)
}],
"tie_breaker": 0.3 // tie_breaker 参数在返回最大评分的基础上将其余的次要匹配语句乘以此系数再相加
},
"bool":{
"should":[
{
"match":{
"userName":"黄"
}
},{
"match":{
"userName":"燕"
}
},{
"match":{
"userName":"梅"
}
},{
"nested": {
"path": "address",
"query": {
"match": {
"address.city": "成都"
}
}
}
}
],
"minimum_should_match": 2 //匹配度
"must":[
{
"match":{
"userName":"黄"
}
},{
"match":{
"userName":"燕"
}
}
],
"filter":[
{
"term":{ //全匹配查询
age:{
"value":25
},
},
"range":{ //范围
"price":{
"from":0,
"to":"50000"
}
}
}
]
},

"match_phrase":{ //位置匹配(按照位置次序匹配, position)
"userName":"黄 燕 梅"
},
"prefix":{ //前缀匹配,(keyword类型,从第一个开始,区分大小写;text 类型,从分词前缀开始,不区分大小写)
"en_name":{
"value":"hu"
}
},
"wildcard":{ //通配符匹配 ?* 效率低下
"en_name":{
"value":"h*ng"
}
},
"match_phrase_prefix":{ //推荐搜索,返回推荐相关性
"en_name":{
"query":"java s ",
"slop":10, //移动次数(s可移动次数)
"max_expansions":10
}
},
"fuzzy":{ //模糊搜索,可容错
"en_name":{
"value":"huaa",
"fuziness":2 //错误字母数量
}
}
},
"sort":{
"saleCount":{
"order":"asc"
}
},
"aggs":{ //分组
"brang_aggs":{ //聚合名
"terms":{
"field":"brandId",
"size":50,
"min_doc_count":1
},
"aggs":{
"brand_name_agg":{
"terms":{
"field":"brandName"
}
}
}
},
"attr_aggs":{
"nestd":{
"path":"attrs"
},
"aggs":{
"attr_name_agg":{
"terms":{
"field":"attrs.attrId"
},
"aggs":{
"attr_name_aggs":{
"terms":{
"field":"attr.attrName"
}
},
"attr_value_agg":{
"terms":{
"field":"attr.attrValue"
}
}
}
}
}
}
},
"highlight":{
"fields":{
"userName":{}
}
}
}