一、需求说明
1、如果选了同城,则要查同城的动态,参数:cityCode
2、如果输入了关键字,则按关键字分词搜索,参数:keyword
查询条件:cityCode + keyword
二、示例代码
String fieldterm = "cityCode";
String termValue = 420100000000;
SearchRequest request = new SearchRequest(index);
SearchSourceBuilder builder = new SearchSourceBuilder();
QueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
builder.query(matchAllQuery);
Integer from = (pageNo - 1) * pageSize;
builder.from(from);
builder.size(pageSize);
if (StringUtils.isNotBlank(keyword) || StringUtils.isNotBlank(fieldterm)) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(fieldterm)) {
// 同城查询:cityCode
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(fieldterm , termValue);
boolQueryBuilder.must(termQueryBuilder);
}
//关键字查询
if (StringUtils.isNotBlank(keyword)) {
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, ArrayUtil.toArray(fieldList, String.class));
if (pageSearchVO.getSearchKeyParticiple()) {
//分词查询
multiMatchQueryBuilder.operator(Operator.OR);
} else {
//精确查询(关键字不分词查询)
multiMatchQueryBuilder.operator(Operator.AND);
}
boolQueryBuilder.must(multiMatchQueryBuilder);
}
builder.query(boolQueryBuilder);
}
if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(orderBy)) {
FieldSortBuilder order = new FieldSortBuilder(orderField);
if (orderBy.equals("ASC")) {
order.order(SortOrder.ASC);
} else {
order.order(SortOrder.DESC);
}
builder.sort(order);
}
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
三、查询语句
{
"from": 0,
"size": 20,
"query": {
"bool": {
"must": [
{
"term": {
"cityCode": {
"value": "420100000000",
"boost": 1
}
}
},
{
"multi_match": {
"query": "科恩",
"fields": [
"calendarTags^1.0",
"calendarTitle^1.0",
"calenderBuType^1.0",
"cityName^1.0",
"content^1.0",
"tagsStr^1.0",
"topicStr^1.0"
],
"type": "best_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
网友评论