/** * 获取过滤器 * * @return */ public BoolFilterBuilder makeFilterBuilder(SearchRequestBuilder srb) { // 过滤器 BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter(); if (request.getSafeFilterScript().size() > 0) { ScriptFilterBuilder filter; for (EsFilterScript filterScript : request.getSafeFilterScript()) { filter = FilterBuilders.scriptFilter(filterScript.getScript()); if (filterScript.getScriptParams() != null && filterScript.getScriptParams().size() > 0) { filter.params(filterScript.getScriptParams()); } boolFilterBuilder.should(filter); } srb.setPostFilter(boolFilterBuilder); } return boolFilterBuilder; }
protected void addFilters(BoolFilterBuilder boolFilter) { Map<String, Filter> filters = query.getFilters(); if (filters != null) { for (Filter filter : filters.values()) { FilterBuilder filterBuilder = converterFilter(filter); if (filterBuilder != null) { switch (filter.getClause()) { case SHOULD: boolFilter.should(filterBuilder); break; case MUST_NOT: boolFilter.mustNot(filterBuilder); break; case MUST: default: boolFilter.must(filterBuilder); } } } } }
private static void addFilterForTopicId( BoolFilterBuilder filterBuilder, InclusionExclusionId id ) { NestedFilterBuilder filterForId = FilterBuilders.nestedFilter( EsContent.TOPICS + "." + EsTopic.TYPE_NAME, FilterBuilders.termFilter( EsContent.TOPICS + "." + EsTopic.TYPE_NAME + "." + EsContent.ID, id.getId() ) ); if (id.isExcluded()) { filterBuilder.mustNot(filterForId); } else { filterBuilder.must(filterForId); } }
private void explanWhere(BoolFilterBuilder boolFilter, Where where) throws SqlParseException { while (where.getWheres().size() == 1) { where = where.getWheres().getFirst(); } if (where instanceof Condition) { addSubFilter(boolFilter, where, (BaseFilterBuilder) make((Condition) where)); } else { BoolFilterBuilder subFilter = FilterBuilders.boolFilter(); addSubFilter(boolFilter, where, subFilter); for (Where subWhere : where.getWheres()) { explanWhere(subFilter, subWhere); } } }
/** * 增加嵌套插 * * @param boolFilter * @param where * @param subFilter */ private void addSubFilter(BoolFilterBuilder boolFilter, Where where, BaseFilterBuilder subFilter) { if (where.getConn() == CONN.AND) { boolFilter.must(subFilter); } else { boolFilter.should(subFilter); } }
/** * Create filters based on * the Where clause. * @param where the 'WHERE' part of the SQL query. * @throws SqlParseException */ private void setWhere(Where where) throws SqlParseException { if (where != null) { BoolFilterBuilder boolFilter = FilterMaker.explan(where); request.setQuery(QueryBuilders.filteredQuery(null, boolFilter)); } }
/** * Create filters or queries based on * the Where clause. * @param where the 'WHERE' part of the SQL query. * @throws SqlParseException */ private void setWhere(Where where) throws SqlParseException { if (where != null) { if (select.isQuery) { BoolQueryBuilder boolQuery = QueryMaker.explan(where); request.setQuery(boolQuery); } else { BoolFilterBuilder boolFilter = FilterMaker.explan(where); request.setQuery(QueryBuilders.filteredQuery(null, boolFilter)); } } }
/** * Create filters based on * the Where clause. * * @param where the 'WHERE' part of the SQL query. * @throws SqlParseException */ private void setWhere(Where where) throws SqlParseException { if (where != null) { BoolFilterBuilder boolFilter = FilterMaker.explan(where); request.setQuery(QueryBuilders.filteredQuery(null, boolFilter)); } else { request.setQuery(QueryBuilders.matchAllQuery()); } }
protected void addQ(SearchRequestBuilder builder, BoolFilterBuilder boolFilter) { if (types.length > 0) { builder.setTypes(types); } if (query.getQ() != null && !query.getQ().isEmpty()) { boolFilter.must(queryFilter(queryString(query.getQ()).defaultOperator(Operator.AND))); } }
protected void addQ(DeleteByQueryRequestBuilder builder, BoolFilterBuilder boolFilter) { if (types.length > 0) { builder.setTypes(types); } if (query.getQ() != null && !query.getQ().isEmpty()) { boolFilter.must(queryFilter(queryString(query.getQ()).defaultOperator(Operator.AND))); } }
protected void addBbox(BoolFilterBuilder boolFilter) { Envelope bbox = query.getBbox(); String geoField = query.getGeoField(); if ((geoField != null && !geoField.isEmpty()) && (bbox != null)) { boolFilter.must(geoBoundingBoxFilter(geoField).bottomLeft(bbox.getMinY(), bbox.getMinX()).topRight(bbox.getMaxY(), bbox.getMaxX())); } }
@Override public SearchRequestBuilder searchRequest() { SearchRequestBuilder builder = client.prepareSearch(indices); builder.setSearchType(SearchType.DEFAULT); BoolFilterBuilder boolFilter = boolFilter(); addPaging(builder); addQ(builder, boolFilter); addFilters(boolFilter); addBbox(boolFilter); setFilter(builder, boolFilter); addAggregations(builder); addSorts(builder); addFields(builder); return builder; }
@Override public DeleteByQueryRequestBuilder deleteRequest() { DeleteByQueryRequestBuilder builder = client.prepareDeleteByQuery(indices); BoolFilterBuilder boolFilter = boolFilter(); addQ(builder, boolFilter); addFilters(boolFilter); addBbox(boolFilter); setFilter(builder, boolFilter); return builder; }
public FilterBuilder aclFilter(Map<String, Repository> repoMap) { if (repoMap.isEmpty()) { return boolFilter().mustNot(matchAllFilter()); } // Compute cryptographic hash of repository set to use for cache key String[] projectRepoPairs = repoMap.keySet().toArray(new String[repoMap.size()]); Arrays.sort(projectRepoPairs); String filterHash; try { MessageDigest hasher = MessageDigest.getInstance("SHA-256"); for (String pair : projectRepoPairs) { hasher.update(pair.getBytes()); hasher.update((byte) 0); } filterHash = new String(Base64.encodeBase64(hasher.digest())); } catch (Exception e) { filterHash = null; log.error("Caught exception generating ACL hash -- caching is disabled.", e); } // Create disjunction of individual repo ACL filters BoolFilterBuilder filter = boolFilter(); if (filterHash != null) { filter.cache(true) .cacheKey("CACHE^ACLORFILTER^" + filterHash); } else { filter.cache(false); } for (Repository repo : repoMap.values()) { filter.should(projectRepositoryFilter(repo.getProject().getKey(), repo.getSlug())); } return filter; }
public FilterBuilder projectFilter(Iterable<String> projects) { boolean filterAdded = false; BoolFilterBuilder filter = boolFilter(); for (String project : projects) { project = project.trim(); if (project.isEmpty()) { continue; } filter.should(termFilter("project", project) .cache(true) .cacheKey("CACHE^PROJECTFILTER^" + project)); filterAdded = true; } return filterAdded ? filter : matchAllFilter(); }
public FilterBuilder repositoryFilter(Iterable<String> repositories) { boolean filterAdded = false; BoolFilterBuilder filter = boolFilter(); for (String repository : repositories) { repository = repository.trim(); if (repository.isEmpty()) { continue; } filter.should(termFilter("repository", repository) .cache(true) .cacheKey("CACHE^REPOFILTER^" + repository)); filterAdded = true; } return filterAdded ? filter : matchAllFilter(); }
public FilterBuilder extensionFilter(Iterable<String> extensions) { boolean filterAdded = false; BoolFilterBuilder filter = boolFilter(); for (String extension : extensions) { extension = extension.trim(); if (extension.isEmpty()) { continue; } filter.should(termFilter("extension", extension) .cache(true) .cacheKey("CACHE^EXTENSIONFILTER^" + extension)); filterAdded = true; } return filterAdded ? filter.should(typeFilter("commit")) : matchAllFilter(); }
public SearchSourceBuilder createFrequentlyRelatedContentSearch(RelatedItemSearch search) { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); String id = search.getRelatedItemId(); BoolFilterBuilder b = FilterBuilders.boolFilter(); b.must(FilterBuilders.termFilter(relatedWithAttribute, id)); RelatedItemAdditionalProperties searchProps = search.getAdditionalSearchCriteria(); int numberOfProps = searchProps.getNumberOfProperties(); for(int i = 0;i<numberOfProps;i++) { b.must(FilterBuilders.termFilter(searchProps.getPropertyName(i), searchProps.getPropertyValue(i))); } ConstantScoreQueryBuilder cs = QueryBuilders.constantScoreQuery(b); TermsFacetBuilder facetBuilder = FacetBuilders.termsFacet(facetResultName).field(itemIdentifierAttribute).size(search.getMaxResults()); if(hasExecutionHint) { facetBuilder.executionHint(executionHint); } sourceBuilder.size(0); sourceBuilder.query(cs); sourceBuilder.timeout(searchTimeoutValue); sourceBuilder.facet(facetBuilder); log.debug("Frequently Related Query Built: {}",sourceBuilder); return sourceBuilder; }
private void addBrandId( IndexQueryParams queryParams, BoolFilterBuilder filterBuilder ) { if (queryParams.getBrandId().isPresent()) { filterBuilder.must( FiltersBuilder.getBrandIdFilter( queryParams.getBrandId().get(), equivIdIndex ) ); } }
private void addSeriesId( IndexQueryParams queryParams, BoolFilterBuilder filterBuilder ) { if (queryParams.getSeriesId().isPresent()) { filterBuilder.must( FiltersBuilder.getSeriesIdFilter( queryParams.getSeriesId().get(), equivIdIndex ) ); } }
private void addTopicFilter( IndexQueryParams queryParams, BoolFilterBuilder filterBuilder ) { if (queryParams.getTopicFilterIds().isPresent()) { filterBuilder.must( FiltersBuilder.buildTopicIdFilter(queryParams.getTopicFilterIds().get()) ); } }
private void addActionableFilter( IndexQueryParams queryParams, BoolFilterBuilder filterBuilder ) { if (queryParams.getActionableFilterParams().isPresent()) { Optional<Id> maybeRegionId = queryParams.getRegionId(); FilterBuilder actionableFilter = FiltersBuilder.buildActionableFilter( queryParams.getActionableFilterParams().get(), maybeRegionId, channelGroupResolver ); filterBuilder.must(actionableFilter); } }
public static FilterBuilder buildTopicIdFilter( ImmutableList<ImmutableList<InclusionExclusionId>> topicIdSets ) { ImmutableList.Builder<FilterBuilder> topicIdFilters = ImmutableList.builder(); for (List<InclusionExclusionId> idSet : topicIdSets) { BoolFilterBuilder filterForThisSet = FilterBuilders.boolFilter(); for (InclusionExclusionId id : idSet) { addFilterForTopicId(filterForThisSet, id); } topicIdFilters.add(filterForThisSet); } OrFilterBuilder orFilter = FilterBuilders.orFilter(); topicIdFilters.build().forEach(orFilter::add); return orFilter; }
/** * This method prepares a Boolean filter builder with respective facet selections from Resource request object. * * @param request the request * @return the bool filter builder */ private BoolFilterBuilder prepareBoolFilterBuilder(ResourceRequest request) { /**Set a filter to get suggestions with Status Approved (0) */ BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter() .must(FilterBuilders.termFilter(ElasticsearchPortletConstants.STATUS, WorkflowConstants.STATUS_APPROVED)); /** Iterate over Suggestion excluded index types and add to Boolfilter. Since these are excluded, mustNot filter should be used */ for (Iterator<String> iterator = propsValues.getSuggestionExcludedTypes().iterator(); iterator.hasNext();) { boolFilterBuilder.mustNot(FilterBuilders.typeFilter(iterator.next())); } /** Process facet selections and apply appropriate filters here Apply UserId filter */ long userId = ParamUtil.getLong(request, WorkflowConstants.CONTEXT_USER_ID); if(userId > ElasticsearchPortletConstants.INTEGER_ZERO_VALUE) { boolFilterBuilder.must(FilterBuilders.termFilter(WorkflowConstants.CONTEXT_USER_ID, userId)); } /** Apply modified filter */ String selectedRange = ParamUtil.getString(request, ElasticsearchPortletConstants.FILTER_MODIFIED); if(Validator.isNotNull(selectedRange) && !selectedRange.isEmpty()){ String[] rangeArray = fetchFromToValuesInRage(selectedRange); boolFilterBuilder.must(FilterBuilders.rangeFilter(ElasticsearchPortletConstants.FILTER_MODIFIED_DATE) .from(rangeArray[0].trim()).to(rangeArray[1].trim())); } /** Apply AssetCategoryIds filter */ long assetCategoryIds = ParamUtil.getLong(request, ElasticsearchPortletConstants.FILTER_ASSET_CATEGORY); if(assetCategoryIds > ElasticsearchPortletConstants.INTEGER_ZERO_VALUE) { boolFilterBuilder.must(FilterBuilders.termFilter(ElasticsearchPortletConstants.FILTER_ASSET_CATEGORY, assetCategoryIds)); } /** Apply FolderId filter */ long folderId = ParamUtil.getLong(request, ElasticsearchPortletConstants.FILTER_FOLDERID); if(folderId > ElasticsearchPortletConstants.INTEGER_ZERO_VALUE) { boolFilterBuilder.must(FilterBuilders.termFilter(ElasticsearchPortletConstants.FILTER_FOLDERID, folderId)); } /** Apply Site id filter */ long groupId = ParamUtil.getLong(request, WorkflowConstants.CONTEXT_GROUP_ID); if(groupId != WorkflowConstants.DEFAULT_GROUP_ID){ boolFilterBuilder.must(FilterBuilders.termFilter(ElasticsearchPortletConstants.FILTER_SCOPE_GROUPID, groupId)) .must(FilterBuilders.termFilter(WorkflowConstants.CONTEXT_GROUP_ID, groupId)); } /** Entryclassname is a special case since object is directly mapped to Index type in Elasticsearch. So instead of applying a filter, we use respective Entryclassname type */ String selectedClassName = ParamUtil.getString(request, ElasticsearchPortletConstants.ENTRY_CLASSNAME); if(Validator.isNotNull(selectedClassName)){ /** Convert selectedClassName to index type by replacing . with _*/ selectedClassName = selectedClassName.replace(StringPool.PERIOD, StringPool.UNDERLINE); boolFilterBuilder.must(FilterBuilders.typeFilter(selectedClassName)); } return boolFilterBuilder; }
protected void setFilter(SearchRequestBuilder builder, BoolFilterBuilder boolFilter) { if (boolFilter.hasClauses()) { builder.setQuery(filteredQuery(matchAllQuery(), boolFilter)); } }
protected void setFilter(DeleteByQueryRequestBuilder builder, BoolFilterBuilder boolFilter) { if (boolFilter.hasClauses()) { builder.setQuery(filteredQuery(matchAllQuery(), boolFilter)); } }
/** * Create our filter builder. We need to restrict our results on edge search, as well as on types, and any filters * that came from the grammar. */ private FilterBuilder createFilterBuilder( final SearchEdge searchEdge, final QueryVisitor visitor, final SearchTypes searchTypes ) { String context = createContextName( applicationScope, searchEdge ); // Add our filter for context to our query for fast execution. // Fast because it utilizes bitsets internally. See this post for more detail. // http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/ // TODO evaluate performance when it's an all query. // Do we need to put the context term first for performance? //make sure we have entity in the context BoolFilterBuilder boolQueryFilter = FilterBuilders.boolFilter(); //add our edge search boolQueryFilter.must( FilterBuilders.termFilter( IndexingUtils.EDGE_SEARCH_FIELDNAME, context ) ); /** * For the types the user specified, add them to an OR so 1 of them must match */ final String[] sourceTypes = searchTypes.getTypeNames( applicationScope ); if ( sourceTypes.length > 0 ) { final FilterBuilder[] typeTerms = new FilterBuilder[sourceTypes.length]; for ( int i = 0; i < sourceTypes.length; i++ ) { typeTerms[i] = FilterBuilders.termFilter( IndexingUtils.ENTITY_TYPE_FIELDNAME, sourceTypes[i] ); } //add all our types, 1 type must match per query boolQueryFilter.must( FilterBuilders.orFilter( typeTerms ) ); } //if we have a filter from our visitor, add it Optional<FilterBuilder> queryBuilder = visitor.getFilterBuilder(); if ( queryBuilder.isPresent() ) { boolQueryFilter.must( queryBuilder.get() ); } return boolQueryFilter; }
@Override public void visit( AndOperand op ) throws IndexException { op.getLeft().visit( this ); op.getRight().visit( this ); //get all the right final QueryBuilder rightQuery = queryBuilders.pop(); final FilterBuilder rightFilter = filterBuilders.pop(); //get all the left final QueryBuilder leftQuery = queryBuilders.pop(); final FilterBuilder leftFilter = filterBuilders.pop(); //push our boolean filters final boolean useLeftQuery = use( leftQuery ); final boolean useRightQuery = use( rightQuery ); /** * We use a left and a right, add our boolean query */ if ( useLeftQuery && useRightQuery ) { final BoolQueryBuilder qb = QueryBuilders.boolQuery().must(leftQuery).must(rightQuery); queryBuilders.push( qb ); } //only use the left else if ( useLeftQuery ) { queryBuilders.push( leftQuery ); } //only use the right else if ( useRightQuery ) { queryBuilders.push( rightQuery ); } //put in an empty in case we're not the root. I.E X and Y and Z else { queryBuilders.push( NoOpQueryBuilder.INSTANCE ); } //possibly use neither if the is a no-op final boolean useLeftFilter = use( leftFilter ); final boolean useRightFilter = use( rightFilter ); //use left and right if ( useLeftFilter && useRightFilter ) { final BoolFilterBuilder fb = FilterBuilders.boolFilter().must(leftFilter).must(rightFilter); filterBuilders.push( fb ); } //only use left else if ( useLeftFilter ) { filterBuilders.push( leftFilter ); } //only use right else if ( useRightFilter ) { filterBuilders.push( rightFilter ); } //push in a no-op in case we're not the root I.E X and Y and Z else { filterBuilders.push( NoOpFilterBuilder.INSTANCE ); } }
@Override public void visit( OrOperand op ) throws IndexException { op.getLeft().visit( this ); op.getRight().visit( this ); final QueryBuilder rightQuery = queryBuilders.pop(); final FilterBuilder rightFilter = filterBuilders.pop(); //get all the left final QueryBuilder leftQuery = queryBuilders.pop(); final FilterBuilder leftFilter = filterBuilders.pop(); final boolean useLeftQuery = use( leftQuery ); final boolean useRightQuery = use(rightQuery); //push our boolean filters if ( useLeftQuery && useRightQuery ) { //when we issue an OR query in usergrid, 1 or more of the terms should match. When doing bool query in ES, there is no requirement for more than 1 to match, where as in a filter more than 1 must match final BoolQueryBuilder qb = QueryBuilders.boolQuery().should( leftQuery ).should(rightQuery).minimumNumberShouldMatch( 1); queryBuilders.push( qb ); } else if ( useLeftQuery ) { queryBuilders.push( leftQuery ); } else if ( useRightQuery ) { queryBuilders.push( rightQuery ); } //put in an empty in case we're not the root. I.E X or Y or Z else { queryBuilders.push( NoOpQueryBuilder.INSTANCE ); } final boolean useLeftFilter = use( leftFilter ); final boolean useRightFilter = use(rightFilter); //use left and right if ( useLeftFilter && useRightFilter ) { final BoolFilterBuilder fb = FilterBuilders.boolFilter().should( leftFilter ).should( rightFilter ); filterBuilders.push( fb ); } //only use left else if ( useLeftFilter ) { filterBuilders.push( leftFilter ); } //only use right else if ( useRightFilter ) { filterBuilders.push( rightFilter ); } //put in an empty in case we're not the root. I.E X or Y or Z else { filterBuilders.push( NoOpFilterBuilder.INSTANCE ); } }
@GET @Path("/auditlog") @Produces(MediaType.APPLICATION_JSON) @AuditIgnore public StreamingOutput getAuditLog( @QueryParam("operation") String operation, @QueryParam("path") String path, @QueryParam("username") String username, @QueryParam("usertype") String usertype, @QueryParam("id") String id, @QueryParam("from") Integer from, @QueryParam("size") Integer size, @QueryParam("sort") String sort) throws IOException { BoolFilterBuilder filterBuilder = FilterBuilders.boolFilter(); boolean filterAdded = false; if (StringUtils.isNotBlank(path)) { filterAdded = true; filterBuilder.must(FilterBuilders.prefixFilter("path", path)); } if (StringUtils.isNotBlank(operation)) { filterAdded = true; filterBuilder.must(FilterBuilders.termFilter("operation", operation)); } if (StringUtils.isNotBlank(username)) { filterAdded = true; filterBuilder.must(FilterBuilders.termFilter("username", username)); } if (StringUtils.isNotBlank(usertype)) { filterAdded = true; filterBuilder.must(FilterBuilders.termFilter("usertype", usertype)); } if (StringUtils.isNotBlank(id)) { filterAdded = true; filterBuilder.must(FilterBuilders.termFilter("id", id)); } if (!filterAdded) { filterBuilder = null; } try { SearchResponse response = statsClientService.performSearch(StatsRecordType.AUDIT, filterBuilder, from, size, sort); return new ESDataOnlyResponse(response); } catch (SearchIndexMissingException e) { return null; } }
private SettableFuture<SearchResponse> queryInternal( AttributeQuerySet query, Iterable<Publisher> publishers, Selection selection ) { SettableFuture<SearchResponse> response = SettableFuture.create(); EsQueryParser.EsQuery esQuery = esQueryParser.parse(query); QueryBuilder queryBuilder = queryBuilderFactory.buildQuery(esQuery.getAttributeQuerySet()); /* matchAllFilter as a bool filter with less than 1 clause is invalid */ BoolFilterBuilder filterBuilder = FilterBuilders.boolFilter() .must(FilterBuilders.matchAllFilter()); SearchRequestBuilder reqBuilder = esClient .prepareSearch(index) .setTypes( EsContent.CHILD_ITEM, EsContent.TOP_LEVEL_CONTAINER, EsContent.TOP_LEVEL_ITEM ) .addField(EsContent.CANONICAL_ID) .addField(EsContent.ID) .addField(EsContent.SOURCE) .setPostFilter(FiltersBuilder.buildForPublishers(EsContent.SOURCE, publishers)) .setFrom(selection.getOffset()) .setSize(Objects.firstNonNull(selection.getLimit(), DEFAULT_LIMIT)); addOrdering(esQuery.getIndexQueryParams(), reqBuilder); queryBuilder = addFuzzyQuery(esQuery.getIndexQueryParams(), queryBuilder, reqBuilder); addBrandId(esQuery.getIndexQueryParams(), filterBuilder); addSeriesId(esQuery.getIndexQueryParams(), filterBuilder); addTopicFilter(esQuery.getIndexQueryParams(), filterBuilder); addActionableFilter(esQuery.getIndexQueryParams(), filterBuilder); reqBuilder.addSort(EsContent.ID, SortOrder.ASC); FilteredQueryBuilder finalQuery = QueryBuilders.filteredQuery(queryBuilder, filterBuilder); reqBuilder.setQuery(finalQuery); log.debug(reqBuilder.internalBuilder().toString()); reqBuilder.execute(FutureSettingActionListener.setting(response)); return response; }
/** * Generate the field name term for the field name for filters */ private NestedFilterBuilder fieldNameTerm( final String fieldName, final FilterBuilder fieldValueBuilder ) { final BoolFilterBuilder booleanQuery = FilterBuilders.boolFilter(); booleanQuery.must( FilterBuilders.termFilter( IndexingUtils.FIELD_NAME_NESTED, fieldName ) ); booleanQuery.must( fieldValueBuilder ); return FilterBuilders.nestedFilter( IndexingUtils.ENTITY_FIELDS, booleanQuery ); }
/** * 将where条件构建成filter * * @param where * @return * @throws SqlParseException */ public static BoolFilterBuilder explan(Where where) throws SqlParseException { BoolFilterBuilder boolFilter = FilterBuilders.boolFilter(); new FilterMaker().explanWhere(boolFilter, where); return boolFilter; }