ElasticSearch 创建索引、插入数据、修改数据、删除数据

创建索引

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )