Java ‘Elasticsearch‘ 操作

本文介绍了如何在SpringBoot应用中使用spring-boot-starter-data-elasticsearch进行Elasticsearch的索引管理、添加、删除以及查询操作,包括分页和高亮显示功能的实现。
摘要由CSDN通过智能技术生成

依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.3.12.RELEASE</version>
</dependency>

配置

spring:
  elasticsearch:
    uris: http://192.168.0.226:9200

对象

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

添加索引操作

elasticsearchRestTemplate.indexOps(IndexCoordinates.of("indexname")).create();

IndexCoordinates可以给多个索引

删除索引

elasticsearchRestTemplate.indexOps(IndexCoordinates.of("indexname")).delete();

添加操作

IndexQuery indexQuery = new IndexQueryBuilder()
        .withId(productId)
                .withObject(product4ES)
                        .build();
elasticsearchRestTemplate.index(indexQuery, IndexCoordinates.of("products"));

查询操作

这个类是封装查询数据的

NativeSearchQuery query = new NativeSearchQueryBuilder()

要查询的字段

.withQuery(multiMatchQuery(keyword, "productName", "productSkuName"))

分页由于里面比尔帮你做好了就不要套用公式了

.withPageable(PageRequest.of(pageNum - 1, limit))

这里是高亮显示,用的是可变参数

.withHighlightFields(new HighlightBuilder.Field("productName").preTags("<label style='color: red'>").postTags("</label>"), new HighlightBuilder.Field("productSkuName").preTags("<label style='color: red'>").postTags("</label>"))

这里是构建

.build();

elasticsearchRestTemplate.search调用这个方法
search(封装的查询调教, 里面装的类型自动的, IndexCoordinates.of("要差的索引"))
SearchHits得到这个
search.getTotalHits();获取查询到的count

product4ES是我自己创建的然后装得到index

获取搜索的结果

List<SearchHit<Product4ES>> searchHits = search.getSearchHits();
创建一个等等用
List<Product4ES> list = new ArrayList<>();
for (SearchHit<Product4ES> hit : searchHits) {
获取返回的对象用什么对象装的什么对象拿自动的
    Product4ES product4ES = hit.getContent();
获取高亮map对象多个高亮map就有多个值
    Map<String, List<String>> highlightFields = hit.getHighlightFields();
获取叫下面这个名字的字段的高亮文本这里可能一段文字多个高亮
    List<String> productName = highlightFields.get("productName");
    if (!productName.isEmpty()) {
拿到第一个高亮,并设置
        product4ES.setProductName(productName.get(0));
    }
设置到对象
    list.add(product4ES);
}

计算总页数

int pageCount = ((int) (count % limit == 0 ? count / limit : count / limit + 1));
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一个开箱即用的框架,可以简化Java应用程序的开发。而Elasticsearch是一个开源的搜索引擎,具有强大的全文搜索和分析能力。 要在Spring Boot中操作Elasticsearch,首先需要在项目的依赖管理中增加Elasticsearch的相关依赖,例如elasticsearch和spring-boot-starter-data-elasticsearch。 在配置文件(application.properties或application.yml)中,需要设置Elasticsearch的连接信息,如主机名、端口号和索引名称等。可以通过spring.data.elasticsearch.cluster-nodes和spring.data.elasticsearch.cluster-name属性来进行配置。 接下来,在Java代码中,可以使用Spring Data Elasticsearch提供的API来进行操作。可以通过注解方式定义实体类和索引,使用ElasticsearchRepository来实现数据的增删改查操作。 通过ElasticsearchRepository的save方法可以将数据保存到Elasticsearch中。通过findById方法可以根据ID查询数据,通过search方法可以进行全文搜索等。 在使用Elasticsearch的时候,还可以进行索引的创建和删除操作。可以使用IndicesAdminClient提供的API来调用创建和删除索引的操作。 此外,Elasticsearch还提供了丰富的搜索功能,如分页查询、排序查询、聚合查询等。可以通过QueryBuilder和SearchRequestBuilder等类来构建复杂的查询语句。 总之,Spring Boot和Elasticsearch的结合可以提供一个简便而强大的搜索引擎应用程序开发框架。开发人员可以通过简单的配置和API调用来实现数据的存储、检索和分析功能,大大简化了开发过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeForWater

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值