记录一下,elasticsearch从创建索引到插入数据的一个crud操作。
一、创建索引
curl -XPUT "http://192.168.99.1:9200/productindex" -d' { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "product" : { "properties": { "name" : { "type": "text", "fielddata": true }, "price" : { "type": "long" } } } } }'
二、插入数据
1、如果需要插入的数据已经存在,那么执行替换操作,如果不存在则执行插入操作
注意: 1、此处是手动指定的id的值
2、如果productindex/product/1 这个数据存在,如果请求体中只有name字段,那么这行数据的price字段就会被删除,即执行的是替换操作,而不是更新操作
curl -XPUT "http://192.168.99.1:9200/productindex/product/1" -d' { "name" : "pen", "price" : 2 }'
2、如果数据已经存在,那么就报错,不存在则执行插入操作
注意:此处只需要加上_create或加上op_type=create即可
curl -XPUT "http://192.168.99.1:9200/productindex/product/1?op_type=create" -d' { "name" : "new pen", "price" : 2 }'
或
curl -XPUT "http://192.168.99.1:9200/productindex/product/1/_create" -d' { "name" : "new pen", "price" : 2 }'
3、插入数据的时候自动生成id的值
注意:此处就需要使用post请求,而不是使用put请求。
curl -XPOST "http://192.168.99.1:9200/productindex/product" -d' { "name" : "pen", "price" : 2 }'
三、修改数据
1、使用_update或op_type=update指定修改,数据不存在报错
curl -XPOST "http://192.168.99.1:9200/productindex/product/1/_update" -d' { "doc": { "name" : "update new name" } }'
2、使用乐观锁version控制修改(防止在并发情况下数据修改有误)
后方的这个version的值为数据当前的版本号,如果es中这个数据的版本号发生了变化,则修改失败。
curl -XPOST "http://192.168.99.1:9200/productindex/product/1/_update?version=7" -d' { "doc": { "name" : "update new name" } }'
3、使用upsert操作,如果数据不存在则执行upsert部分(插入操作),否则执行更新操作
curl -XPOST "http://192.168.99.1:9200/productindex/product/12/_update" -d' { "doc":{ "name":"update new value" }, "upsert" : { "name" : "数据不存在执行插入操作", "price" : 1 } }'
或执行如下操作 (使用doc_as_upsert,如果文档不存在,则将doc的部分当做upsert的部分)
curl -XPOST "http://192.168.99.1:9200/productindex/product/13/_update" -d' { "doc":{ "name":"update new value" }, "doc_as_upsert" : true }'
四、高亮查询
1、插入一条新的数据
curl -XPUT "http://192.168.99.1:9200/productindex/product/29" -d' { "name" : "new name", "desc" : "this is a desc field" }'
2、匹配name字段中有has name的值或desc字段中有desc的值,并对匹配到的值进行高亮
curl -XGET "http://192.168.99.1:9200/productindex/product/_search" -d' { "query": { "bool": { "should": [ { "match": { "name": "has name" } }, { "term": { "desc": { "value": "desc" } } } ] } }, "highlight": { "pre_tags": "<span style=\"color:red\">", "post_tags": "</span>", "fields": { "name" : { "pre_tags": "<span style=\"color:blue\">", "post_tags": "</span>" }, "*" : { } } } }'
五、删除数据
1、删除id=1的数据
curl -XDELETE "http://192.168.99.1:9200/productindex/product/1"
相关推荐
es入门操作-elasticsearch入门操作
ElasticSearch入门到精通
(狂神)ElasticSearch快速入门笔记,ElasticSearch基本操作以及爬虫(Java-ES仿京东实战),包含了小狂神讲的东西,特别适合新手学习,笔记保存下来可以多看看。好记性不如烂笔头哦~,ElasticSearch,简称es,es是一个...
Elasticsearch入门教程
主要介绍了SpringBoot整合Elasticsearch并实现CRUD操作,需要的朋友可以参考下
Elaticsearch,简称为es, es是一个开源的高扩展的分布式...es也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
简化elasticsearch API的使用方式 使用示例 ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); ESBasicService service = context.getBean(ESBasicService.class);...
全套 elasticsearch从入门到精通到运维 基于ES5.6版本 有视频 文档 快速上手
Elasticsearch入门(五):Elasticsearch基础概念与基本操作 文章中所用用到的 http 文件,包括 es.http和es_book.http
ElasticSearch的一个示例程序,鸣谢:http://donlianli.iteye.com/blog/1902238。这个MyEclipse小项目是在上述资源的基础上进行了...ElasticSearch本身的安装就没说了:下载,解压,运行bin目录下的bat文件即可运行。
基于elasticsearch的封装。简化CRUD操作。索引托管。支持所有分词器。实现es分布式锁.zip
ElasticSearch CRUD 在这个项目中,我已经使用Java API完成了ElasticSearch CRUD(创建读取更新和删除)操作。 这个项目对初学者学习非常有帮助。先决条件安装 ,然后启动elasticSearch。跑步将该项目作为Maven导入...
Elasticsearch 6.1官方入门教程.rar
本实例涵盖ES中的各类操作,如索引操作、CRUD操作、批处理、结果排序、分页查询、检索查询、关键字查询、高亮显示、逻辑查询、过滤查询、分组查询等等。并且已经过生产环境验证,各位可放心使用。
Elasticsearch入门篇(一、基本概念)1
Elasticsearch入门视频教程下载
最完整的Elasticsearch 基础教程,欢迎大家一起探讨学习!
elasticSearch的操作demo:里面只有java类,pom配置,application.xml配置自己找
《elasticSearch入门指南》 1、es是什么?基本概念 2、安装、集群探索 3、操作数据 4、2.0主要变化
(1)\1.课件;目录中文件数:2个 ├─2-课程内容讲解.bmpr (2)\2....目录中文件数:6个 ├─elasticsearch-7.8.0-linux-x86_64.tar.gz ...├─004 - Elasticsearch - 入门 - 环境准备.mp4 ├─005 - Elasticsearch