es 字段加权重

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;
}
 

   

{
  "query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "should" : {
            "query_string" : {
              "query" : "卫衣",
              "default_field" : "cnGoodsName"
            }
          }
        }
      },
      "functions" : [ {
        "field_value_factor" : {
          "field" : "pid",
          "factor" : 2.0,
          "modifier" : "log1p"
        }
      } ]
    }
  }
}

   _search?explain=true

 

 

 

   https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

http://www.programcreek.com/java-api-examples/index.php?api=org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder

 

你可能感兴趣的:(es 字段加权重)