ElasticSearch总结

本文主要介绍了在SpringBoot项目中使用Java操作Elasticsearch的CRUD操作,包括通过继承`ElasticsearchRepository`进行操作和利用`ElasticsearchTemplate`进行详细操作。特别提到了当ID类型为字符串时的配置方法。
摘要由CSDN通过智能技术生成

这段时间一直在用ES,主要对在SpringBoot中Java对ES的CRUD做下总结,分为两种方式

  1. 继承ElasticsearchRepository类

​​​​​​采用这种方式​具体创作实体类和大部分CRUD可以参考下面这篇文章,讲的比较详细,本文主要讲一下另一种方式。

https://blog.csdn.net/chen_2890/article/details/83895646

有个需要注意的地方是很多情况下ID不一定是Long型,可能为字符串,如果想把ID设成String,需要在继承类的泛型中对应的泛型传String,

public interface ItemRepository extends ElasticsearchRepository<Item, String> {
   

}

    2.使用ElasticSearchTemplate

import com.example.springbootmybatisdemo.pojo.ESpojo.Item;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.UpdateByQueryAction;
import org.elasticsearch.index.reindex.UpdateByQueryRequestBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.cor
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值