索引
{
"mappings" : {
"_source" : {
"includes" : [
"name",
"shortname",
"score",
"status",
"state",
"classify",
"province",
"city",
"frName",
"entType",
"entStatus",
"regNo",
"regCap",
"regOrg",
"authorityCode",
"esDate",
"tagArr"
]
},
"properties" : {
"authorityCode" : {
"type" : "keyword",
"store" : true
},
"city" : {
"type" : "keyword",
"store" : true
},
"classify" : {
"type" : "keyword",
"store" : true
},
"entStatus" : {
"type" : "keyword",
"store" : true
},
"entType" : {
"type" : "keyword",
"store" : true
},
"esDate" : {
"type" : "long",
"store" : true
},
"frName" : {
"type" : "keyword",
"store" : true
},
"lastmodifiedtime" : {
"type" : "date"
},
"name" : {
"type" : "text",
"analyzer" : "ik_pinyin_analyzer"
},
"province" : {
"type" : "keyword",
"store" : true
},
"regCap" : {
"type" : "keyword",
"store" : true
},
"regNo" : {
"type" : "keyword",
"store" : true
},
"regOrg" : {
"type" : "keyword",
"store" : true
},
"score" : {
"type" : "integer",
"store" : true
},
"shortname" : {
"type" : "text",
"analyzer" : "ik_pinyin_analyzer"
},
"state" : {
"type" : "keyword",
"store" : true
},
"status" : {
"type" : "integer",
"store" : true
},
"tagArr" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "3",
"max_result_window" : "10000000",
"analysis" : {
"filter" : {
"my_pinyin" : {
"lowercase" : "true",
"keep_original" : "true",
"remove_duplicated_term" : "true",
"keep_separate_first_letter" : "false",
"type" : "pinyin",
"limit_first_letter_length" : "16",
"keep_full_pinyin" : "true"
}
},
"analyzer" : {
"ik_pinyin_analyzer" : {
"filter" : [
"my_pinyin"
],
"type" : "custom",
"tokenizer" : "ik_max_word"
}
}
},
"number_of_replicas" : "0"
}
}
}
查询demo
public class EsTest {
@Autowired
private RestHighLevelClient restClient;
public void esTest(Parm parm) throws IOException {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//搜索的条件
if (parm.getCondition() != null && !"".equals(parm.getCondition())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("name", parm.getCondition()).minimumShouldMatch("100%"));
}
if (parm.getProvince() != null && !"".equals(parm.getProvince())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("province", parm.getProvince()));
}
if (parm.getCity() != null && !"".equals(parm.getCity())) {
boolQueryBuilder.must(QueryBuilders.matchQuery("city", parm.getCity()));
}
//classify 字段在classify列表中
if (parm.getIndustryCategoriesList() != null && parm.getIndustryCategoriesList().size() > 0) {
boolQueryBuilder.must(QueryBuilders.termsQuery("classify", parm.getIndustryCategoriesList()));
}
//存在某个字段
boolQueryBuilder.must(QueryBuilders.existsQuery("tagArr"));
//大于等于 小于等于
boolQueryBuilder.must(QueryBuilders.rangeQuery("score").gte(parm.getScoreBegin()).lte(parm.getScoreEnd()));
searchSourceBuilder.query(boolQueryBuilder);
//分页
searchSourceBuilder.from((parm.getPageNo() - 1) * parm.getPageSize());
searchSourceBuilder.size(parm.getPageSize());
//设置高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
/*highlightBuilder.fields().add(new HighlightBuilder.Field("name"));*/
highlightBuilder.field("name").field("classify").preTags("<em>").postTags("</em>").requireFieldMatch(false);
//最大高亮分片数
highlightBuilder.fragmentSize(800000);
//从第一个分片获取高亮片段
highlightBuilder.numOfFragments(0);
searchSourceBuilder.highlighter(highlightBuilder);
HashMap resultEs = new HashMap();
// SearchRequest searchRequest = new SearchRequest(ES_INDEX).types(ES_TYPE);
SearchRequest searchRequest = new SearchRequest("index");
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
//获得匹配总记录
long totalHits = hits.getTotalHits().value;
// 如果总数为0 就是没查到数据在es里面 就直接返回
if (totalHits == 0) {
}
SearchHit[] searchHits = hits.getHits();
//处理数据
for (SearchHit hit : searchHits) {
String id = hit.getId();
}
resultEs.put("totalCounts", totalHits);
resultEs.put("totalPage", totalHits % parm.getPageSize() > 0 ? totalHits / parm.getPageSize() + 1 : totalHits / parm.getPageSize());
resultEs.put("pageNo", parm.getPageNo());
resultEs.put("pageSize", parm.getPageSize());
}
}
@Data
class Parm{
public String condition;
public String province;
public String city;
public List<String> industryCategoriesList;
public Integer scoreBegin;
public Integer scoreEnd;
private int pageNo;
private int pageSize = 10;
}