Java如何操作Elasticsearch
目录
本文文章介绍的是通过template的方法操作elasticsearch,他的话直接本地注入使用就行,repository方法还需要实现接口,所以我个人喜欢template(他还支持复杂查询嘞!)
前言
Procuct实体类
@Document(indexName = "product",createIndex = true)
@Data
@AllArgsConstructor
public class Product {
@Id
@Field(type = FieldType.Integer,store = true,index = true)
private Integer id;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productName;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productDesc;
}
一、操作索引
@SpringBootTest
public class TemplateTest {
@Autowired
private ElasticsearchTemplate template;
// 新增索引
@Test
public void addIndex() {
// 获得索引操作对象
IndexOperations indexOperations = template.indexOps(Product.class);
// 创建索引,注:该方法无法设置索引结构,不推荐使用
indexOperations.create();
}
// 删除索引
@Test
public void delIndex() {
// 获得索引操作对象
IndexOperations indexOperations = template.indexOps(Product.class);
// 删除索引
indexOperations.delete();
}
}
不过一般我们创建索引在实体类创建时就创建或者是在kibna中自己创建就行!
二、操作文档
// 新增/修改文档
@Test
public void addDocument() {
Product product = new Product(7, "es1", "es是一款优秀的搜索引擎");
template.save(product);
}
// 删除文档
@Test
public void delDocument() {
template.delete("7", Product.class);
}
// 根据id查询
@Test
public void findAllDocument() {
Product product = template.get("1", Product.class);
System.out.println(product);
}
三、查询文档
template的search方法可以查询文档
SearchHits<T> search(Query query, Class<T> clazz):查询文档,query是查询条件对象,clazz是结果类型。
// 查询文档2
@Test
public void searchDocument2() {
String productName = "三体";
String productDesc = "优秀";
// 1.构建查询条件
NativeQuery query=new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery())
.build();
// 2.查询
SearchHits<Product> result = template.search(query, Product.class);
// 3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
Product product = productSearchHit.getContent();
System.out.println(product);
}
}
四、复杂条件查询
// 复杂查询
@Test
public void searchDocument2(){
String productName = "三体";
String productDesc = "优秀";
// 1.构造查询条件
NativeQueryBuilder nativeQueryBuilder = new NativeQueryBuilder();
// 如果没有传入参数,查询所有
if (productName == null && productDesc == null){
nativeQueryBuilder.withQuery(Queries.matchAllQueryAsQuery());
}else {
BoolQuery.Builder boolQuery = QueryBuilders.bool();
if (productName != null){
boolQuery.must(Queries.matchQueryAsQuery("productName",productName,null,null));
}
if (productDesc != null){
boolQuery.must(Queries.matchQueryAsQuery("productDesc",productDesc,null,null));
}
nativeQueryBuilder.withQuery(boolQuery.build()._toQuery());
}
NativeQuery query = nativeQueryBuilder.build();
// 2.查询
SearchHits<Product> result = template.search(query, Product.class);
// 3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
Product product = productSearchHit.getContent();
System.out.println(product);
}
}
五、分页查询
// 分页查询文档
@Test
public void searchDocumentPage() {
// 1.构建查询条件
Pageable pageable = PageRequest.of(0, 3);
NativeQuery query = new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery())
.withPageable(pageable)
.build();
// 2.查询
SearchHits<Product> result = template.search(query, Product.class);
// 3.处理查询结果
List<Product> content = new ArrayList();
for (SearchHit<Product> productSearchHit : result) {
Product product = productSearchHit.getContent();
content.add(product);
}
/**
* 封装Page对象,参数1:具体数据,参数2:分页条件对象,参数3:总条数
*/
Page<Product> page = new PageImpl(content, pageable, result.getTotalHits());
System.out.println(page.getTotalElements());
System.out.println(page.getTotalPages());
System.out.println(page.getContent());
}
六、结果排序
// 结果排序
@Test
public void searchDocumentSort() {
// 1.构建查询条件
NativeQuery query = new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery())
.withSort(Sort.by(Sort.Direction.DESC, "id"))
.build();
// 2.查询
SearchHits<Product> result = template.search(query, Product.class);
// 3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
Product product = productSearchHit.getContent();
System.out.println(product);
}
}
原文地址:https://blog.csdn.net/gaoqiandr/article/details/142437243
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!