1.引入maven依赖
<!-- springboot 整合ES -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
2.yml配置
spring:
data:
elasticsearch:
####集群名称
cluster-name: elasticsearch-cluster
####地址
cluster-nodes: 192.168.8.192:9300
3.ProductEntity 实体类
@Document(indexName = "goods", type = "goods")
@Data
public class ProductEntity {
/** 主键ID */
private Integer id;
/** 类型ID */
private Integer categoryId;
/** 名称 */
private String name;
/** 小标题 */
private String subtitle;
/** 主图像 */
private String mainImage;
/** 小标题图像 */
private String subImages;
/** 描述 */
private String detail;
/** 商品规格 */
private String attributeList;
/** 价格 */
private Double price;
/** 库存 */
private Integer stock;
/** 状态 */
private Integer status;
/** 创建人 */
private String createdBy;
/** 创建时间 */
private Date createdTime;
/** 更新时间 */
private Timestamp updatedTime;
}
4.controller层
@RestController
public class ProductSearchController {
@Autowired
private ProductReposiory productReposiory;
//通过es实现商品搜索服务
@RequestMapper("/seach")
public List<ProductEntity> search(String name) {
BoolQueryBuilder builder = QueryBuilders.boolQuery();
// 模拟查询
builder.must(QueryBuilders.fuzzyQuery("name", name));
// 分页
Pageable pageable = new QPageRequest(0, 5);
Page<ProductEntity> page = productReposiory.search(builder, pageable);
List<ProductEntity> content = page.getContent();
return content;
}
}
5.repository层
public interface ProductReposiory extends ElasticsearchRepository<ProductEntity, Long> {
}
6.在启动类添加注解@EnableElasticsearchRepositories(basePackages = “xxx.xxx.xxx”)
7.elasticsearch集成IK分词器,实现中文模糊查询(默认分词器会把汉字分成一个一个字去)
注意:ES版本需要和IK分词器对应一样的版本,否则情况下会报版本号不一致的错误。
8.es文档类型映射
8.1查询映射类型:GET /goods/_mapping
8.2.删除索引:DELETE /goods
8.3.创建索引PUT /goods
//创建文档映射
POST /goods/_mapping/goods
{
"goods": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"attribute_list": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"category_id": {
"type": "long"
},
"created_time": {
"type": "date"
},
"detail": {
"type": "text",
"analyzer":"ik_smart",
"search_analyzer":"ik_smart"
},
"id": {
"type": "long"
},
"main_image": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"analyzer":"ik_smart",
"search_analyzer":"ik_smart"
},
"revision": {
"type": "long"
},
"status": {
"type": "long"
},
"sub_images": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"subtitle": {
"type": "text",
"analyzer":"ik_smart",
"search_analyzer":"ik_smart"
},
"updated_time": {
"type": "date"
}
}
}
}
9.测试
通过postman请求接口:http://192.168.8.192:9200/_analyze
传递json数据查询,分别看通过ik分词器和默认分词器分词后的效果~
{
//使用中文分词器
"analyzer": "ik_smart",
"text": "苹果"
}
{
//使用默认分词器
"analyzer": "standard",
"text": "小米"
}