Java Code Examples for org.elasticsearch.index.query.QueryBuilders#queryString()

The following examples show how to use org.elasticsearch.index.query.QueryBuilders#queryString() . 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 5 votes vote down vote up
protected QueryBuilder getQueryBuilder(SearchCriteria searchCriteria)
{
    QueryBuilder matchQueryBuilder = null;
    
    String queryString = searchCriteria.getQuery();
    
    if (StringUtils.isBlank(queryString))
    {
        matchQueryBuilder = QueryBuilders.matchAllQuery();
    } 
    else
    {
        final String filterSpecialCharsQueryString = escapeQueryChars(queryString);
        final QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryString(filterSpecialCharsQueryString);

        // Add fields
        queryStringQueryBuilder.field(SearchDocumentFieldName.TITLE.getFieldName(), (float) 0.5)
                                .field(SearchDocumentFieldName.DESCRIPTION.getFieldName(), (float) 0.15)
                                ;
        
        for (final String contentCategoryFieldName : SearchFacetName.categoryFacetFields)
        {
            queryStringQueryBuilder.field(SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName() + "."
                    + contentCategoryFieldName, 1);
        }
        
        matchQueryBuilder = queryStringQueryBuilder;
    }
    
    if(searchCriteria.isUseBoostingFactor())
    {
        FunctionScoreQueryBuilder queryBuilder = new FunctionScoreQueryBuilder(matchQueryBuilder);
        ScoreFunctionBuilder scoreFunctionBuilder = new ScriptScoreFunctionBuilder().script(SearchDocumentFieldName
                .getCalculatedScoreScriptForBostFactor());
        queryBuilder.add(scoreFunctionBuilder);
        return queryBuilder;
    }

    return matchQueryBuilder;
}
 
Example 2
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
protected QueryBuilder getQueryBuilder(final SearchCriteria searchCriteria) {
	QueryBuilder matchQueryBuilder = null;
	final String queryString = searchCriteria.getQuery();
	if (StringUtils.isBlank(queryString)) {
		matchQueryBuilder = QueryBuilders.matchAllQuery();
	} else {
		final String filterSpecialCharsQueryString = escapeQueryChars(queryString);
		final QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders
				.queryString(filterSpecialCharsQueryString);
		// Add fields
		queryStringQueryBuilder.field(
				SearchDocumentFieldName.TITLE.getFieldName(), (float) 0.5)
				.field(SearchDocumentFieldName.DESCRIPTION.getFieldName(),
						(float) 0.15);
		for (final String contentCategoryFieldName : SearchFacetName.categoryFacetFields) {
			queryStringQueryBuilder.field(
					SearchDocumentFieldName.CATEGORIES_ARRAY.getFieldName()
							+ "." + contentCategoryFieldName, 1);
		}
		matchQueryBuilder = queryStringQueryBuilder;
	}
	if (searchCriteria.isUseBoostingFactor()) {
		final FunctionScoreQueryBuilder queryBuilder = new FunctionScoreQueryBuilder(
				matchQueryBuilder);
		final ScoreFunctionBuilder scoreFunctionBuilder = new ScriptScoreFunctionBuilder()
				.script(SearchDocumentFieldName
						.getCalculatedScoreScriptForBostFactor());
		queryBuilder.add(scoreFunctionBuilder);
		return queryBuilder;
	}
	return matchQueryBuilder;
}