es依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
实体类
package com.lzy.order.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.format.annotation.DateTimeFormat;
/**
* 产品表
* @TableName tb_product
*/
@TableName(value ="tb_product")
@Data
@Document(indexName = "tb_product",shards = 1, replicas = 1)
public class TbProduct implements Serializable {
/**
* 产品ID
*/
@TableId(type = IdType.AUTO)
@Id
private Integer productId;
/**
* 产品名称
*/
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String productName;
/**
* 产品价格,单位:分
*/
@Field(type = FieldType.Integer)
private Integer productPrice;
/**
* 产品库存
*/
@Field(type = FieldType.Integer)
private Integer productWare;
/**
* 产品描述
*/
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String productDesc;
/**
* 删除状态0:未删除,1:已删除
*/
@Field(type = FieldType.Integer)
private Integer deleted;
/**
* 创建时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second")
private Date createTime;
/**
* 更新时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Field(type = FieldType.Date, format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second")
private Date updateTime;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
controller层
@PostMapping(value = "findByword")
public ResponentIty findByword(@RequestBody SearchInfoVo searchInfoVo){
return tbProductService.findByword(searchInfoVo);
}
service层
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Override
public ResponentIty findByword(SearchInfoVo searchInfoVo) {
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//分页
PageRequest page = PageRequest.of(searchInfoVo.getPageNum()-1, searchInfoVo.getPageSize());
//分词查询
if(searchInfoVo.getKeyWord()!=null){
MatchQueryBuilder query = QueryBuilders.matchQuery("productName", searchInfoVo.getKeyWord());
builder.withQuery(query);
}else {
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
builder.withQuery(matchAllQueryBuilder);
}
//高亮查询
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<font color='red'>").postTags("</font>").field("productName");
//排序
FieldSortBuilder order = new FieldSortBuilder("productPrice").order(SortOrder.ASC);
NativeSearchQuery build = builder.withHighlightBuilder(highlightBuilder).withPageable(page).withSort(order).build();
//总条数
long count = elasticsearchRestTemplate.count(build, TbProduct.class);
SearchHits<TbProduct> productSearchHits = elasticsearchRestTemplate.search(build, TbProduct.class);
List<SearchHit<TbProduct>> searchHits = productSearchHits.getSearchHits();
ArrayList<TbProduct> list = new ArrayList<>();
for (SearchHit<TbProduct> searchHit : searchHits) {
//获取对象
TbProduct content = searchHit.getContent();
//获取高亮
if(searchHit.getHighlightField("productName").size()>0){
String productName = searchHit.getHighlightField("productName").get(0);
content.setProductName(productName);
}
list.add(content);
}
// 计算页数
Long pages = 0L;
if(count%searchInfoVo.getPageSize()>0) {
pages = count/searchInfoVo.getPageSize() + 1;
}
else {
pages = count/searchInfoVo.getPageSize();
}
HashMap<String, Object> map = new HashMap<>();
map.put("records",list);
map.put("pageNum",searchInfoVo.getPageNum());
map.put("pageSize",searchInfoVo.getPageSize());
map.put("total",count);
map.put("pages", pages);
return new ResponentIty(map,"成功",200);
}
版权声明:本文为lzyyyds55原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。