org.elasticsearch.index.query.BoolFilterBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.BoolFilterBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EsQueryBuilder.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 获取过滤器
 * 
 * @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;
}
 
Example #2
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #3
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #4
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #5
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #6
Source File: SearchRequestBuilderStrategy.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@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 );
    }
}
 
Example #8
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@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 );
    }
}
 
Example #9
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 3 votes vote down vote up
/**
 * 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 );
}