栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch[03]SpringData集成ElasticSearch

ElasticSearch[03]SpringData集成ElasticSearch

参考视频

【尚硅谷】ElasticSearch教程入门到精通(基于ELK技术栈elasticsearch 7.8.x版本).

【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂 P13.

【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂 P14.

目录

环境准备

SpringData-ElasticSearch测试

  创建索引

  删除索引

  文档新增

  文档修改

  根据 id 查询文档

  文档批量新增

  文档查询所有

  根据 id 删除文档

  文档排序分页查询

  文档多条件查询(条件+高亮+排序+分页)

环境准备
软件版本
ElasticSearch7.8.1
IDEA2021.2
ElasticSearch Head0.1.5

创建普通sping项目

pom.xml

添加依赖

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.projectlombok
            lombok
            1.18.22
        
        
            com.alibaba
            fastjson
            1.2.75
        
        
            junit
            junit
            4.13.1
            compile
        
        
            org.springframework.data
            spring-data-elasticsearch
            4.0.9.RELEASE
            compile
        
        
            commons-httpclient
            commons-httpclient
            3.1
        
        
            org.elasticsearch.client
            elasticsearch-rest-client
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
        
    
application.properties
#es hostname
elasticsearch.hostname=127.0.0.1
#es port
elasticsearch.port=9200
#es scheme
elasticsearch.scheme=http
#es indexName
elasticsearch.ES_INDEX=shopping_index
ElasticSearchClientConfig.java

编写ElasticSearchClient配置类

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
@Component
public class ElasticSearchClientConfig {

    private String hostname;
    private Integer port;
    private String scheme;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(hostname, port, scheme))
        );
        return client;
    }
}

Product.java

编写pojo实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@document(indexName="shopping_index" , shards = 3, replicas = 0)
public class Product {
    //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
    @Id
    private Long id;//商品唯一标识
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;//商品名称
    @Field(type = FieldType.Keyword)
    private String category;//分类名称
    @Field(type = FieldType.Double)
    private Double price;//商品价格
    @Field(type = FieldType.Keyword, index = false)
    private String images;//图片地址
}

ProductDao.java

编写pojo对应的dao,继承ElasticsearchRepository

package com.elasticsearch.springdata.dao;

import com.elasticsearch.springdata.pojo.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductDao extends ElasticsearchRepository {

}
SpringDataESIndexTest.java(ElasticsearchRestTemplate)

测试ElasticsearchRestTemplate

这里测试索引相关操作

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {
    //注入 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Value("${elasticsearch.ES_INDEX}")
    private String ES_INDEX;

}
SpringDataESDocTest.java(ElasticsearchRepository)

测试dao

这里测试简单的文档操作

import com.elasticsearch.springdata.dao.ProductDao;
import com.elasticsearch.springdata.pojo.Product;
import org.junit.jupiter.api.Test;
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 java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class SpringDataESDocTest {

    @Autowired
    private ProductDao productDao;
    
}
SpringDataESDocPageTest.java(RestHighLevelClient)

测试RestHighLevelClient

这里测试文档的多条件查询操作

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.core.Timevalue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class SpringDataESDocPageTest {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Value("${elasticsearch.ES_INDEX}")
    private String ES_INDEX;
    
}
SpringData-ElasticSearch测试 SpringDataESIndexTest.java 创建索引
    ///创建索引并增加映射配置
    @Test
    public void createIndex() {
        //创建索引,系统初始化会自动创建索引
        System.out.println("创建索引");
    }
创建索引

删除索引
    //删除索引
    @Test
    public void deleteIndex() {
        boolean flg = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(ES_INDEX)).delete();
        System.out.println("删除索引 = " + flg);
    }
删除索引 = true
SpringDataESDocTest.java 文档新增
    //文档新增
    @Test
    public void save() {
        Product product = new Product();
        product.setId(1L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.0);
        product.setImages("http://www.atguigu/hw.jpg");

        productDao.save(product);
    }

文档修改
    //文档修改
    @Test
    public void update() {
        Product product = new Product();
        product.setId(1L);
        product.setTitle("小米 2 手机");
        product.setCategory("手机");
        product.setPrice(9999.0);
        product.setImages("http://www.atguigu/xm.jpg");
        productDao.save(product);
    }

根据 id 查询文档
    //根据 id 查询
    @Test
    public void findById() {
        Product product = productDao.findById(1L).get();
        System.out.println(product);
    }
Product(id=1, title=小米 2 手机, category=手机, price=9999.0, images=http://www.atguigu/xm.jpg)
文档批量新增
    //批量新增
    @Test
    public void saveAll() {
        List productList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Product product = new Product();
            product.setId(Long.valueOf(i));
            product.setTitle("[" + i + "]小米手机");
            product.setCategory("手机");
            product.setPrice(1999.0 + i);
            product.setImages("http://www.atguigu/xm.jpg");
            productList.add(product);
        }
        productDao.saveAll(productList);
    }

文档查询所有
    //查询所有
    @Test
    public void findAll() {
        Iterable products = productDao.findAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }
Product(id=5, title=[5]小米手机, category=手机, price=2004.0, images=http://www.atguigu/xm.jpg)
Product(id=7, title=[7]小米手机, category=手机, price=2006.0, images=http://www.atguigu/xm.jpg)
Product(id=0, title=[0]小米手机, category=手机, price=1999.0, images=http://www.atguigu/xm.jpg)
Product(id=2, title=[2]小米手机, category=手机, price=2001.0, images=http://www.atguigu/xm.jpg)
Product(id=3, title=[3]小米手机, category=手机, price=2002.0, images=http://www.atguigu/xm.jpg)
Product(id=4, title=[4]小米手机, category=手机, price=2003.0, images=http://www.atguigu/xm.jpg)
Product(id=1, title=[1]小米手机, category=手机, price=2000.0, images=http://www.atguigu/xm.jpg)
Product(id=6, title=[6]小米手机, category=手机, price=2005.0, images=http://www.atguigu/xm.jpg)
Product(id=8, title=[8]小米手机, category=手机, price=2007.0, images=http://www.atguigu/xm.jpg)
Product(id=9, title=[9]小米手机, category=手机, price=2008.0, images=http://www.atguigu/xm.jpg)
根据 id 删除文档
    //根据 id 删除文档
    @Test
    public void delete() {
        Product product = new Product();
        product.setId(1L);
        productDao.delete(product);
    }

文档排序分页查询
    //文档排序分页查询
    @Test
    public void findByPageable() {
        //设置排序(排序方式,正序还是倒序,排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        int currentPage = 0;//当前页,第一页从 0 开始,1 表示第二页
        int pageSize = 5;//每页显示多少条
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);
        //分页查询
        Page productPage = productDao.findAll(pageRequest);
        for (Product Product : productPage.getContent()) {
            System.out.println(Product);
        }
    }
Product(id=9, title=[9]小米手机, category=手机, price=2008.0, images=http://www.atguigu/xm.jpg)
Product(id=8, title=[8]小米手机, category=手机, price=2007.0, images=http://www.atguigu/xm.jpg)
Product(id=7, title=[7]小米手机, category=手机, price=2006.0, images=http://www.atguigu/xm.jpg)
Product(id=6, title=[6]小米手机, category=手机, price=2005.0, images=http://www.atguigu/xm.jpg)
Product(id=5, title=[5]小米手机, category=手机, price=2004.0, images=http://www.atguigu/xm.jpg)
SpringDataESDocPageTest.java 文档多条件查询(条件+高亮+排序+分页)

新增几条测试数据用于测试

    //文档多条件查询(条件+高亮+排序+分页)
    @Test
    void testSearch() throws Exception {
        SearchRequest searchRequest = new SearchRequest(ES_INDEX);

        //构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

//        MatchAllQueryBuilder termQueryBuilder = QueryBuilders.matchAllQuery();//matchAll匹配所有

        //查询条件可以使用QueryBuilders工具类实现
        String field = "category";//字段
        String fieldValue = "测试手机";//字段值
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(field, fieldValue);//term精确匹配
        sourceBuilder.query(termQueryBuilder);

        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder
                .field(field)//高亮字段
                .requireFieldMatch(false)//是否每个字都高亮
                .preTags("")//高亮前缀
                .postTags("");//高亮后缀
        sourceBuilder.highlighter(highlightBuilder);

//        sourceBuilder.sort("price");//默认升序
        sourceBuilder.sort("price", SortOrder.DESC);//按price降序
        sourceBuilder.from(0);//页码
        sourceBuilder.size(2);//每页条数
        sourceBuilder.timeout(new Timevalue(60, TimeUnit.SECONDS));

        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(searchResponse.getHits()));

        System.out.println("*******打印文档内容*********");
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }

        System.out.println("********内容高亮显示********");
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            Map highlightFields = documentFields.getHighlightFields();
            HighlightField title = highlightFields.get(field);
            Map sourceAsMap = documentFields.getSourceAsMap();
            if (title != null) {
                Text[] fragments = title.fragments();
                String newTitle = "";
                for (Text text : fragments) {
                    newTitle += text;
                }
                sourceAsMap.put(field, newTitle);
            }
            System.out.println(sourceAsMap);
        }
    }
{"fragment":true,"hits":[{"documentFields":{},"fields":{},"fragment":false,"highlightFields":{"category":{"fragment":true,"fragments":[{"fragment":true}],"name":"category"}},"id":"10","matchedQueries":[],"metadataFields":{},"primaryTerm":0,"rawSortValues":[],"score":null,"seqNo":-2,"sortValues":[5999.0],"sourceAsMap":{"images":"http://www.atguigu/htc.jpg","price":5999.0,"_class":"com.elasticsearch.springdata.pojo.Product","id":10,"title":"HTC手机","category":"测试手机"},"sourceAsString":"{"_class":"com.elasticsearch.springdata.pojo.Product","id":10,"title":"HTC手机","category":"测试手机","price":5999.0,"images":"http://www.atguigu/htc.jpg"}","sourceRef":{"fragment":true},"type":"_doc","version":-1},{"documentFields":{},"fields":{},"fragment":false,"highlightFields":{"category":{"fragment":true,"fragments":[{"fragment":true}],"name":"category"}},"id":"11","matchedQueries":[],"metadataFields":{},"primaryTerm":0,"rawSortValues":[],"score":null,"seqNo":-2,"sortValues":[999.0],"sourceAsMap":{"images":"http://www.atguigu/xlj.jpg","price":999.0,"_class":"com.elasticsearch.springdata.pojo.Product","id":11,"title":"小辣椒手机","category":"测试手机"},"sourceAsString":"{"_class":"com.elasticsearch.springdata.pojo.Product","id":11,"title":"小辣椒手机","category":"测试手机","price":999.0,"images":"http://www.atguigu/xlj.jpg"}","sourceRef":{"fragment":true},"type":"_doc","version":-1}],"maxScore":null,"totalHits":{"relation":"EQUAL_TO","value":3}}
*******打印文档内容*********
{images=http://www.atguigu/htc.jpg, price=5999.0, _class=com.elasticsearch.springdata.pojo.Product, id=10, title=HTC手机, category=测试手机}
{images=http://www.atguigu/xlj.jpg, price=999.0, _class=com.elasticsearch.springdata.pojo.Product, id=11, title=小辣椒手机, category=测试手机}
********内容高亮显示********
{images=http://www.atguigu/htc.jpg, price=5999.0, _class=com.elasticsearch.springdata.pojo.Product, id=10, title=HTC手机, category=测试手机}
{images=http://www.atguigu/xlj.jpg, price=999.0, _class=com.elasticsearch.springdata.pojo.Product, id=11, title=小辣椒手机, category=测试手机}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号