/** * 构建查询 * * @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; }
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; }
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; }
public ElasticSearchPlaceQuery addBoostForChildren() { // places with many children should get a higher score queryBuilder = QueryBuilders.functionScoreQuery(queryBuilder) .add(new ScriptScoreFunctionBuilder().script("_score + (1.0 - 1.0 / ( 0.001 * doc['children'].value + 1.0 ) )").lang("groovy")); return this; }
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; }
public static ScriptScoreFunctionBuilder scriptFunction(Script script) { return (new ScriptScoreFunctionBuilder(script)); }
public static ScriptScoreFunctionBuilder scriptFunction(String script) { return (new ScriptScoreFunctionBuilder(new Script(script))); }