Java Code Examples for org.elasticsearch.index.query.FilterBuilders#termFilter()

The following examples show how to use org.elasticsearch.index.query.FilterBuilders#termFilter() . 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: 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 2
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 3
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 4
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
private TermFilterBuilder getTermFilter(String fieldName, String fieldValue)
{
    return FilterBuilders.termFilter(fieldName, fieldValue);
}
 
Example 5
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
private TermFilterBuilder getTermFilter(final String fieldName,
		final String fieldValue) {
	return FilterBuilders.termFilter(fieldName, fieldValue);
}
 
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( Equal op ) throws NoIndexException {
    final String name = op.getProperty().getValue().toLowerCase();
    final Object value = op.getLiteral().getValue();

    //special case so we support our '*' char with wildcard, also should work for uuids
    if ( value instanceof String || value instanceof UUID ) {
        String stringValue = ((value instanceof String) ? (String)value : value.toString()).toLowerCase().trim();

        // or field is just a string that does need a prefix us a query
        if ( stringValue.contains( "*" ) ) {

            //Because of our legacy behavior, where we match CCCC*, we need to use the unanalyzed string to ensure that
            //we start
            final WildcardQueryBuilder wildcardQuery =
                    QueryBuilders.wildcardQuery( IndexingUtils.FIELD_STRING_NESTED_UNANALYZED, stringValue );
            queryBuilders.push( fieldNameTerm( name, wildcardQuery ) );
            filterBuilders.push( NoOpFilterBuilder.INSTANCE );
            return;
        }

        // Usergrid query parser allows single quotes to be escaped in values
        if ( stringValue.contains("\\'")) {
            stringValue = stringValue.replace("\\'", "'");
        }

        //it's an exact match, use a filter
        final TermFilterBuilder termFilter =
                FilterBuilders.termFilter( IndexingUtils.FIELD_STRING_NESTED_UNANALYZED, stringValue );

        queryBuilders.push( NoOpQueryBuilder.INSTANCE );
        filterBuilders.push( fieldNameTerm( name, termFilter ) );

        return;
    }

    // assume all other types need prefix

    final TermFilterBuilder termQuery =
            FilterBuilders.termFilter(getFieldNameForType(value), sanitize(value));

    filterBuilders.push( fieldNameTerm( name, termQuery ) );

    queryBuilders.push( NoOpQueryBuilder.INSTANCE );
}
 
Example 8
Source File: SortBuilder.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Create a term filter for our sorts
 */
public static TermFilterBuilder sortPropertyTermFilter( final String propertyName ) {
    return FilterBuilders.termFilter( IndexingUtils.FIELD_NAME, propertyName );
}