SpringBoot2.3.6整合Elasticsearch7.10.1教程

网上教程多是Elasticsearch旧版本,很多方法已弃用,本文以Elasticsearch 7.10.1,SpringBoot 2.3.6.RELEASE为例说明如何整合:

一、添加pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.um.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

二、application.yml文件配置

spring:
  elasticsearch:
    rest:
      uris: http://10.211.55.8:9200

三、实体类及注解

Spring Data通过注解来声明字段的映射属性,有下面的三个注解:

  • @Document 作用在类,标记实体类为文档对象,一般有以下属性
    • indexName:对应索引库名称
    • shards:分片数量,默认5
    • replicas:副本数量,默认1
  • @Id 作用在成员变量,标记一个字段作为id主键
  • @Field 作用在成员变量,标记为文档的字段,并指定字段映射属性:
    • type:字段类型,取值是枚举:FieldType
    • index:是否索引,布尔类型,默认是true
    • store:是否存储,布尔类型,默认是false
    • analyzer:分词器名称:ik_max_word

示例代码:

@Document(indexName = "item", shards = 1, replicas = 0)
public class Item {
	@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.Keyword)
	private String brand;
	@Field(type = FieldType.Double)
	private Double price;
	@Field(type = FieldType.Keyword, index = false)
	private String images;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getImages() {
		return images;
	}

	public void setImages(String images) {
		this.images = images;
	}
}

四、Elasticsearch索引操作

1、创建索引
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticsearchTest {

	@Autowired
	private ElasticsearchRestTemplate elasticsearchRestTemplate;

	@Test
	public void testCreateIndex(){
		IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Item.class);
		Document mapping = indexOperations.createMapping();
		indexOperations.putMapping(mapping);
	}
}
2、删除索引
	@Test
	public void testDeleteIndex() {
		IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Item.class);
		indexOperations.delete();
	}

五、Elasticsearch文档操作

1、新增/修改文档

定义接口ItemRepository继承ElasticsearchRepository

public interface ItemRepository extends ElasticsearchRepository<Item, Long> {

	List<Item> findByTitle(String title);

	List<Item> findByPriceBetween(Double d1,Double d2);
}

新增文档

	@Test
	public void testCreate() {
		Item item = new Item(1L, "小米手机7", " 手机", "小米", 3499.00, "http://image.leyou.com/13123.jpg");
		itemRepository.save(item);
	}
2、删除文档
	@Test
	public void testDelete() {
		itemRepository.deleteById(1L);
	}
3、基本查询
	@Test
	public void testFind() {
		Iterable<Item> items = itemRepository.findAll(Sort.by("price").descending());
		items.forEach(System.out::println);
	}

	@Test
	public void testFindByTitle() {
		List<Item> items = itemRepository.findByTitle("手机");
		items.forEach(System.out::println);
	}

	@Test
	public void testFindByPriceBetween() {
		List<Item> items = itemRepository.findByPriceBetween(3699D, 4499D);
		items.forEach(System.out::println);
	}
4、高级查询
	@Test
	public void testSearch() {
		NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
		queryBuilder.withQuery(QueryBuilders.matchQuery("title", "手机"));
		int page = 0;
		int size = 1;
		queryBuilder.withPageable(PageRequest.of(page, size));
		SearchHits<Item> hits = elasticsearchRestTemplate.search(queryBuilder.build(), Item.class);
		List<Item> items = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
		items.forEach(System.out::println);
	}
5、聚合查询
	@Test
	public void testAgg() {
		// 初始化自定义查询构建器
		NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
		// 添加聚合,嵌套聚合求平均值
		queryBuilder.addAggregation(AggregationBuilders.terms("brandAgg").field("brand")
				.subAggregation(AggregationBuilders.avg("price_avg").field("price")));
		// 添加结果集过滤,不包括任何字段
		queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{}, null));
		// 执行聚合查询,获取分组数据
		SearchHits<Item> searchHits = elasticsearchRestTemplate.search(queryBuilder.build(), Item.class);
		Terms terms = (Terms) searchHits.getAggregations().asMap().get("brandAgg");
		List<? extends Terms.Bucket> buckets = terms.getBuckets();
		buckets.forEach(bucket -> {
			System.out.println(bucket.getKeyAsString());
			System.out.println(bucket.getDocCount());
			Map<String, Aggregation> stringAggregationMap = bucket.getAggregations().asMap();
			Avg price_avg = (Avg) stringAggregationMap.get("price_avg");
			System.out.println(price_avg.getValue());
		});
	}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脉脉情缘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值