震惊 居然 elasticsearch must mustnot should 构建工具类
2017-11-28 18:14
591 查看
package builder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.search.sort.SortOrder; import java.util.Map; /** * @Author: yang * @Date: 2017/11/28.13:35 */ public class BooleanCondtionBuilder { // and private Map<String, String> queryMap; // and private Map<String, String> mustMap; // or private Map<String, String> mustNotMap; // not private Map<String, String> shouldMap; // paixu private Map<String, SortOrder> sortMap; private BoolQueryBuilder boolQueryBuilder = null; private int start = 0; private int row; public Map<String, String> getMustMap() { return mustMap; } public Map<String, String> getMustNotMap() { return mustNotMap; } public Map<String, String> getShouldMap() { return shouldMap; } public Map<String, SortOrder> getSortMap() { return sortMap; } public BoolQueryBuilder getBoolQueryBuilder() { return boolQueryBuilder; } public int getStart() { return start; } public int getRow() { return row; } public BooleanCondtionBuilder deal() { boolQueryBuilder = QueryBuilders.boolQuery(); if (this.queryMap != null && this.queryMap.size() > 0) { BoolQueryBuilder boolQueryBuilder = null; boolQueryBuilder = QueryBuilders.boolQuery(); for (Map.Entry<String, String> entry : this.queryMap.entrySet()) { QueryStringQueryBuilder queryFilterBuilder = new QueryStringQueryBuilder(entry.getValue()); queryFilterBuilder.defaultField(entry.getKey()); boolQueryBuilder.should(queryFilterBuilder); } this.boolQueryBuilder.must(boolQueryBuilder); } if (this.mustMap != null && this.mustMap.size() > 0) { BoolQueryBuilder mustTagsBuilder = QueryBuilders.boolQuery(); for (Map.Entry<String, String> entry : this.mustMap.entrySet()) { mustTagsBuilder.must(QueryBuilders.termQuery(entry.getKey(), entry.getValue())); } this.boolQueryBuilder.must(mustTagsBuilder); } if (this.mustNotMap != null && this.mustNotMap.size() > 0) { BoolQueryBuilder mustNotTagsBuilder = QueryBuilders.boolQuery(); for (Map.Entry<String, String> entry : this.m a5dc ustNotMap.entrySet()) { mustNotTagsBuilder.mustNot(QueryBuilders.termQuery(entry.getKey(), entry.getValue())); } this.boolQueryBuilder.must(mustNotTagsBuilder); } if (this.shouldMap != null && this.shouldMap.size() > 0) { BoolQueryBuilder shoudlTagsBuilder = QueryBuilders.boolQuery(); for (Map.Entry<String, String> entry : this.shouldMap.entrySet()) { shoudlTagsBuilder.should(QueryBuilders.termQuery(entry.getKey(), entry.getValue())); } this.boolQueryBuilder.must(shoudlTagsBuilder); } return this; } public static class Builder { private BooleanCondtionBuilder condtionBuilder = new BooleanCondtionBuilder(); public Builder setSortMap(Map<String, SortOrder> sortMap) { condtionBuilder.sortMap = sortMap; return this; } public Builder setMustMap(Map<String, String> mustMap) { condtionBuilder.mustMap = mustMap; return this; } public Builder setQueryMap(Map<String, String> queryMap) { condtionBuilder.queryMap = queryMap; return this; } public Builder setMustNotMap(Map<String, String> mustNotMap) { condtionBuilder.mustNotMap = mustNotMap; return this; } public Builder setShouldMap(Map<String, String> shouldMap) { condtionBuilder.shouldMap = shouldMap; return this; } public Builder setStart(int start) { condtionBuilder.start = start; return this; } public Builder setRow(int row) { condtionBuilder.row = row; return this; } public BooleanCondtionBuilder builder() { return condtionBuilder.deal(); } } }调用方式:
Map<String, String> mustMap = new HashMap<>();
// queryMap 存的是你要查的字段和值
Map<String, String> queryMap = new HashMap<>();
// mustMap.put("userId", "1");
mustMap.put("brandId", "1");
mustMap.put("userId", "1");
mustMap.put("systemType", "1");
mustMap.put("isDelete", "0");
mustMap.put("sellStatus", "1");
queryMap.put("productName.pinyin", "商品");
BooleanCondtionBuilder booleanCondtionBuilder = new BooleanCondtionBuilder.Builder().setMustMap(mustMap)
.setQueryMap(queryMap)
.builder(); 返回的是
BooleanCondtionBuilder booleanCondtionBuilder 对象 这个对象可以直接用于以下代码SearchRequestBuilder searchRequestBuilder = client.prepareSearch(indexName);
searchRequestBuilder.setTypes(typeName);
// 设置是否按查询匹配度排序
searchRequestBuilder.setExplain(true);
// 设置查询类型
searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
//设置query
searchRequestBuilder.setQuery(booleanCondtionBuilder);
相关文章推荐
- Could not find the main class: org.elasticsearch.bootstrap.Elasticsearch. Program will exit.
- Elasticsearch:Elasticsearch基础上构建推荐引擎 资料收集
- ES Head is not working with elasticsearch-1.4.0.Beta1
- elasticsearch之modeling your data(not flat)--nested objects
- Could not find the main class: org.elasticsearch.bootstrap.Elasticsearch. Program will exit.
- org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: too_many_clauses: maxClauseCount
- Elasticsearch Exception:Message not fully read (response) for requestId
- run as service Elasticsearch Not Working
- Spark Streaming + Elasticsearch构建App异常监控平台
- ElasticSearch Java API中的and、or和not
- elasticsearch this is not a http port
- spark连接es的问题java.lang.ClassNotFoundException:org.elasticsearch.client.transport.NoNodeAvailableExcep
- elasticsearch class file for java.lang.AutoCloseable not found
- Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.threadpool.ThreadPool
- SparkSQL read Elasticsearch ClassNotFoundException
- must,must_not,should组合关系以及OR和AND
- Elasticsearch java API (16)Aggregations 构建聚合
- Ubuntu中SonarQube6.7 启动报错:can not run elasticsearch as root
- Spring Boot + Elasticsearch 实现索引批量写入
- QuickMark: ElasticSearch curl command