集成Spring Data ES
具体步骤:
1、导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、application.yml加入如下配置
spring:
elasticsearch:
rest:
uris: http://192.168.136.136:9200
ES API
ES官方原生的API:
Spring Data ElasticSearch: 是Spring针对ElasticSearch操作的时候所提供的一套Java API,底层是对ES官方所提供的Java API进行了封装,用
来简化ES的操作。
3.6.2 RestHighLevelClient(原生api)
注意:当我们在系统中直接导入spring-boot-starter-data-elasticsearch的依赖的时候,关于RestHighLevelClient已经由Spring Boot实现了它的自
动化配置,在Spring容器中已经存在了该类的对象了。
代码演示基本CRUD
实体类:
// 封装文档数据
@Data
publicclassPerson {
privateLongid;
privateStringusername;
privateStringaddress;
privateIntegerage;
}
基本方法:
publicfinalIndexResponseindex(IndexRequestindexRequest, RequestOptionsoptions) throwsIOException
publicfinalGetResponseget(GetRequestgetRequest, RequestOptionsoptions) throwsIOException
publicfinalUpdateResponseupdate(UpdateRequestupdateRequest, RequestOptionsoptions) throwsIOExceptionpublicfinalDeleteResponsedelete(DeleteRequestdeleteRequest, RequestOptionsoptions) throwsIOException
代码演示高级查询
核心方法:
publicfinalSearchResponsesearch(SearchRequestsearchRequest, RequestOptionsoptions) throwsIOException
3.6.3 ElasticsearchRestTemplate
使用Spring Data ES的时候,首先就需要建立实体类和ES索引库之间的对应关系,然后就是建立实体类中的属性和索引库字段之间的映射关系。
@Document(indexName="test_person")
@Data
publicclassPerson {
@Id
privateLongid;
@Field(type=FieldType.Text,analyzer="ik_smart")
privateStringusername;
@Field(type=FieldType.Text, analyzer="ik_smart") //全文检索
privateStringaddress;
@Field(index=false,type=FieldType.Long) //这个字段不用创建检索的倒排索引
privateIntegerage;
}
代码演示基本CRUD
核心方法:
public<T>Iterable<T>save(T... entities)
public<T>Tget(Stringid, Class<T>clazz)
UpdateResponseupdate(UpdateQueryupdateQuery, IndexCoordinatesindex);
voiddelete(DeleteQueryquery, IndexCoordinatesindex);
修改代码:
@Test
publicvoidupdateDocument() {
// 创建一个Document对象
Documentdocument=Document.create();
document.put("username" , "张三") ;
UpdateQueryupdateQuery=UpdateQuery.builder("1").withDocument(document).build() ;
UpdateResponseupdateResponse=elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("person"));
System.out.println(updateResponse);
}
代码演示高级查询
核心方法:
public<T>SearchHits<T>search(Queryquery, Class<T>clazz)
Query对象的构建可以使用:NativeSearchQueryBuilder
3.6.4 ElasticsearchRepository
Spring Data ES提供了ElasticsearchRepository持久层操作的接口,在该接口中定义了数据操作基本的CRUD方法。
使用思想:
1、定义一个接口继承ElasticsearchRepository接口
publicinterfacePersonRepositoryextendsElasticsearchRepository<Person , Long> {}
2、从Spring 容器中获取该接口的代理对象,然后进行CRUD操作
注意:使用了ElasticsearchRepository接口以后,在项目启动的时候会自动进行索引库的创建,需要给字段指定type属性值。
Nested类型介绍
扁平化处理演示
注意:针对集合类型的属性/对象数组存储,默认情况下ES会对其进行扁平化处理
Nested数据类型ES官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/nested.html
PUT my_index/my_type/1
{
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
扁平化后
{
"group" : "fans",
"user.first" : [ "alice", "john" ],
"user.last" : [ "smith", "white" ]
}
导致结果:
"first" : "John",可以和 "last" : "White"组合到一起
@Field(type = FieldType.Nested) 处理扁平化
private Integer age;
@Document(indexName = "goods" , shards = 3 , replicas = 2)
public class Goods {
// String 在 es中可以对应 Text(要分词存储) 、 Keyword(无需分词存储)
// index = false,无需索引。这个字段不会用来检索,不用建立索引
@Field(type = FieldType.Keyword, index = false)
private String defaultImg;
// es 中能分词的字段,这个字段数据类型必须是 text!. keyword 不分词!
@Field(type = FieldType.Text, analyzer = "ik_smart")//ik是分词器ik_samrt为分词粗细力度
private String title; //skuName;
@Field(type = FieldType.Double)
private BigDecimal price;
// @Field(type = FieldType.Date) 6.8.1
@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime; // 新品。 上架时间
// 平台属性集合对象
// Nested 支持嵌套查询,集合属性会被es自动扁平化处理
@Field(type = FieldType.Nested)
private List<SearchAttr> attrs;
}