ElasticSearch实现忽略大小写模糊查询
短信预约 -IT技能 免费直播动态提醒
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
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341