步骤一: 导入pom文件
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
步骤二: 写入xml文件配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch // 集群的名称
cluster-nodes: 127.0.0.1:9300 // 集群的配置
步骤一: 注入ElasticsearchTemplate 对象
@Resource
private ElasticsearchTemplate elasticsearchTemplate;
步骤二: 创建映射类
package com.xma.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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;
/**
* @author: mingan.xie
* @since: 2020/6/6 11:22
* @email:
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Document(indexName = "defan",type = "book",shards = 1,replicas = 0)
public class Book {
@Id
private Long id;
@Field(type = FieldType.Integer)
private Integer bid;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Float)
private float price;
}
操作索引API
import javax.annotation.Resource;
/**
* @author: mingan.xie
* @since: 2020/6/6 12:26
* @email:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestElasticsearchTemplate {
@Resource
private ElasticsearchTemplate elasticsearchTemplate;
/**
* 创建索引
*/
@Test
public void run1(){
elasticsearchTemplate.createIndex(Book.class);
}
/**
* 添加映射
*/
@Test
public void run2(){
elasticsearchTemplate.putMapping(Book.class);
}
/**
* 删除索引
*/
@Test
public void run3(){
elasticsearchTemplate.deleteIndex(Book.class);
}
}
创建接口并继承ElasticsearchRepository<目标类,Long(id)>
package com.xma.book;
import com.xma.domain.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @author: mingan.xie
* @since: 2020/6/6 11:33
* @email:
*/
public interface BookRepository extends ElasticsearchRepository<Book,Long> {
}
package com.xma.Test;
import com.xma.TestApplication;
import com.xma.book.BookRepository;
import com.xma.domain.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
/**
* @author: mingan.xie
* @since: 2020/6/6 11:30
* @email:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestBookRepository {
@Resource
private BookRepository bookRepository;
/**
* 查询所有
*/
@Test
public void run1(){
Iterable<Book> all = bookRepository.findAll();
for (Book book : all) {
System.out.println(book);
}
}
/**
* 查询指定id
*/
@Test
public void run2(){
Optional<Book> all = bookRepository.findById(1L);
// 判断是否存在
boolean present = all.isPresent();
System.out.println(present);
}
/**
* 添加和修改是同一个方法,id不存在,则添加,存在则修改
*/
@Test
public void run3(){
Book all = bookRepository.save(new Book(5L,1,"草房子",15.0f));
System.out.println(all);
}
}
package com.xma.Test;
import com.xma.TestApplication;
import com.xma.book.BookRepository;
import com.xma.domain.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
/**
* @author: mingan.xie
* @since: 2020/6/6 11:30
* @email:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestBookRepository {
@Resource
private BookRepository bookRepository;
/**
* 自定义方法: 根据价格区间查询
*/
@Test
public void run5(){
final List<Book> all = bookRepository.findBooksByPriceBetween(11.0f,20.3f);
System.out.println(all);
}
/**
* 自定义方法: 根据价格区间查询 正序
*/
@Test
public void run6(){
final List<Book> all = bookRepository.findBooksByPriceBetweenOrderByPriceAsc(11.0f,20.3f);
System.out.println(all);
}
}
package com.xma.Test;
import com.xma.TestApplication;
import com.xma.book.BookRepository;
import com.xma.domain.Book;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
/**
* @author: mingan.xie
* @since: 2020/6/6 11:30
* @email:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestBookRepository {
@Resource
private BookRepository bookRepository;
/**
* 自定义查询: 单条件查询
*/
@Test
public void run7() {
// 创建条件构造器
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 拼接条件: query -> match
queryBuilder.withQuery(QueryBuilders.matchQuery("name", "简史"));
// 调用查询
Page<Book> search = bookRepository.search(queryBuilder.build());
for (Book book : search) {
System.out.println(book);
}
}
/**
* 区间查询并分页
*/
@Test
public void run8() {
// 完成写法
/*NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withFilter(QueryBuilders.rangeQuery("price").gte(10).lt(50));
nativeSearchQueryBuilder.withPageable(PageRequest.of(0,2));
Page search = bookRepository.search(nativeSearchQueryBuilder.build());*/
// 简化写法: filter -> range -> price -> gte: 10/ite: 50
NativeSearchQuery build = new NativeSearchQueryBuilder().withFilter(QueryBuilders.rangeQuery("price").gte(10).lt(50))
// 分页语法
.withPageable(PageRequest.of(0, 2)).build();
Page<Book> search = bookRepository.search(build);
for (Book book : search) {
System.out.println(book);
}
}
/**
* 多条件查询
*/
@Test
public void run9() {
// 完整写法
/*// 创建条件构造器
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 拼接条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name","简史"));
boolQueryBuilder.must(QueryBuilders.matchQuery("name","时间"));
queryBuilder.withPageable(PageRequest.of(0,2));
Page search = bookRepository.search(queryBuilder.build());*/
// 简化写法: query -> bool -> must/not must/should -> math
NativeSearchQuery build = new NativeSearchQueryBuilder()
// query
.withQuery(
// bool -> must
QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("name", "简史"))
// math
.must(QueryBuilders.matchQuery("name", "简史"))
)
// 分页操作
.withPageable(PageRequest.of(0, 2)).build();
Page<Book> search = bookRepository.search(build);
for (Book book : search) {
System.out.println(book);
}
}
@Test
public void run10() {
SortBuilders.fieldSort("price").order(SortOrder.DESC)
// 设置排序 - 排序字段 - 排序方式
FieldSortBuilder order = SortBuilders.fieldSort("price").order(SortOrder.DESC);
// 创建条件构造器 并拼接条件
NativeSearchQuery build = new NativeSearchQueryBuilder()
// 设置查询条件
.withQuery(QueryBuilders.rangeQuery("price").gte(10f))
// 设置排序
.withSort(order)
// 设置分页
.withPageable(PageRequest.of(0, 5)).build();
Page<Book> search = bookRepository.search(build);
for (Book book : search) {
System.out.println(book);
}
}
}