天天看点

springboot 集成Elasticsearch使用ElasticSearchTemplate进行增删改查操作

类比mysql

增删改查都是用的json

elastic

mysql

index

database

type

table

document

row

file

column

mapping

schema

everything

PUT index/type

insert

DELETE index/type

delete

update

GET index/type

select

每个index有10个shard,对应5个primary shade,5个replicate shard

ElasticSearchTemplate的使用

ElasticSearchTemplate更多是对ESRepository的补充,里面提供了一些更底层的方法。

springboot 集成Elasticsearch使用ElasticSearchTemplate进行增删改查操作

 这里主要是一些查询相关的,同样是构建各种SearchQuery条件。

也可以完成add操作

add主要是通过index方法来完成,需要构建一个IndexQuery对象

springboot 集成Elasticsearch使用ElasticSearchTemplate进行增删改查操作

 构建这个对象,主要是设置一下id,就是你的对象的id,Object就是对象本身,indexName和type就是在你的对象javaBean上声明的

springboot 集成Elasticsearch使用ElasticSearchTemplate进行增删改查操作

 其他的字段自行发掘含义,构建完IndexQuery后就可以通过Template的index方法插入了。

template里还有各种deleteIndex,delete,update等方法,用到的时候就查查看吧。

下面讲一个批量插入的方法,我们经常需要往ElasticSearch中插入大量的测试数据来完成测试搜索,一条一条插肯定是不行的,ES提供了批量插入数据的功能——bulk。

前面讲过JPA的save方法也可以save(List)批量插值,但适用于小数据量,要完成超大数据的插入就要用ES自带的bulk了,可以迅速插入百万级的数据。

和index插入单条数据一样,这里需要的是List仅此而已,是不是很简单。

这里是创建了100万个对象,每到500就用bulkIndex插入一次,速度飞快,以秒的速度插入了百万数据。

OK,这篇主要是讲一些ElasticSearchRepository和ElasticSearchTemplate的用法,构造QueryBuilder的方式

``

转载:https://blog.csdn.net/tianyaleixiaowu/article/details/76149547