这篇“SpringBoot集成ElasticSearch的代码是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringBoot集成ElasticSearch的代码是什么”文章吧。

一、Elasticseach介绍1.简单介绍

Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。是一种全文检索技术。

Elasticsearch 是位于 Elastic Stack 核心的分布式搜索和分析引擎。Logstash 和 Beats 有助于收集、聚合和丰富您的数据并将其存储在 Elasticsearch 中。Kibana 使您能够以交互方式探索、可视化和分享对数据的见解,并管理和监控堆栈。Elasticsearch 是索引、搜索和分析魔法发生的地方。

Elasticsearch 是一个基于JSON的分布式搜索和分析引擎。

Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

2.对比关系:

索引(indices)--------------------------------Databases 数据库

类型(type)-----------------------------Table 数据表
文档(Document)----------------Row 行
字段(Field)-------------------Columns 列

3.详细说明:

概念

说明

索引库(indices)

indices是index的复数,代表许多的索引,

类型(type)

类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,比如商品索引,订单索引,其数据格式不同。不过这会导致索引库混乱,因此未来版本中会移除这个概念

文档(document)

存入索引库原始的数据。比如每一条商品信息,就是一个文档

字段(field)

文档中的属性

映射配置(mappings)

字段的数据类型、属性、是否索引、是否存储等特性

4.查出数据的解释

took:查询花费时间,单位是毫秒
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据

二、SpringBoot集成Elasticseach1.引入依赖

<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>2.添加配置

spring:data:elasticsearch:cluster-name:elasticsearchcluster-nodes:192.168.7.132:93003.创建pojo类与索引对应

packagecom.leyou.elasticsearch.pojo;importorg.springframework.data.annotation.Id;importorg.springframework.data.elasticsearch.annotations.Document;importorg.springframework.data.elasticsearch.annotations.Field;importorg.springframework.data.elasticsearch.annotations.FieldType;/***创建pojo类与索引对应**@authorPromsing(张有博)*@version1.0.0*@since2022/1/26-20:35*/@Document(indexName="item",type="docs",shards=1,replicas=0)publicclassItem{@IdprivateLongid;/***标题*/@Field(type=FieldType.Text,analyzer="ik_max_word")privateStringtitle;*分类@Field(type=FieldType.Keyword)privateStringcategory;*品牌privateStringbrand;*价格@Field(type=FieldType.Double)privateDoubleprice;*图片地址privateStringimages;publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;publicStringgetTitle(){returntitle;publicvoidsetTitle(Stringtitle){this.title=title;publicStringgetCategory(){returncategory;publicvoidsetCategory(Stringcategory){this.category=category;publicStringgetBrand(){returnbrand;publicvoidsetBrand(Stringbrand){this.brand=brand;publicDoublegetPrice(){returnprice;publicvoidsetPrice(Doubleprice){this.price=price;publicStringgetImages(){returnimages;publicvoidsetImages(Stringimages){this.images=images;publicItem(){publicItem(Longid,Stringtitle,Stringcategory,Stringbrand,Doubleprice,Stringimages){@OverridepublicStringtoString(){return"Item{"+"id="+id+",title='"+title+'\''+",category='"+category+'\''+",brand='"+brand+'\''+",price="+price+",images='"+images+'\''+'}';}4.SpringData封装了基础的增删改查,自定义增删改查

这里需要继承接口-ItemRepository

/***自定义的增删改查接口**@authorPromsing(张有博)*@version1.0.0*@since2022/1/27-15:10*/publicinterfaceItemRepositoryextendsElasticsearchRepository<Item,Long>{List<Item>findByTitle(Stringtitle);List<Item>findByPriceBetween(Doubled1,Doubled2);}5.测试方法--增删改查

packagecom.leyou.elasticsearch;importcom.leyou.elasticsearch.dao.ItemRepository;importcom.leyou.elasticsearch.pojo.Item;importorg.elasticsearch.index.query.MatchQueryBuilder;importorg.elasticsearch.index.query.QueryBuilders;importorg.elasticsearch.search.sort.SortBuilders;importorg.elasticsearch.search.sort.SortOrder;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.data.domain.Page;importorg.springframework.data.domain.PageRequest;importorg.springframework.data.domain.Sort;importorg.springframework.data.elasticsearch.core.ElasticsearchTemplate;importorg.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.ArrayList;importjava.util.List;importjava.util.Optional;/***测试ES的增删改查**@authorPromsing(张有博)*@version1.0.0*@since2022/1/26-20:57*/@SpringBootTest(classes=ElasticsearchApplication.class)@RunWith(SpringRunner.class)publicclassElasticsearchTest{@AutowiredprivateElasticsearchTemplateelasticsearchTemplate;//ES的模板类privateItemRepositoryitemRepository;//Item的增删改查/***创建索引库*/@TestpublicvoidtestIndex(){this.elasticsearchTemplate.createIndex(Item.class);this.elasticsearchTemplate.putMapping(Item.class);//this.elasticsearchTemplate.deleteIndex();}*插入与更新publicvoidtestCreate(){Itemitem=newItem(1L,"小米手机9","手机","小米",3999.00,"https:www.baidu.com");Objectsave=this.itemRepository.save(item);List<Item>list=newArrayList<>();list.add(newItem(2L,"坚果手机R1","手机","锤子",3699.00,"http://image.leyou.com/123.jpg"));list.add(newItem(3L,"华为META10","手机","华为",4499.00,"http://image.leyou.com/3.jpg"));//接收对象集合,实现批量新增Iterable<Item>items=itemRepository.saveAll(list);System.out.println(items);*删除publicvoidtestDelete(){Itemitem=newItem(1L,"小米手机9","手机","小米",3999.00,"https:www.baidu.com");this.itemRepository.delete(item);*查询publicvoidtestFind(){System.out.println("-----主键查询------");Optional<Item>byId=this.itemRepository.findById(1L);System.out.println(byId.get());System.out.println("-----查询全部------");Iterable<Item>all=this.itemRepository.findAll();all.forEach(i->System.out.println(i));System.out.println("-----排序查询(升序降序)------");Iterable<Item>price=this.itemRepository.findAll(Sort.by("price").descending());price.forEach(System.out::println);*调用自定义方法publicvoidtestFindByU(){List<Item>phone=this.itemRepository.findByTitle("手机");//phone.forEach(i->{//System.out.println(i);//});List<Item>byPriceBetween=this.itemRepository.findByPriceBetween(4000.0,5000.0);byPriceBetween.forEach(i->System.out.println(i));*批量插入publicvoidindexList(){list.add(newItem(1L,"小米手机7","手机","小米",3299.00,"http://image.leyou.com/13123.jpg"));list.add(newItem(2L,"坚果手机R1","手机","锤子",3699.00,"http://image.leyou.com/13123.jpg"));list.add(newItem(3L,"华为META10","手机","华为",4499.00,"http://image.leyou.com/13123.jpg"));list.add(newItem(4L,"小米Mix2S","手机","小米",4299.00,"http://image.leyou.com/13123.jpg"));list.add(newItem(5L,"荣耀V10","手机","华为",2799.00,"http://image.leyou.com/13123.jpg"));itemRepository.saveAll(list);*高级查询publicvoidtestSearch(){//通过查询构建器工具构建--重点:QueryBuilders:词条、模糊、范围MatchQueryBuilderqueryBuilder=QueryBuilders.matchQuery("title","手机");//获取结果集Iterable<Item>items=this.itemRepository.search(queryBuilder);items.forEach(System.out::println);*重点--自定义查询publicvoidtestNative(){//构建自定义查询构建器NativeSearchQueryBuilderqueryBuilder=newNativeSearchQueryBuilder();//添加基本查询条件queryBuilder.withQuery(QueryBuilders.matchQuery("title","手机"));//查询分页结果集Page<Item>itemPage=this.itemRepository.search(queryBuilder.build());System.out.println(itemPage.getTotalPages());System.out.println(itemPage.getTotalElements());itemPage.forEach(i->System.out.println(i));*重点--分页查询publicvoidtestPage(){queryBuilder.withQuery(QueryBuilders.matchQuery("category","手机"));queryBuilder.withPageable(PageRequest.of(1,2));*重点--排序publicvoidtestSort(){queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));}

以上就是关于“SpringBoot集成ElasticSearch的代码是什么”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。