<!-- elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.2.6.RELEASE</version> </dependency> <!-- elasticsearch--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.2.6.RELEASE</version> </dependency>
elasticsearch版本:6.8.4
pojo类
@Document(indexName = "jd", type = "item") //指定索引(数据库)、类型(数据表)
@Setting(settingPath = "elastic-setting.json")
@Data
public class ItemByEs {
@Id
private String id;
@Field(type = FieldType.Keyword, normalizer = "string_lowercase")//指定类型、使用的分词器
private String title; //标题
@Field(type = FieldType.Keyword)
private String category;// 分类
@Field(type = FieldType.Keyword)
private String brand; // 品牌
@Field(type = FieldType.Double)
private Double price; // 价格
@Field(index = false, type = FieldType.Keyword) //index = false
private String images; // 图片地址
}
elastic-setting.json
{ "analysis": { "normalizer": { "string_lowercase": { "type": "custom", "filter": [ "lowercase" ] } } } }
query:
QueryBuilder matchQuery = QueryBuilders.matchQuery("title", itemByEsVo.getTitle())
// .analyzer("caseSensitive")
.operator(Operator.OR);//or查询 Operator.OR、and查询Operator.AND
SearchResponse response = client.prepareSearch("jd")
.setQuery(matchQuery)
.highlighter(highlightBuilder)
// 设置一次返回的文档数量
.setSize(10)
.get();
结果:
Index:jd
Type:item
Id:1
title: huawei1
Scope:2.5389738
<span style="color:red;">huawei1</span>
Source As String:{"id":"51","title":"HuaWei1","category":"手机","brand":"小米","price":3550.0,"images":"http://image.baidu.com/13123.jpg"}
Source As Map:{images=http://image.baidu.com/13123.jpg, price=3550.0, id=51, title=HuaWei1, category=手机, brand=小米}
Index:jd
Type:item
Id:51
title: HuaWei1
Scope:2.4849067
<span style="color:red;">HuaWei1</span>
[{Source As String={"id":"1","title":"huawei1","category":"手机","brand":"华为","price":3500.0,"images":"http://image.baidu.com/13123.jpg"}, Source As Map={images=http://image.baidu.com/13123.jpg, price=3500.0, id=1, title=huawei1, category=手机, brand=华为}, Index=jd, Type=item, Id=1, Title=huawei1, Scope=2.5389738, Highlight=<span style="color:red;">huawei1</span>}, {Source As String={"id":"51","title":"HuaWei1","category":"手机","brand":"小米","price":3550.0,"images":"http://image.baidu.com/13123.jpg"}, Source As Map={images=http://image.baidu.com/13123.jpg, price=3550.0, id=51, title=HuaWei1, category=手机, brand=小米}, Index=jd, Type=item, Id=51, Title=HuaWei1, Scope=2.4849067, Highlight=<span style="color:red;">HuaWei1</span>}]
共查出2条记录!
共耗时8ms