Java Code Examples for org.elasticsearch.index.query.BoolFilterBuilder#should()

The following examples show how to use org.elasticsearch.index.query.BoolFilterBuilder#should() . 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();
}