org.elasticsearch.index.query.FilterBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.FilterBuilder. 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: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
private long getTraceCompletionCount(String tenantId, Criteria criteria, boolean onlyFaulty) {
    String index = client.getIndex(tenantId);
    if (!refresh(index)) {
        return 0;
    }

    BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, CompletionTime.class);
    SearchRequestBuilder request = getTraceCompletionRequest(index, criteria, query, 0);

    if (onlyFaulty) {
        FilterBuilder filter = FilterBuilders.queryFilter(QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery(ElasticsearchUtil.PROPERTIES_NAME_FIELD, Constants.PROP_FAULT)));
        request.setPostFilter(FilterBuilders.nestedFilter("properties", filter));
    }

    SearchResponse response = request.execute().actionGet();
    if (response.isTimedOut()) {
        msgLog.warnQueryTimedOut();
        return 0;
    } else {
        return response.getHits().getTotalHits();
    }
}
 
Example #2
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 6 votes vote down vote up
private FilterBuilder getBaseFilterBuilder(String facetName, String fieldValue)
    {
        if(facetName.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX))
        {
            return getTermFilter(SearchFacetName.CATEGORIES_FIELD_PREFIX + facetName + "." + SearchDocumentFieldName.FACETFILTER.getFieldName(), fieldValue.toLowerCase());
        }
        else if(facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE.getCode()))
        {
            return FilterBuilders.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName()).includeLower(true).includeUpper(false).from(fieldValue.split("-")[0]).to(fieldValue.split("-")[1]);
        }
        else
        {
            return FilterBuilders.termFilter(facetName, fieldValue);
        }
//        return null;
    }
 
Example #3
Source File: AST_Search.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
private static FilterBuilder fromValueType(String field, String value, int valueType){
	if(value.contains("*") || value.contains("?")){
		if( value.length() > 1 && value.indexOf('*') == (value.length()-1))
			return FilterBuilders.prefixFilter(field, value.substring(0, value.length()-1));
		else
			return FilterBuilders.queryFilter(QueryBuilders.wildcardQuery(field, value));
	}
	
	switch(valueType){
	case AST_TermExpression.TERM:
		return FilterBuilders.termFilter(field, value);
	case AST_TermExpression.PHRASE:
		/*for(byte b: value.getBytes()){
			System.out.printf("0x%02X ", b);		        
		}*/
		return FilterBuilders.queryFilter(QueryBuilders.matchPhraseQuery(field, value));
	}
	return null;
}
 
Example #4
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
private FilterBuilder getBaseFilterBuilder(final String facetName,
		final String fieldValue) {
	if (facetName
			.startsWith(SearchFacetName.SEARCH_FACET_TYPE_FACET_PREFIX)) {
		return getTermFilter(
				SearchFacetName.CATEGORIES_FIELD_PREFIX
						+ facetName
						+ "."
						+ SearchDocumentFieldName.FACETFILTER
								.getFieldName(),
				fieldValue.toLowerCase());
	} else if (facetName.startsWith(SearchFacetName.PRODUCT_PRICE_RANGE
			.getCode())) {
		return FilterBuilders
				.rangeFilter(SearchDocumentFieldName.PRICE.getFieldName())
				.includeLower(true).includeUpper(false)
				.from(fieldValue.split("-")[0])
				.to(fieldValue.split("-")[1]);
	} else {
		return FilterBuilders.termFilter(facetName, fieldValue);
	}
	// return null;
}
 
Example #5
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<FilterBuilder> getFilterBuilder() {
    if ( filterBuilders.empty() ) {
        return Optional.absent();
    }

    final FilterBuilder builder = filterBuilders.peek();

    if ( !use( builder ) ) {
        return Optional.absent();
    }

    return Optional.of( builder );
}
 
Example #6
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
public FilterBuilder dateRangeFilter(ReadableInstant from, ReadableInstant to) {
    if (from == null && to == null) {
        return matchAllFilter();
    }
    RangeFilterBuilder dateFilter = rangeFilter("commit.commitdate");
    if (from != null) {
        dateFilter.gte(from.getMillis());
    }
    if (to != null) {
        dateFilter.lte(to.getMillis());
    }
    // Match all files as well, since they don't have date info (user can turn off by
    // unchecking "search files" option.)
    return orFilter(dateFilter, typeFilter("file"));
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
public FilterBuilder projectRepositoryFilter(String project, String repository) {
    return boolFilter()
        .must(termFilter("project", project))
        .must(termFilter("repository", repository))
        .cache(true)
        .cacheKey("CACHE^PROJECTREPOFILTER^" + project + "^" + repository);
}
 
Example #12
Source File: ElasticsearchUtil.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns a filter associated with the supplied criteria.
 *
 * @param criteria The criteria
 * @return The filter, or null if not relevant
 */
public static FilterBuilder buildFilter(Criteria criteria) {
    if (criteria.getTransaction() != null && criteria.getTransaction().trim().isEmpty()) {
        return FilterBuilders.missingFilter(TRANSACTION_FIELD);
    }
    return null;
}
 
Example #13
Source File: SearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public SearchRequestBuilder<JsonInput, JsonOutput> filterBuilder(FilterBuilder filter) {
    sourceBuilder().filter(filter);
    return this;
}
 
Example #14
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public void visit( NotOperand op ) throws IndexException {

    //we need to know if we're the root entry for building our queries correctly
    final boolean rootNode = queryBuilders.empty() && filterBuilders.isEmpty();

    op.getOperation().visit( this );

    //push our not operation into our query

    final QueryBuilder notQueryBuilder = queryBuilders.pop();

    if ( use( notQueryBuilder ) ) {
        final QueryBuilder notQuery = QueryBuilders.boolQuery().mustNot(notQueryBuilder);
        queryBuilders.push( notQuery  );
    }
    else {
        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
    }

    final FilterBuilder notFilterBuilder = filterBuilders.pop();

    //push the filter in
    if ( use( notFilterBuilder ) ) {

        final FilterBuilder notFilter = FilterBuilders.boolFilter().mustNot( notFilterBuilder ) ;

        //just the root node
        if(!rootNode) {
            filterBuilders.push( notFilter );
        }
        //not the root node, we have to select all to subtract from with the NOT statement
        else{
            final FilterBuilder selectAllFilter = FilterBuilders.boolFilter().must( FilterBuilders.matchAllFilter()) .must( notFilter );
            filterBuilders.push( selectAllFilter );
        }

    }
    else {
        filterBuilders.push( NoOpFilterBuilder.INSTANCE );
    }
}
 
Example #15
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 #16
Source File: EsQueryVistor.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Return false if our element is a no-op, true otherwise
 */
private boolean use( final FilterBuilder filterBuilder ) {
    return filterBuilder != NoOpFilterBuilder.INSTANCE;
}
 
Example #17
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 #18
Source File: UpdateIndicesAliasesRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public UpdateIndicesAliasesRequestBuilder<JsonInput, JsonOutput> addAlias(String index, String alias, FilterBuilder filterBuilder) {
    request.addAlias(index, alias, filterBuilder);
    return this;
}
 
Example #19
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 #20
Source File: AppConfig.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
@Override
public <T> FacetedSearchResult facetedSearch(Class<T> aClass, String s, Map<String, String[]> map, FilterBuilder filterBuilder, String s1, int i, int i1, String s2, boolean b) {
    return null;
}
 
Example #21
Source File: AppConfig.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
@Override
public <T> FacetedSearchResult facetedSearch(Class<T> aClass, String s, Map<String, String[]> map, FilterBuilder filterBuilder, String s1, int i, int i1) {
    return null;
}
 
Example #22
Source File: AppConfig.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
@Override
public GetMultipleDataResult<Object> search(String[] strings, Class<?>[] classes, String s, Map<String, String[]> map, FilterBuilder filterBuilder, String s1, int i, int i1) {
    return null;
}
 
Example #23
Source File: AppConfig.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
@Override
public <T> GetMultipleDataResult<T> search(Class<T> aClass, String s, Map<String, String[]> map, FilterBuilder filterBuilder, String s1, int i, int i1) {
    return null;
}
 
Example #24
Source File: AppConfig.java    From SeaCloudsPlatform with Apache License 2.0 4 votes vote down vote up
@Override
public <T> GetMultipleDataResult<T> search(Class<T> aClass, String s, Map<String, String[]> map, FilterBuilder filterBuilder, String s1, int i, int i1, String s2, boolean b) {
    return null;
}
 
Example #25
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder authorFilter(String[] authors) {
    return authorFilter(toIterable(authors));
}
 
Example #26
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder extensionFilter(String[] extensions) {
    return extensionFilter(toIterable(extensions));
}
 
Example #27
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder repositoryFilter(String[] repositories) {
    return repositoryFilter(toIterable(repositories));
}
 
Example #28
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder projectFilter(String[] projects) {
    return projectFilter(toIterable(projects));
}
 
Example #29
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder refFilter(String[] refs) {
    return refFilter(toIterable(refs));
}
 
Example #30
Source File: SearchFilterUtils.java    From stash-codesearch-plugin with Apache License 2.0 4 votes vote down vote up
public FilterBuilder exactRefFilter(String ref) {
    return termFilter("refs.untouched", ref)
        .cache(true)
        .cacheKey("CACHE^EXACTREFFILTER^" + ref);
}