1.引入依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
2.定义
import io.swagger.annotations.apiModel;
import java.io.Serializable;
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;
import org.springframework.data.elasticsearch.annotations.Setting;
@Document(indexName = "GoodsIdx", type = "goodsType")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Setting(settingPath = "elastic-setting.JSON")
public class GoodsIndex implements Serializable {
@Id
private Long id;
@Field(type = FieldType.KeyWord, analyzer = "caseSensitive")
private String name;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String brief;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String introduction;
}
3.定义配置文件
{
"analysis": {
"analyzer": {
"caseSensitive": {
"filter": "lowercase",
"type": "custom",
"tokenizer": "keyword"
}
}
}
}
4.实现忽略大小写的模糊查询
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void testSearch() {
String content = "abc";
Map<Long, List<Goods>> rst = new HashMap<>();
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
BoolQueryBuilder contentQueryBuilder = QueryBuilders.boolQuery();
WildcardQueryBuilder groupNameQueryBuilder = QueryBuilders.wildcardQuery("name", "*" + content + "*");
MatchQueryBuilder briefMatchQueryBuilder = new MatchQueryBuilder("brief", content);
briefMatchQueryBuilder.operator(Operator.AND);
MatchQueryBuilder detailMatchQueryBuilder = new MatchQueryBuilder("introduction", content);
detailMatchQueryBuilder.operator(Operator.AND);
contentQueryBuilder
.should(groupNameQueryBuilder)
.should(briefMatchQueryBuilder)
.should(detailMatchQueryBuilder)
;
boolQueryBuilder.must(contentQueryBuilder);
queryBuilder.withQuery(boolQueryBuilder);
queryBuilder.withPageable(PageRequest.of(0, 10000));
List<Goods> serviceGroupInfos = elasticsearchRestTemplate
.queryForList(queryBuilder.build(), Goods.class);
}
5.搜索语句
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"groupName": {
"wildcard": "*银行*",
"boost": 1
}
}
},
{
"match": {
"groupBrief": {
"query": "银行",
"operator": "AND",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
{
"match": {
"groupIntroduction": {
"query": "银行",
"operator": "AND",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}