org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder. 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 5 votes vote down vote up
/**
 * 构建查询
 * 
 * @return
 */
public QueryBuilder makeQueryBuilder(SearchRequestBuilder srb) {

    // 拼接查询层次
    BoolQueryBuilder boolQ = QueryBuilders.boolQuery();
    Map<SearchCondition, ConditionType> searchTypeMap = this.request.getSearchConditions();
    ConditionType conditionType;
    BoolQueryBuilder subBoolQ;
    for (SearchCondition searchCondition : searchTypeMap.keySet()) {
        conditionType = searchTypeMap.get(searchCondition);
        subBoolQ = queryBuilder(searchCondition, conditionType);
        mergeBuilder(boolQ, subBoolQ, conditionType);
    }
    // 没有条件直接返回
    if (!boolQ.hasClauses()) {
        return null;
    }
    // 得分脚本
    if (request.getSafeScoreScript().size() < 1) {
        srb.setQuery(boolQ);
        return boolQ;
    }
    String boost_mode = request.getSafeExtend().get(Constants.BOOST_MODE_KEY);
    if (StringUtils.isBlank(boost_mode)) {
        boost_mode = Constants.boost_mode;
    }
    FunctionScoreQueryBuilder functionScoreQ = QueryBuilders.functionScoreQuery(boolQ);
    functionScoreQ.boostMode(boost_mode);
    for (ScoreScript scoreScript : request.getSafeScoreScript()) {
        ScriptScoreFunctionBuilder builder = ScoreFunctionBuilders.scriptFunction(scoreScript
                .getScript());
        if (scoreScript.getScriptParams() != null && scoreScript.getScriptParams().size() > 0) {
            builder.params(scoreScript.getScriptParams());
        }
        functionScoreQ.add(builder);
    }
    srb.setQuery(functionScoreQ);
    return functionScoreQ;
}
 
Example #2
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 #3
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 #4
Source File: ScoreFunctionBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static ScriptScoreFunctionBuilder scriptFunction(Script script) {
    return (new ScriptScoreFunctionBuilder(script));
}
 
Example #5
Source File: ScoreFunctionBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static ScriptScoreFunctionBuilder scriptFunction(String script) {
    return (new ScriptScoreFunctionBuilder(new Script(script)));
}