创建索引
PUT demoindex
返回
1{ 2 "demoindex" : { 3 "settings" : { 4 "index" : { 5 "routing" : { 6 "allocation" : { 7 "include" : { 8 "_tier_preference" : "data_content" 9 } 10 } 11 }, 12 "number_of_shards" : "1", 13 "provided_name" : "demoindex", 14 "creation_date" : "1612333489325", 15 "number_of_replicas" : "1", 16 "uuid" : "0L-lMc6CQR2ft-jDlO670A", 17 "version" : { 18 "created" : "7100299" 19 } 20 } 21 } 22 } 23}
number_of_shards:分片数,创建后不能修改
number_of_replicas:副本数/备份数
创建索引,并指定分片参数
1PUT /demoindex/ 2{ 3 "settings":{ 4 "index":{ 5 "number_of_shards" : "5", 6 "number_of_replicas" : "1" 7 } 8 } 9}
返回
1{ 2 "acknowledged" : true, 3 "shards_acknowledged" : true, 4 "index" : "demoindex2" 5}
查看索引配置
GET demoindex2/_settings
返回
1{ 2 "demoindex2" : { 3 "settings" : { 4 "index" : { 5 "routing" : { 6 "allocation" : { 7 "include" : { 8 "_tier_preference" : "data_content" 9 } 10 } 11 }, 12 "number_of_shards" : "5", 13 "provided_name" : "demoindex2", 14 "creation_date" : "1612334114521", 15 "number_of_replicas" : "1", 16 "uuid" : "75BT_fx5Rhy3e0uf_zkayw", 17 "version" : { 18 "created" : "7100299" 19 } 20 } 21 } 22 } 23}
删除索引
DELETE demoindex2
插入数据
1PUT demoindex/_create/1 2{ 3 "userName": "", 4 "age": 22 5}
返回
1{ 2 "_index" : "demoindex", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 1, 6 "result" : "created", 7 "_shards" : { 8 "total" : 2, 9 "successful" : 1, 10 "failed" : 0 11 }, 12 "_seq_no" : 0, 13 "_primary_term" : 1 14}
更新数据
1PUT demoindex/_doc/1 2{ 3 "userName": "laowang", 4 "age": 22 5}
删除
DELETE demoindex/_doc/1
查询
GET demoindex/_doc/_search
GET demoindex/_doc/1
返回高亮代码的查询
1PUT demoindex/_doc/3 2{ 3 "userName": "laowang", 4 "age": 22, 5 "content": "中国人民解放军,美国美利坚合众国武装部队,远哥" 6} 7 8PUT demoindex/_doc/4 9{ 10 "userName": "laowang", 11 "age": 22, 12 "content": "中国人民解放军,美国美利坚合众国武装部队,远哥,中国人民站起来了" 13} 14 15GET demoindex/_doc/_search 16{ 17 "query" : { "match" : { "content" : "中国" }}, 18 "highlight" : { 19 "pre_tags" : ["<font color='red'>"], 20 "post_tags" : ["</font>"], 21 "fields" : { 22 "content" : {} 23 } 24 } 25}
返回结果
1{ 2 "took" : 2, 3 "timed_out" : false, 4 "_shards" : { 5 "total" : 1, 6 "successful" : 1, 7 "skipped" : 0, 8 "failed" : 0 9 }, 10 "hits" : { 11 "total" : { 12 "value" : 2, 13 "relation" : "eq" 14 }, 15 "max_score" : 0.23802689, 16 "hits" : [ 17 { 18 "_index" : "demoindex", 19 "_type" : "_doc", 20 "_id" : "4", 21 "_score" : 0.23802689, 22 "_source" : { 23 "userName" : "laowang", 24 "age" : 22, 25 "content" : "中国人民解放军,美国美利坚合众国武装部队,远哥,中国人民站起来了" 26 }, 27 "highlight" : { 28 "content" : [ 29 "<font color='red'>中国</font>人民解放军,美国美利坚合众国武装部队,远哥,<font color='red'>中国</font>人民站起来了" 30 ] 31 } 32 }, 33 { 34 "_index" : "demoindex", 35 "_type" : "_doc", 36 "_id" : "3", 37 "_score" : 0.1976162, 38 "_source" : { 39 "userName" : "laowang", 40 "age" : 22, 41 "content" : "中国人民解放军,美国美利坚合众国武装部队,远哥" 42 }, 43 "highlight" : { 44 "content" : [ 45 "<font color='red'>中国</font>人民解放军,美国美利坚合众国武装部队,远哥" 46 ] 47 } 48 } 49 ] 50 } 51}
分页查询、过滤返回的字段属性
1GET demoindex/_doc/_search 2{ 3 "query" : { "match" : { "content" : "中国" }}, 4 "sort": [{ 5 "updateTime": "asc" 6 }], 7 "highlight" : { 8 "pre_tags" : ["<font color='red'>"], 9 "post_tags" : ["</font>"], 10 "fields" : { 11 "content" : {} 12 } 13 }, 14 "_source": { 15 "includes": [ "title","author","docType","fileDir","filePath","fileSHA1" ], 16 "excludes": [ "content" ] 17 }, 18 "size": 10, 19 "from": 0 20}
安装ik分词插件 elasticsearch-analysis-ik
1、解压 elasticsearch-analysis-ik-7.10.2.zip
2、拷贝解压出来的所有文件放到 elasticsearch-7.10.2\plugins\ik 目录下(首次需要创建一个 ik 目录)
3、重启启动 elasticsearch
创建Ik分词的mapping
1POST demoindex/_mapping 2{ 3 "properties":{ 4 "content":{ 5 "type":"text", 6 "analyzer":"ik_max_word", 7 "search_analyzer":"ik_smart" 8 } 9 } 10} 11 12PUT demoindex 13{ 14 "mappings":{ 15 "properties":{ 16 "userName":{ 17 "type":"keyword" 18 }, 19 "age":{ 20 "type":"long" 21 }, 22 "content": { 23 "type": "text", 24 "analyzer": "ik_max_word", 25 "search_analyzer": "ik_smart" 26 } 27 } 28 } 29}
分词粒度分为:
ik_smart 和 ik_max_word
数据类型说明:
字符串类型
text 、 keyword
数值类型
long, integer, short, byte, double, float, half_float, scaled_float
日期类型
date
te布尔值类型
boolean
二进制类型
binary
范围类型
integer_range , float_range, long_range, double_range, date_range