1.Kibana集合设置
PUT demo_v1{
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
},
"analysis": {
"analyzer": {
"comma": {
"type": "pattern",
"pattern":","
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"goods_list": {
"type": "nested",
"properties": {
"goods_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"analyzer": "ik_max_word"
},
"goods_type_no": {
"type": "text",
"analyzer": "comma",
"search_analyzer": "comma"
}
}
}
}
}
}
说明:
- settings是设置分词
- number_of_replicas 是数据备份数,如果只有一台机器,设置为0
- number_of_shards 是数据分片数,默认为5,有时候设置为3
- analyzer 分词设置
- comma 分词设置的名称
- “type”: “pattern”, 指定是要分词
- “pattern”:"," 设置以逗号分词
- mappings:存储属性的设置,其中goods_list是集合,集合中包含goods_name(Ik分词器)、goods_type_no(自定义分词器)
2.Java查询设置,我们用的是org.elasticsearch.index.query.BoolQueryBuilder
// 设置检索条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (StrUtil.isNotBlank(goodsType)) {
boolQueryBuilder.must(QueryBuilders.nestedQuery("goods_list",
QueryBuilders.matchQuery("goods_list.goods_type_no", goodsType), ScoreMode.None));
}
说明:
goodsType 是自定义的Java变量。nestedQuery是Elasticsearch引用包里的集合查询,matchQuery是分词查询,因为我们第一步已经设置了goods_type_no的自定义分词所以他会直接以逗号分词完成查询。
创作不易,到饭点了,都扫个码,博主实名认证绝对真实。