1
|
package com.yoho.search.common;
|
1
|
package com.yoho.search.common;
|
2
|
|
2
|
|
3
|
-import java.util.Map;
|
|
|
4
|
-
|
3
|
+import com.yoho.search.base.utils.ConvertUtils;
|
5
|
import org.apache.commons.lang.StringUtils;
|
4
|
import org.apache.commons.lang.StringUtils;
|
6
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
5
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
7
|
import org.elasticsearch.index.query.QueryBuilders;
|
6
|
import org.elasticsearch.index.query.QueryBuilders;
|
8
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
9
|
|
7
|
|
10
|
-import com.yoho.search.base.utils.ConvertUtils;
|
|
|
11
|
-import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
12
|
-import com.yoho.search.service.helper.SearchSortHelper;
|
8
|
+import java.util.List;
|
|
|
9
|
+import java.util.Map;
|
13
|
|
10
|
|
14
|
/**
|
11
|
/**
|
15
|
* Created by wangnan on 2016/12/16.
|
12
|
* Created by wangnan on 2016/12/16.
|
16
|
*/
|
13
|
*/
|
17
|
public class BaseService {
|
14
|
public class BaseService {
|
18
|
|
15
|
|
19
|
- /**
|
|
|
20
|
- * 增加一个termsQuery
|
|
|
21
|
- */
|
|
|
22
|
- protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
|
|
|
23
|
- if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
|
|
|
24
|
- int[] values = ConvertUtils.stringToIntArray(paramMap.get(fieldName), ",");
|
|
|
25
|
- boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
|
|
|
26
|
- }
|
|
|
27
|
- return boolFilter;
|
|
|
28
|
- }
|
16
|
+ /**
|
|
|
17
|
+ * 增加一个termsQuery
|
|
|
18
|
+ */
|
|
|
19
|
+ protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
|
|
|
20
|
+ if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
|
|
|
21
|
+ List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ",");
|
|
|
22
|
+ if (values == null || values.isEmpty()) {
|
|
|
23
|
+ return boolFilter;
|
|
|
24
|
+ }
|
|
|
25
|
+ boolFilter.must(QueryBuilders.termsQuery(fieldName, values));
|
|
|
26
|
+ }
|
|
|
27
|
+ return boolFilter;
|
|
|
28
|
+ }
|
29
|
|
29
|
|
30
|
- /**
|
|
|
31
|
- * 增加一个termQuery
|
|
|
32
|
- */
|
|
|
33
|
- protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
|
|
|
34
|
- if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
|
|
|
35
|
- boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
|
|
|
36
|
- }
|
|
|
37
|
- return boolFilter;
|
|
|
38
|
- }
|
30
|
+ /**
|
|
|
31
|
+ * 增加一个termQuery
|
|
|
32
|
+ */
|
|
|
33
|
+ protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) {
|
|
|
34
|
+ if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) {
|
|
|
35
|
+ boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName)));
|
|
|
36
|
+ }
|
|
|
37
|
+ return boolFilter;
|
|
|
38
|
+ }
|
39
|
|
39
|
|
40
|
- /**
|
|
|
41
|
- * 通用的过滤请求
|
|
|
42
|
- */
|
|
|
43
|
- protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {
|
|
|
44
|
- for (String key : paramMap.keySet()) {
|
|
|
45
|
- if (key.startsWith("not_")) {
|
|
|
46
|
- String[] values = paramMap.get(key).split(",");
|
|
|
47
|
- boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
|
|
|
48
|
- }
|
|
|
49
|
- if (key.startsWith("filter_")) {
|
|
|
50
|
- String field = key.substring(7, key.length());
|
|
|
51
|
- boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));
|
|
|
52
|
- }
|
|
|
53
|
- if (key.startsWith("missing_")) {
|
|
|
54
|
- String field = key.substring(8, key.length());
|
|
|
55
|
- boolFilter.mustNot(QueryBuilders.existsQuery(field));
|
|
|
56
|
- }
|
|
|
57
|
- }
|
|
|
58
|
- if (boolFilter.hasClauses()) {
|
|
|
59
|
- return boolFilter;
|
|
|
60
|
- } else {
|
|
|
61
|
- return null;
|
|
|
62
|
- }
|
|
|
63
|
- }
|
40
|
+ /**
|
|
|
41
|
+ * 通用的过滤请求
|
|
|
42
|
+ */
|
|
|
43
|
+ protected BoolQueryBuilder generalHandler(Map<String, String> paramMap, BoolQueryBuilder boolFilter) {
|
|
|
44
|
+ for (String key : paramMap.keySet()) {
|
|
|
45
|
+ if (key.startsWith("not_")) {
|
|
|
46
|
+ String[] values = paramMap.get(key).split(",");
|
|
|
47
|
+ boolFilter.mustNot(QueryBuilders.termsQuery(key.substring(4, key.length()), values));
|
|
|
48
|
+ }
|
|
|
49
|
+ if (key.startsWith("filter_")) {
|
|
|
50
|
+ String field = key.substring(7, key.length());
|
|
|
51
|
+ boolFilter.must(QueryBuilders.termsQuery(field, paramMap.get(key).split(",")));
|
|
|
52
|
+ }
|
|
|
53
|
+ if (key.startsWith("missing_")) {
|
|
|
54
|
+ String field = key.substring(8, key.length());
|
|
|
55
|
+ boolFilter.mustNot(QueryBuilders.existsQuery(field));
|
|
|
56
|
+ }
|
|
|
57
|
+ }
|
|
|
58
|
+ if (boolFilter.hasClauses()) {
|
|
|
59
|
+ return boolFilter;
|
|
|
60
|
+ } else {
|
|
|
61
|
+ return null;
|
|
|
62
|
+ }
|
|
|
63
|
+ }
|
64
|
|
64
|
|
65
|
- protected int getPage(Map<String, String> paramMap) {
|
|
|
66
|
- int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
67
|
- return page;
|
|
|
68
|
- }
|
65
|
+ protected int getPage(Map<String, String> paramMap) {
|
|
|
66
|
+ int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
67
|
+ return page;
|
|
|
68
|
+ }
|
69
|
} |
69
|
} |