SearchServiceHelper.java
16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.yohomars.search.service;
import com.yohomars.search.utils.ISearchConstans;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.GeoDistanceSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;
/**
* Created by wangnan on 2016/8/27.
*/
@Service
public class SearchServiceHelper {
/**
* 构造距离查询范围
*
* @param paramMap
* @return
*/
public QueryBuilder constructGeoDistanceRangeQueryBuilder(Map<String, String> paramMap) {
if (paramMap.get("range_start") != null && paramMap.get("range_end") != null) {
QueryBuilder builder = new GeoDistanceRangeQueryBuilder("pin.location")
.point(Double.valueOf(paramMap.get("latitude")), Double.valueOf(paramMap.get("longitude")))//注意纬度在前,经度在后
.from(paramMap.get("range_start") + "m")
.to(paramMap.get("range_end") + "m")
.includeLower(true)
.includeUpper(false)
.optimizeBbox("memory")
.geoDistance(GeoDistance.PLANE);
return builder;
} else {
QueryBuilder builder = new GeoDistanceRangeQueryBuilder("pin.location")
.point(Double.valueOf(paramMap.get("latitude")), Double.valueOf(paramMap.get("longitude")))//注意纬度在前,经度在后
.includeLower(true)
.includeUpper(false)
.optimizeBbox("memory")
.geoDistance(GeoDistance.PLANE);
return builder;
}
}
/**
* 构造距离查询排序
*
* @param paramMap
* @return
*/
public GeoDistanceSortBuilder constructGeoDistanceSortBuilder(Map<String, String> paramMap) {
GeoDistanceSortBuilder sort = new GeoDistanceSortBuilder("pin.location");
sort.unit(DistanceUnit.METERS);//距离单位米
sort.order(SortOrder.ASC);
if (StringUtils.isNotBlank(paramMap.get("distance_order"))) {
if (paramMap.get("distance_order").equals("desc")) {
sort.order(SortOrder.DESC);
}
}
sort.point(Double.valueOf(paramMap.get("latitude")), Double.valueOf(paramMap.get("longitude")));//注意纬度在前,经度在后
return sort;
}
/**
* 构造Query
*
* @param paramMap
* @return
*/
public QueryBuilder constructQueryBuilder(Map<String, String> paramMap, String indexName, boolean isSuggest) {
String keyword = paramMap.get("query");
//如果query参数为空,则设置为matchAllQuery
if (StringUtils.isBlank(keyword)) {
return QueryBuilders.matchAllQuery();
}
if (isSuggest) {
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("suggest", "*" + keyword + "*");
return wildcardQueryBuilder;
} else {
//构建MultiMatch
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
//获取MultiMatchQueryType
MultiMatchQueryBuilder.Type multiMatchQueryBuilderType = this.getMultiMatchQueryBuilderType();
if (multiMatchQueryBuilderType != null) {
queryBuilder.type(multiMatchQueryBuilderType);
}
if (paramMap.containsKey("queryField") && StringUtils.isNotBlank(paramMap.get("queryField"))) {
String[] fields = paramMap.get("queryField").split(",");
for (int i = 0; i < fields.length; i++) {
queryBuilder.field(fields[i]);
}
} else {
//设置searchfield和权重
this.setDefaultSearchField(queryBuilder, indexName);
}
//设置Operator
if (ISearchConstans.SEARCH_OPERATOR.equalsIgnoreCase("or")) {
queryBuilder.operator(MatchQueryBuilder.Operator.OR);
queryBuilder.minimumShouldMatch(ISearchConstans.SEARCH_MINIMUM_SHOULD_MATCH);
} else {
queryBuilder.operator(MatchQueryBuilder.Operator.AND);
}
return queryBuilder;
}
}
/**
* 构造Filter
*
* @param paramMap
* @return
* @throws Exception
*/
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) throws Exception {
//创建BoolQueryBuilder
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
//添加过滤请求
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ID);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_CITYID);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ISTOP);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ISRECOMMEND);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_SORT);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_BIZID);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ORDERBY);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_SCORE);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ASSESSMENTNUM);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_TAGS);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_CONSUMPTION);
//boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_LATITUDE);
//boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_LONGITUDE);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_PUBLISHTIME);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_USERTYPE);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_USERAUTH);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_ISDELETED);
boolFilter = BoolQueryBuilder(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_UID);
boolFilter = BoolQueryBuilderGte(paramMap, boolFilter, ISearchConstans.PARAM_SEARCH_STORENUM);
// 通用的过滤请求
for (String key : paramMap.keySet()) {
if (key.startsWith("not_")) {
String[] values = paramMap.get(key).split(",");
boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
}
if (key.startsWith("filter_")) {
String field = key.substring(7, key.length());
boolFilter.must(QueryBuilders.termQuery(field, paramMap.get(key).split(",")));
}
if (key.startsWith("missing_")) {
String field = key.substring(8, key.length());
boolFilter.mustNot(QueryBuilders.existsQuery(field));
}
}
return boolFilter;
}
/**
* 构造Sort 格式: order=shelve_time:desc
*
* @param paramMap
* @return
*/
public LinkedHashMap<String, String> buildSort(Map<String, String> paramMap) {
LinkedHashMap<String, String> sortFields = new LinkedHashMap<String, String>();
if (StringUtils.isNotBlank(paramMap.get("order"))) {
String[] sortTypes = paramMap.get("order").split(",");
for (String sortType : sortTypes) {
if (sortType.split(ISearchConstans.SPLIT_CHAR_COLON).length != 2) {
throw new IllegalArgumentException("order参数格式不正确,请检查");
}
String field = sortType.split(ISearchConstans.SPLIT_CHAR_COLON)[0];
String type = sortType.split(ISearchConstans.SPLIT_CHAR_COLON)[1];
sortFields.put(field, type);
}
}
return sortFields;
}
/**
* 判断MultiMatchQueryBuilder的类型
*
* @return
*/
private MultiMatchQueryBuilder.Type getMultiMatchQueryBuilderType() {
String configMultiMatchQueryType = ISearchConstans.SEARCH_MULTIMATCHQUERY_TYPE;
if (configMultiMatchQueryType.equalsIgnoreCase(MultiMatchQueryBuilder.Type.BEST_FIELDS.name())) {
return MultiMatchQueryBuilder.Type.BEST_FIELDS;
}
if (configMultiMatchQueryType.equalsIgnoreCase(MultiMatchQueryBuilder.Type.MOST_FIELDS.name())) {
return MultiMatchQueryBuilder.Type.MOST_FIELDS;
}
if (configMultiMatchQueryType.equalsIgnoreCase(MultiMatchQueryBuilder.Type.CROSS_FIELDS.name())) {
return MultiMatchQueryBuilder.Type.CROSS_FIELDS;
}
if (configMultiMatchQueryType.equalsIgnoreCase(MultiMatchQueryBuilder.Type.PHRASE.name())) {
return MultiMatchQueryBuilder.Type.PHRASE;
}
if (configMultiMatchQueryType.equalsIgnoreCase(MultiMatchQueryBuilder.Type.PHRASE_PREFIX.name())) {
return MultiMatchQueryBuilder.Type.PHRASE_PREFIX;
}
return null;
}
/**
* 设置默认搜索字段(字段+权重)
*
* @param queryBuilder
*/
private void setDefaultSearchField(MultiMatchQueryBuilder queryBuilder, String indexName) {
List<String> fields = new ArrayList<>();
if (indexName.equals(ISearchConstans.INDEX_NAME_BIZAREA)) {
fields = ISearchConstans.SEARCH_DEFAULT_FIELD_BIZAREA;
}
if (indexName.equals(ISearchConstans.INDEX_NAME_COMMENT)) {
fields = ISearchConstans.SEARCH_DEFAULT_FIELD_COMMENT;
}
if (indexName.equals(ISearchConstans.INDEX_NAME_LINE)) {
fields = ISearchConstans.SEARCH_DEFAULT_FIELD_LINE;
}
if (indexName.equals(ISearchConstans.INDEX_NAME_STORE)) {
fields = ISearchConstans.SEARCH_DEFAULT_FIELD_STORE;
}
if (indexName.equals(ISearchConstans.INDEX_NAME_TOPIC)) {
fields = ISearchConstans.SEARCH_DEFAULT_FIELD_TOPIC;
}
for (String field : fields) {
String[] fieldBoost = field.split("^");
if (fieldBoost.length == 2) {
queryBuilder.field(fieldBoost[0], Float.parseFloat(fieldBoost[1]));
} else if (fieldBoost.length == 1) {
queryBuilder.field(fieldBoost[0]);
}
}
}
/**
* 增加一个筛选项
*
* @param paramMap
* @param boolFilter
* @param fieldName
* @return
*/
private BoolQueryBuilder BoolQueryBuilder(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
boolFilter.must(QueryBuilders.termsQuery(fieldName, paramMap.get(fieldName)));
}
return boolFilter;
}
/**
* 增加一个筛选项,筛选大于某个数值的项
*
* @param paramMap
* @param boolFilter
* @param fieldName
* @return
*/
private BoolQueryBuilder BoolQueryBuilderGte(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
int begin = Integer.parseInt(paramMap.get(fieldName));
if (begin == 0) {
boolFilter.must(QueryBuilders.termQuery(fieldName, begin));
} else {
boolFilter.must(QueryBuilders.rangeQuery(fieldName).gte(begin));
}
}
return boolFilter;
}
/**
* 获取返回结果
*
* @param resultList
* @return
*/
// public List<Map<String, Map<String, Object>>> getMapList(Map<String, String> paramMap, List<Map<String, Object>> resultList, boolean isSuggest) {
// List<Map<String, Map<String, Object>>> returnList = new ArrayList<Map<String, Map<String, Object>>>();
// Map<String, Map<String, Object>> dataMap = new HashMap<>();
// for (Map<String, Object> map : resultList) {
// if (isSuggest) {
// dataMap.put(map.get("id").toString(), getSuggestMap(map));
// } else if (paramMap.containsKey("returnField") && StringUtils.isNotBlank(paramMap.get("returnField"))) {
// dataMap.put(map.get("id").toString(), getReturnMap(map, paramMap));
// } else {
// dataMap.put(map.get("id").toString(), map);
// }
// }
// returnList.add(dataMap);
// return returnList;
// }
/**
* 获取商品列表返回结果
*
* @param resultList
* @return
*/
public List<Map<String, Object>> getMapList(Map<String, String> paramMap, List<Map<String, Object>> resultList, boolean isSuggest) {
List<Map<String, Object>> pageList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> map : resultList) {
if (isSuggest) {
pageList.add(getSuggestMap(map));
} else if (paramMap.containsKey("returnField") && StringUtils.isNotBlank(paramMap.get("returnField"))) {
pageList.add(getReturnMap(map, paramMap));
} else {
pageList.add(map);
}
}
return pageList;
}
/**
* 拼装返回结果
*
* @param map
* @return
*/
private Map<String, Object> getSuggestMap(Map<String, Object> map) {
Map<String, Object> dataMap = new HashMap<String, Object>();
//dataMap.put("id", map.get("id") == null ? "" : map.get("id"));
// dataMap.put("city_id", map.get("city_id") == null ? "" : map.get("city_id"));
// dataMap.put("is_top", map.get("is_top") == null ? "" : map.get("is_top"));
// dataMap.put("store_num", map.get("store_num") == null ? "" : map.get("store_num"));
// dataMap.put("sort", map.get("sort") == null ? "" : map.get("sort"));
// dataMap.put("name", map.get("name") == null ? "" : map.get("name"));
// dataMap.put("english_name", map.get("english_name") == null ? "" : map.get("english_name"));
// dataMap.put("name_tc", map.get("name_tc") == null ? "" : map.get("name_tc"));
// dataMap.put("des", map.get("des") == null ? "" : map.get("des"));
dataMap.put("suggest", map.get("suggest") == null ? "" : map.get("suggest"));
return dataMap;
}
/**
* 拼装返回结果
*
* @param map
* @return
*/
private Map<String, Object> getReturnMap(Map<String, Object> map, Map<String, String> paramMap) {
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
if (paramMap.containsKey("returnField") && StringUtils.isNotBlank(paramMap.get("returnField"))) {
dataMap.put("id", map.get("id") == null ? "" : map.get("id"));
String[] fields = paramMap.get("returnField").split(",");
for (int i = 0; i < fields.length; i++) {
dataMap.put(fields[i], map.get(fields[i]) == null ? "" : map.get(fields[i]));
}
return dataMap;
}
return dataMap;
}
public static void main(String[] args) throws IOException {
String jsonData = null;
XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
com.yoho.search.dal.model.Store store = new com.yoho.search.dal.model.Store();
store.setId(123);
store.setStoreName("name");
store.setLatitude(123.2);
store.setLongitude(321.1);
jsonBuild.startObject().field("id", store.getId()).field("name", store.getStoreName()).startArray("location").value(store.getLatitude()).value(store.getLongitude()).endArray()
.endObject();
jsonData = jsonBuild.string();
System.out.println(jsonData);
}
}