org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder. 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;
}
 
Example #3
Source File: KeyMatchHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected Map<String, Pair<QueryBuilder, ScoreFunctionBuilder<?>>> getQueryMap() {
    final String key = ComponentUtil.getVirtualHostHelper().getVirtualHostKey();
    final Map<String, Pair<QueryBuilder, ScoreFunctionBuilder<?>>> map = keyMatchQueryMap.get(key);
    if (map != null) {
        return map;
    }
    return Collections.emptyMap();
}
 
Example #4
Source File: KeyMatchHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
public void buildQuery(final List<String> keywordList, final List<FilterFunctionBuilder> list) {
    keywordList.stream().forEach(keyword -> {
        final Pair<QueryBuilder, ScoreFunctionBuilder<?>> pair = getQueryMap().get(toLowerCase(keyword));
        if (pair != null) {
            list.add(new FilterFunctionBuilder(pair.getFirst(), pair.getSecond()));
        }
    });
}
 
Example #5
Source File: KeyMatchHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> getBoostedDocumentList(final String term, final int size) {
    final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
    final Pair<QueryBuilder, ScoreFunctionBuilder<?>> pair = getQueryMap().get(toLowerCase(term));
    if (pair == null) {
        return Collections.emptyList();
    }
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    return fessEsClient.getDocumentList(fessConfig.getIndexDocumentSearchIndex(), searchRequestBuilder -> {
        searchRequestBuilder.setPreference(Constants.SEARCH_PREFERENCE_LOCAL).setQuery(pair.getFirst()).setSize(size);
        return true;
    });
}
 
Example #6
Source File: FactorBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ScoreFunctionBuilder setWeight(float weight) {
    throw new IllegalArgumentException(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE);
}
 
Example #7
Source File: QueryHelper.java    From fess with Apache License 2.0 4 votes vote down vote up
public void addBoostFunction(final ScoreFunctionBuilder<?> scoreFunction) {
    boostFunctionList.add(new FilterFunctionBuilder(scoreFunction));
}
 
Example #8
Source File: QueryHelper.java    From fess with Apache License 2.0 4 votes vote down vote up
public void addBoostFunction(final QueryBuilder filter, final ScoreFunctionBuilder<?> scoreFunction) {
    boostFunctionList.add(new FilterFunctionBuilder(filter, scoreFunction));
}
 
Example #9
Source File: KeyMatchHelper.java    From fess with Apache License 2.0 4 votes vote down vote up
protected void reload(final long interval) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final Map<String, Map<String, Pair<QueryBuilder, ScoreFunctionBuilder<?>>>> keyMatchQueryMap = new HashMap<>();
    getAvailableKeyMatchList().stream().forEach(
            keyMatch -> {
                final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading KeyMatch Query: {}, Size: {}", keyMatch.getQuery(), keyMatch.getMaxSize());
                }
                getDocumentList(keyMatch).stream().map(doc -> {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Loaded KeyMatch doc: {}", doc);
                    }
                    return DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
                }).forEach(docId -> {
                    boolQuery.should(QueryBuilders.termQuery(fessConfig.getIndexFieldDocId(), docId));
                });

                if (boolQuery.hasClauses()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Loaded KeyMatch Boost Query: {}", boolQuery);
                    }
                    String virtualHost = keyMatch.getVirtualHost();
                    if (StringUtil.isBlank(virtualHost)) {
                        virtualHost = StringUtil.EMPTY;
                    }
                    Map<String, Pair<QueryBuilder, ScoreFunctionBuilder<?>>> queryMap = keyMatchQueryMap.get(virtualHost);
                    if (queryMap == null) {
                        queryMap = new HashMap<>();
                        keyMatchQueryMap.put(virtualHost, queryMap);
                    }
                    queryMap.put(toLowerCase(keyMatch.getTerm()),
                            new Pair<>(boolQuery, ScoreFunctionBuilders.weightFactorFunction(keyMatch.getBoost())));
                } else if (logger.isDebugEnabled()) {
                    logger.debug("No KeyMatch boost docs");
                }

                if (interval > 0) {
                    ThreadUtil.sleep(interval);
                }
            });
    this.keyMatchQueryMap = keyMatchQueryMap;
}
 
Example #10
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A query that allows to define a custom scoring function.
 *
 * @param function The function builder used to custom score
 */
public static FunctionScoreQueryBuilder functionScoreQuery(ScoreFunctionBuilder function) {
    return new FunctionScoreQueryBuilder(function);
}
 
Example #11
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A query that allows to define a custom scoring function.
 *
 * @param queryBuilder The query to custom score
 * @param function     The function builder used to custom score
 */
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, ScoreFunctionBuilder function) {
    return (new FunctionScoreQueryBuilder(queryBuilder)).add(function);
}
 
Example #12
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 votes vote down vote up
void filter(final OperatorCall<T> cqLambda, final ScoreFunctionBuilder<?> scoreFunctionBuilder); 
Example #13
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 votes vote down vote up
void filter(final OperatorCall<T> cqLambda, final ScoreFunctionBuilder<?> scoreFunctionBuilder); 
Example #14
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 votes vote down vote up
void filter(final OperatorCall<T> cqLambda, final ScoreFunctionBuilder<?> scoreFunctionBuilder);