public void testScore() throws Exception { createIndex("test"); ensureGreen("test"); indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"), client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye")); ScoreFunctionBuilder<?> score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap())); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE)); req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent SearchResponse rsp = req.get(); assertSearchResponse(rsp); SearchHits hits = rsp.getHits(); assertEquals(3, hits.getTotalHits()); assertEquals("1", hits.getAt(0).getId()); assertEquals("3", hits.getAt(1).getId()); assertEquals("2", hits.getAt(2).getId()); }
private void registerScoreFunctions(List<SearchPlugin> plugins) { registerScoreFunction(new ScoreFunctionSpec<>(ScriptScoreFunctionBuilder.NAME, ScriptScoreFunctionBuilder::new, ScriptScoreFunctionBuilder::fromXContent)); registerScoreFunction( new ScoreFunctionSpec<>(GaussDecayFunctionBuilder.NAME, GaussDecayFunctionBuilder::new, GaussDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(LinearDecayFunctionBuilder.NAME, LinearDecayFunctionBuilder::new, LinearDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(ExponentialDecayFunctionBuilder.NAME, ExponentialDecayFunctionBuilder::new, ExponentialDecayFunctionBuilder.PARSER)); registerScoreFunction(new ScoreFunctionSpec<>(RandomScoreFunctionBuilder.NAME, RandomScoreFunctionBuilder::new, RandomScoreFunctionBuilder::fromXContent)); registerScoreFunction(new ScoreFunctionSpec<>(FieldValueFactorFunctionBuilder.NAME, FieldValueFactorFunctionBuilder::new, FieldValueFactorFunctionBuilder::fromXContent)); //weight doesn't have its own parser, so every function supports it out of the box. //Can be a single function too when not associated to any other function, which is why it needs to be registered manually here. namedWriteables.add(new NamedWriteableRegistry.Entry(ScoreFunctionBuilder.class, WeightBuilder.NAME, WeightBuilder::new)); registerFromPlugin(plugins, SearchPlugin::getScoreFunctions, this::registerScoreFunction); }
private void registerScoreFunction(ScoreFunctionSpec<?> scoreFunction) { namedWriteables.add(new NamedWriteableRegistry.Entry( ScoreFunctionBuilder.class, scoreFunction.getName().getPreferredName(), scoreFunction.getReader())); // TODO remove funky contexts namedXContents.add(new NamedXContentRegistry.Entry( ScoreFunctionBuilder.class, scoreFunction.getName(), (XContentParser p, Object c) -> scoreFunction.getParser().fromXContent((QueryParseContext) c))); }
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; }
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; }
@Override public ScoreFunctionBuilder setWeight(float weight) { throw new IllegalArgumentException(BoostScoreFunction.BOOST_WEIGHT_ERROR_MESSAGE); }
/** * 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); }
/** * 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, function)); }
/** * 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); }