add builders for nicer java api

This commit and the previous 10 commits close issue 3533.

Closes #3533
This commit is contained in:
Britta Weber 2013-08-19 14:23:18 +02:00
parent 5258940d9e
commit 8d6dc5908e
10 changed files with 268 additions and 176 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.query;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import java.util.Collection;
@ -549,12 +550,30 @@ public abstract class QueryBuilders {
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder) {
return new FunctionScoreQueryBuilder(queryBuilder);
}
/**
* A query that allows to define a custom scoring function.
*
* @param queryBuilder The query to custom score
* @param scoreFunctionBuilder The score function used to re-score the query
*/
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, ScoreFunctionBuilder function) {
return (new FunctionScoreQueryBuilder(queryBuilder)).add(function);
}
/**
* A query that allows to define a custom scoring function.
*
* @param filterBuilder The query to custom score
*/
public static FunctionScoreQueryBuilder functionScoreQuery(FilterBuilder filterBuilder, ScoreFunctionBuilder function) {
return (new FunctionScoreQueryBuilder(filterBuilder)).add(function);
}
/**
* A query that allows to define a custom scoring function.
*
* @param filterBuilder The filterBuilder to custom score
*/
public static FunctionScoreQueryBuilder functionScoreQuery(FilterBuilder filterBuilder) {
return new FunctionScoreQueryBuilder(filterBuilder);
}

View File

@ -0,0 +1,96 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.query.functionscore;
import org.elasticsearch.index.query.functionscore.exp.ExponentialDecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.factor.FactorBuilder;
import org.elasticsearch.index.query.functionscore.gauss.GaussDecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.lin.LinearDecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.random.RandomScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import java.util.Map;
public class ScoreFunctionBuilders {
/**
* A query that match on all documents.
*/
public static ExponentialDecayFunctionBuilder exponentialDecayFunction(String fieldName, Object origin, Object scale) {
return new ExponentialDecayFunctionBuilder(fieldName, origin, scale);
}
public static ExponentialDecayFunctionBuilder exponentialDecayFunction(String fieldName, Object origin, Object scale, double decay) {
return (ExponentialDecayFunctionBuilder) (new ExponentialDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay);
}
public static ExponentialDecayFunctionBuilder exponentialDecayFunction(String fieldName, Object origin, Object scale, double decay,
double offset) {
return (ExponentialDecayFunctionBuilder) (new ExponentialDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay).setOffset(
offset);
}
public static GaussDecayFunctionBuilder gaussDecayFunction(String fieldName, Object origin, Object scale) {
return new GaussDecayFunctionBuilder(fieldName, origin, scale);
}
public static GaussDecayFunctionBuilder gaussDecayFunction(String fieldName, Object origin, Object scale, double decay) {
return (GaussDecayFunctionBuilder) (new GaussDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay);
}
public static GaussDecayFunctionBuilder gaussDecayFunction(String fieldName, Object origin, Object scale, double decay, double offset) {
return (GaussDecayFunctionBuilder) (new GaussDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay).setOffset(offset);
}
public static LinearDecayFunctionBuilder linearDecayFunction(String fieldName, Object origin, Object scale) {
return new LinearDecayFunctionBuilder(fieldName, origin, scale);
}
public static LinearDecayFunctionBuilder linearDecayFunction(String fieldName, Object origin, Object scale, double decay) {
return (LinearDecayFunctionBuilder) (new LinearDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay);
}
public static LinearDecayFunctionBuilder linearDecayFunction(String fieldName, Object origin, Object scale, double decay, double offset) {
return (LinearDecayFunctionBuilder) (new LinearDecayFunctionBuilder(fieldName, origin, scale)).setDecay(decay).setOffset(offset);
}
public static ScriptScoreFunctionBuilder scriptFunction(String script) {
return (new ScriptScoreFunctionBuilder()).script(script);
}
public static ScriptScoreFunctionBuilder scriptFunction(String script, String lang) {
return (new ScriptScoreFunctionBuilder()).script(script).lang(lang);
}
public static ScriptScoreFunctionBuilder scriptFunction(String script, String lang, Map<String, Object> params) {
return (new ScriptScoreFunctionBuilder()).script(script).lang(lang).params(params);
}
public static ScriptScoreFunctionBuilder scriptFunction(String script, Map<String, Object> params) {
return (new ScriptScoreFunctionBuilder()).script(script).params(params);
}
public static FactorBuilder factorFunction(float boost) {
return (new FactorBuilder()).boostFactor(boost);
}
public static RandomScoreFunctionBuilder randomFunction(long seed) {
return (new RandomScoreFunctionBuilder()).seed(seed);
}
}

View File

@ -39,7 +39,6 @@ import org.elasticsearch.index.engine.DocumentMissingException;
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test;
@ -50,6 +49,7 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.*;
@ -1159,7 +1159,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setScore(true)
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(numQueries));
@ -1178,7 +1178,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSort(true)
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(numQueries));
@ -1203,8 +1203,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSort(true)
.setSize(size)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchQuery("field1", value))
.add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchQuery("field1", value), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
@ -1237,7 +1236,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSort(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(2l));
@ -1249,7 +1248,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
response = client().preparePercolate().setIndices("my-index").setDocumentType("my-type")
.setSort(true)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertThat(response.getCount(), equalTo(0l));
assertThat(response.getSuccessfulShards(), equalTo(3));
@ -1269,7 +1268,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSort(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(0l));
@ -1290,7 +1289,7 @@ public class SimplePercolatorTests extends AbstractSharedClusterTest {
.setSort(true)
.setSize(2)
.setPercolateDoc(docBuilder().setDoc("field", "value"))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("doc['level'].value")))
.setPercolateQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), scriptFunction("doc['level'].value")))
.execute().actionGet();
assertNoFailures(response);
assertThat(response.getCount(), equalTo(0l));

View File

@ -30,7 +30,6 @@ import org.elasticsearch.common.Priority;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
@ -45,6 +44,7 @@ import static com.google.common.collect.Maps.newHashMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.search.facet.FacetBuilders.termsFacet;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
@ -1185,8 +1185,7 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest {
.setQuery(
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0))
.add(new ScriptScoreFunctionBuilder().script("doc['c_field1'].value"))
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), scriptFunction("doc['c_field1'].value"))
.boostMode(CombineFunction.REPLACE.getName())).scoreType("sum")).execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -1202,8 +1201,7 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest {
.setQuery(
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0))
.add(new ScriptScoreFunctionBuilder().script("doc['c_field1'].value"))
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), scriptFunction("doc['c_field1'].value"))
.boostMode(CombineFunction.REPLACE.getName())).scoreType("max")).execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -1219,8 +1217,7 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest {
.setQuery(
QueryBuilders.hasChildQuery(
"child",
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0))
.add(new ScriptScoreFunctionBuilder().script("doc['c_field1'].value"))
QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), scriptFunction("doc['c_field1'].value"))
.boostMode(CombineFunction.REPLACE.getName())).scoreType("avg")).execute().actionGet();
assertThat(response.getHits().totalHits(), equalTo(3l));
@ -1236,8 +1233,7 @@ public class SimpleChildQuerySearchTests extends AbstractSharedClusterTest {
.setQuery(
QueryBuilders.hasParentQuery(
"parent",
QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"))
.add(new ScriptScoreFunctionBuilder().script("doc['p_field2'].value"))
QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), scriptFunction("doc['p_field2'].value"))
.boostMode(CombineFunction.REPLACE.getName())).scoreType("score"))
.addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).execute().actionGet();

View File

@ -26,8 +26,6 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.Priority;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.functionscore.factor.FactorBuilder;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test;
@ -39,6 +37,8 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.factorFunction;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
@ -144,10 +144,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
SearchResponse searchResponse = client()
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("first")
.add(termFilter("field", "value4"), new ScriptScoreFunctionBuilder().script("2"))
.add(termFilter("field", "value2"), new ScriptScoreFunctionBuilder().script("3"))).setExplain(true)
.execute().actionGet();
functionScoreQuery(matchAllQuery()).scoreMode("first").add(termFilter("field", "value4"), scriptFunction("2"))
.add(termFilter("field", "value2"), scriptFunction("3"))).setExplain(true).execute().actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -173,10 +171,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
searchResponse = client()
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("first")
.add(termFilter("field", "value4"), new ScriptScoreFunctionBuilder().script("2"))
.add(termFilter("field", "value2"), new ScriptScoreFunctionBuilder().script("3")).boost(2))
.setExplain(true).execute().actionGet();
functionScoreQuery(matchAllQuery()).scoreMode("first").add(termFilter("field", "value4"), scriptFunction("2"))
.add(termFilter("field", "value2"), scriptFunction("3")).boost(2)).setExplain(true).execute().actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -362,9 +358,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource()
.explain(true)
.query(functionScoreQuery(termQuery("test", "value"))
.add(new ScriptScoreFunctionBuilder()
.script("c_min = 1000; foreach (x : doc['snum'].values) { c_min = min(Integer.parseInt(x), c_min) } return c_min")))))
.query(functionScoreQuery(
termQuery("test", "value"),
scriptFunction("c_min = 1000; foreach (x : doc['snum'].values) { c_min = min(Integer.parseInt(x), c_min) } return c_min")))))
.actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
@ -378,9 +374,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource()
.explain(true)
.query(functionScoreQuery(termQuery("test", "value"))
.add(new ScriptScoreFunctionBuilder()
.script("c_min = 1000; foreach (x : doc['lnum'].values) { c_min = min(x, c_min) } return c_min")))))
.query(functionScoreQuery(
termQuery("test", "value"),
scriptFunction("c_min = 1000; foreach (x : doc['lnum'].values) { c_min = min(x, c_min) } return c_min")))))
.actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
@ -394,9 +390,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource()
.explain(true)
.query(functionScoreQuery(termQuery("test", "value"))
.add(new ScriptScoreFunctionBuilder()
.script("c_min = 1000; foreach (x : doc['dnum'].values) { c_min = min(x, c_min) } return c_min")))))
.query(functionScoreQuery(
termQuery("test", "value"),
scriptFunction("c_min = 1000; foreach (x : doc['dnum'].values) { c_min = min(x, c_min) } return c_min")))))
.actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
@ -410,9 +406,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource()
.explain(true)
.query(functionScoreQuery(termQuery("test", "value"))
.add(new ScriptScoreFunctionBuilder()
.script("c_min = 1000; foreach (x : doc['gp'].values) { c_min = min(x.lat, c_min) } return c_min")))))
.query(functionScoreQuery(
termQuery("test", "value"),
scriptFunction("c_min = 1000; foreach (x : doc['gp'].values) { c_min = min(x.lat, c_min) } return c_min")))))
.actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
@ -551,8 +547,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
SearchResponse response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("doc['num1'].value"))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("doc['num1'].value"))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -564,8 +559,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("-doc['num1'].value"))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("-doc['num1'].value"))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -577,8 +571,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("pow(doc['num1'].value, 2)"))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("pow(doc['num1'].value, 2)"))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -590,8 +583,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("max(doc['num1'].value, 1d)"))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("max(doc['num1'].value, 1d)"))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -603,8 +595,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("doc['num1'].value * _score"))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("doc['num1'].value * _score"))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -616,9 +607,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(
new ScriptScoreFunctionBuilder().script("param1 * param2 * _score").param("param1", 2)
.param("param2", 2))))).actionGet();
functionScoreQuery(termQuery("test", "value"), scriptFunction("param1 * param2 * _score")
.param("param1", 2).param("param2", 2))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -629,9 +619,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termFilter("test", "value")).add(
new ScriptScoreFunctionBuilder().script("param1 * param2 * _score").param("param1", 2)
.param("param2", 2))))).actionGet();
functionScoreQuery(termFilter("test", "value"),
scriptFunction("param1 * param2 * _score").param("param1", 2).param("param2", 2))))).actionGet();
assertThat(response.getHits().totalHits(), equalTo(2l));
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
@ -681,7 +670,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(fuzzyQuery("field", "value")).add(FilterBuilders.idsFilter("type").addIds("1"),
new FactorBuilder().boostFactor(3))).execute().actionGet();
factorFunction(3))).execute().actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
assertThat(searchResponse.getHits().totalHits(), equalTo(4l));
@ -886,8 +875,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery())
.add(termFilter("field", "value4"), new ScriptScoreFunctionBuilder().script("2")).add(
termFilter("field", "value2"), new ScriptScoreFunctionBuilder().script("3"))).setExplain(true)
.add(termFilter("field", "value4"), scriptFunction("2")).add(
termFilter("field", "value2"), scriptFunction("3"))).setExplain(true)
.execute().actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -906,8 +895,8 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
searchResponse = client()
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2)).add(
termFilter("field", "value2"), new FactorBuilder().boostFactor(3))).setExplain(true).execute().actionGet();
functionScoreQuery(matchAllQuery()).add(termFilter("field", "value4"), factorFunction(2)).add(
termFilter("field", "value2"), factorFunction(3))).setExplain(true).execute().actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -926,9 +915,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("sum")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -941,9 +930,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("max")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -968,9 +957,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("avg")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -986,9 +975,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("min")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -1007,9 +996,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(matchAllQuery()).scoreMode("multiply")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -1028,9 +1017,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(termsQuery("field", "value1", "value2", "value3", "value4")).scoreMode("first")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value3"), new FactorBuilder().boostFactor(3))
.add(termFilter("field", "value2"), new FactorBuilder().boostFactor(4))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value3"), factorFunction(3))
.add(termFilter("field", "value2"), factorFunction(4))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
@ -1049,9 +1038,9 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
.prepareSearch("test")
.setQuery(
functionScoreQuery(termsQuery("field", "value1", "value2", "value3", "value4")).scoreMode("multiply")
.add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2))
.add(termFilter("field", "value1"), new FactorBuilder().boostFactor(3))
.add(termFilter("color", "red"), new FactorBuilder().boostFactor(5))).setExplain(true).execute()
.add(termFilter("field", "value4"), factorFunction(2))
.add(termFilter("field", "value1"), factorFunction(3))
.add(termFilter("color", "red"), factorFunction(5))).setExplain(true).execute()
.actionGet();
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));

View File

@ -29,9 +29,7 @@ import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.MatchAllFilterBuilder;
import org.elasticsearch.index.query.functionscore.DecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.exp.ExponentialDecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.gauss.GaussDecayFunctionBuilder;
import org.elasticsearch.index.query.functionscore.lin.LinearDecayFunctionBuilder;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
@ -45,6 +43,7 @@ import static org.elasticsearch.client.Requests.searchRequest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.*;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.Matchers.*;
@ -91,7 +90,6 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
List<Float> lonlat = new ArrayList<Float>();
lonlat.add(new Float(20));
lonlat.add(new Float(11));
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("loc", lonlat, "1000km");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
@ -102,7 +100,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(fb))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", lonlat, "1000km")))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -110,7 +109,6 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat(sh.getAt(1).getId(), equalTo("2"));
// Test Exp
fb = new ExponentialDecayFunctionBuilder("loc", lonlat, "1000km");
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
@ -121,7 +119,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(fb))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), linearDecayFunction("loc", lonlat, "1000km")))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -129,7 +128,6 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat(sh.getAt(1).getId(), equalTo("2"));
// Test Lin
fb = new LinearDecayFunctionBuilder("loc", lonlat, "1000km");
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
@ -140,7 +138,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(fb))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), exponentialDecayFunction("loc", lonlat, "1000km")))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -174,14 +173,14 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
refresh();
// Test Gauss
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("num", 1.0, 5.0);
fb.setOffset(1.0);
ActionFuture<SearchResponse> response = client()
.search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.REPLACE.getName()))));
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource()
.explain(true)
.size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 1.0, 5.0).setOffset(1.0))
.boostMode(CombineFunction.REPLACE.getName()))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -193,14 +192,15 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
}
// Test Exp
fb = new ExponentialDecayFunctionBuilder("num", 1.0, 5.0);
fb.setOffset(1.0);
response = client()
.search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.REPLACE.getName()))));
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource()
.explain(true)
.size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value"),
exponentialDecayFunction("num", 1.0, 5.0).setOffset(1.0)).boostMode(
CombineFunction.REPLACE.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -211,14 +211,13 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
assertThat(sh.getAt(i + 2).getId(), equalTo(Integer.toString(i + 3)));
}
// Test Lin
fb = new LinearDecayFunctionBuilder("num", 1.0, 20.0);
fb.setOffset(1.0);
response = client()
.search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.REPLACE.getName()))));
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource()
.explain(true)
.size(numDummyDocs + 2)
.query(functionScoreQuery(termQuery("test", "value"), linearDecayFunction("num", 1.0, 20.0).setOffset(1.0))
.boostMode(CombineFunction.REPLACE.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
@ -257,12 +256,12 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
List<Float> lonlat = new ArrayList<Float>();
lonlat.add(new Float(20));
lonlat.add(new Float(11));
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("loc", lonlat, "1000km");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", lonlat, "1000km")).boostMode(
CombineFunction.MULT.getName()))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2)));
@ -273,7 +272,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.REPLACE.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", lonlat, "1000km")).boostMode(
CombineFunction.REPLACE.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2)));
@ -301,24 +301,23 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
indexRandom("test", false, builders);
refresh();
GeoPoint point = new GeoPoint(20, 11);
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("loc", point, "1000km");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", point, "1000km")).boostMode(
CombineFunction.MULT.getName()))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
assertThat(sh.getAt(0).getId(), equalTo("1"));
assertThat((double) sh.getAt(0).score(), closeTo(0.30685282, 1.e-5));
float[] coords = { 11, 20 };
fb = new GaussDecayFunctionBuilder("loc", coords, "1000km");
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boostMode(CombineFunction.MULT.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", coords, "1000km")).boostMode(
CombineFunction.MULT.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -340,14 +339,13 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
indexRandom("test", false, builders);
refresh();
DecayFunctionBuilder fb = new GaussDecayFunctionBuilder("num", 0.0, 1.0).setDecay(0.5);
// function score should return 0.5 for this function
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.MULT.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.MULT.getName()))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -358,8 +356,8 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.REPLACE.getName()))));
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.REPLACE.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -369,9 +367,9 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.SUM.getName()))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.SUM.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -381,9 +379,9 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.AVG.getName()))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.AVG.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -393,9 +391,9 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.MIN.getName()))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.MIN.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -405,9 +403,9 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true)
.query(functionScoreQuery(termQuery("test", "value")).add(fb).boost(2.0f)
.boostMode(CombineFunction.MAX.getName()))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 0.0, 1.0).setDecay(0.5)).boost(
2.0f).boostMode(CombineFunction.MAX.getName()))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (1)));
@ -430,11 +428,10 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
.source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-28").endObject())).actionGet();
refresh();
DecayFunctionBuilder gfb = new GaussDecayFunctionBuilder("num1", "2013-05-28", "-1d");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(functionScoreQuery(termQuery("test", "value")).add(gfb))));
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num1", "2013-05-28", "-1d")))));
SearchResponse sr = response.actionGet();
ElasticsearchAssertions.assertNoFailures(sr);
@ -478,14 +475,12 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
refresh();
DecayFunctionBuilder gfb1 = new LinearDecayFunctionBuilder("num1", "2013-05-28", "+3d");
DecayFunctionBuilder gfb2 = new LinearDecayFunctionBuilder("num2", "0.0", "1");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(false).query(
functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(), gfb1)
.add(new MatchAllFilterBuilder(), gfb2).scoreMode("multiply"))));
functionScoreQuery(termQuery("test", "value"))
.add(new MatchAllFilterBuilder(), linearDecayFunction("num1", "2013-05-28", "+3d"))
.add(new MatchAllFilterBuilder(), linearDecayFunction("num2", "0.0", "1")).scoreMode("multiply"))));
SearchResponse sr = response.actionGet();
ElasticsearchAssertions.assertNoFailures(sr);
@ -531,17 +526,17 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
List<Float> lonlat = new ArrayList<Float>();
lonlat.add(new Float(100));
lonlat.add(new Float(110));
DecayFunctionBuilder gfb1 = new LinearDecayFunctionBuilder("date", "2013-05-30", "+15d");
DecayFunctionBuilder gfb2 = new LinearDecayFunctionBuilder("geo", lonlat, "1000km");
DecayFunctionBuilder gfb3 = new LinearDecayFunctionBuilder("num", Integer.toString(numDocs), Integer.toString(numDocs / 2));
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource()
.explain(true)
.size(numDocs)
.query(functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(), gfb1)
.add(new MatchAllFilterBuilder(), gfb2).add(new MatchAllFilterBuilder(), gfb3)
.query(functionScoreQuery(termQuery("test", "value"))
.add(new MatchAllFilterBuilder(), linearDecayFunction("date", "2013-05-30", "+15d"))
.add(new MatchAllFilterBuilder(), linearDecayFunction("geo", lonlat, "1000km"))
.add(new MatchAllFilterBuilder(),
linearDecayFunction("num", Integer.toString(numDocs), Integer.toString(numDocs / 2)))
.scoreMode("multiply").boostMode(CombineFunction.REPLACE.getName()))));
SearchResponse sr = response.actionGet();
@ -573,14 +568,13 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
List<Float> lonlat = new ArrayList<Float>();
lonlat.add(new Float(100));
lonlat.add(new Float(110));
DecayFunctionBuilder gfb2 = new LinearDecayFunctionBuilder("type1.geo", lonlat, "1000km");
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource()
.explain(true)
.size(numDocs)
.query(functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(), gfb2).scoreMode(
"multiply"))));
.query(functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(),
linearDecayFunction("type1.geo", lonlat, "1000km")).scoreMode("multiply"))));
SearchResponse sr = response.actionGet();
}
@ -593,13 +587,12 @@ public class DecayFunctionScoreTests extends AbstractSharedClusterTest {
indexRequest("test").type("type").source(
jsonBuilder().startObject().field("test", "value").field("num", Integer.toString(1)).endObject())).actionGet();
refresh();
DecayFunctionBuilder lfb = new LinearDecayFunctionBuilder("num", Integer.toString(1), Integer.toString(1 / 2));
// so, we indexed a string field, but now we try to score a num field
ActionFuture<SearchResponse> response = client()
.search(searchRequest()
.searchType(SearchType.QUERY_THEN_FETCH)
.source(searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(), lfb).scoreMode("multiply"))));
ActionFuture<SearchResponse> response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true).query(
functionScoreQuery(termQuery("test", "value")).add(new MatchAllFilterBuilder(),
linearDecayFunction("num", Integer.toString(1), Integer.toString(1 / 2))).scoreMode("multiply"))));
response.actionGet();
}

View File

@ -21,7 +21,6 @@ package org.elasticsearch.test.integration.search.functionscore;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.functionscore.random.RandomScoreFunctionBuilder;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.hamcrest.CoreMatchers;
@ -33,6 +32,7 @@ import java.util.Arrays;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.randomFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@ -63,7 +63,7 @@ public class RandomScoreFunctionTests extends AbstractSharedClusterTest {
for (int i = 0; i < innerIters; i++) {
SearchResponse searchResponse = client().prepareSearch()
.setPreference(preference)
.setQuery(functionScoreQuery(matchAllQuery()).add(new RandomScoreFunctionBuilder().seed(seed)))
.setQuery(functionScoreQuery(matchAllQuery(), randomFunction(seed)))
.execute().actionGet();
assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, CoreMatchers.equalTo(0));
int hitCount = searchResponse.getHits().getHits().length;
@ -99,7 +99,7 @@ public class RandomScoreFunctionTests extends AbstractSharedClusterTest {
for (int i = 0; i < count; i++) {
SearchResponse searchResponse = client().prepareSearch()
.setQuery(functionScoreQuery(matchAllQuery()).add(new RandomScoreFunctionBuilder().seed(System.nanoTime())))
.setQuery(functionScoreQuery(matchAllQuery(), randomFunction(System.nanoTime())))
.execute().actionGet();
matrix[Integer.valueOf(searchResponse.getHits().getAt(0).id())]++;

View File

@ -31,7 +31,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.rescore.RescoreBuilder;
@ -40,6 +39,7 @@ import org.elasticsearch.test.integration.AbstractSharedClusterTest;
import org.junit.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
@ -482,9 +482,9 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.queryRescorer(
QueryBuilders.boolQuery()
.disableCoord(true)
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[0])).add(new ScriptScoreFunctionBuilder().script("5.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[1])).add(new ScriptScoreFunctionBuilder().script("7.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[3])).add(new ScriptScoreFunctionBuilder().script("0.0f")).boostMode(CombineFunction.REPLACE.getName())))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[0]), scriptFunction("5.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[1]), scriptFunction("7.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[3]), scriptFunction("0.0f")).boostMode(CombineFunction.REPLACE.getName())))
.setQueryWeight(primaryWeight)
.setRescoreQueryWeight(secondaryWeight);
@ -497,10 +497,10 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.setPreference("test") // ensure we hit the same shards for tie-breaking
.setQuery(QueryBuilders.boolQuery()
.disableCoord(true)
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[0])).add(new ScriptScoreFunctionBuilder().script("2.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[1])).add(new ScriptScoreFunctionBuilder().script("3.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[2])).add(new ScriptScoreFunctionBuilder().script("5.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[3])).add(new ScriptScoreFunctionBuilder().script("0.2f")).boostMode(CombineFunction.REPLACE.getName())))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[0]), scriptFunction("2.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[1]), scriptFunction("3.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[2]), scriptFunction("5.0f")).boostMode(CombineFunction.REPLACE.getName()))
.should(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", intToEnglish[3]), scriptFunction("0.2f")).boostMode(CombineFunction.REPLACE.getName())))
.setFrom(0)
.setSize(10)
.setRescorer(rescoreQuery)

View File

@ -34,7 +34,6 @@ import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.functionscore.script.ScriptScoreFunctionBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.ScriptSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
@ -50,6 +49,7 @@ import java.util.concurrent.ExecutionException;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
@ -279,21 +279,21 @@ public class SimpleSortTests extends AbstractSharedClusterTest {
refresh();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("_source.field"))).execute().actionGet();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery(), scriptFunction("_source.field"))).execute().actionGet();
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
assertThat(searchResponse.getHits().getAt(1).score(), Matchers.lessThan(searchResponse.getHits().getAt(0).score()));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.getHits().getAt(2).score(), Matchers.lessThan(searchResponse.getHits().getAt(1).score()));
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery(), scriptFunction("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
assertThat(searchResponse.getHits().getAt(1).score(), Matchers.lessThan(searchResponse.getHits().getAt(0).score()));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.getHits().getAt(2).score(), Matchers.lessThan(searchResponse.getHits().getAt(1).score()));
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery()).add(new ScriptScoreFunctionBuilder().script("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
searchResponse = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery(), scriptFunction("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));

View File

@ -56,7 +56,6 @@ import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MapperServiceModule;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.functionscore.FunctionScoreModule;
import org.elasticsearch.index.query.functionscore.factor.FactorBuilder;
import org.elasticsearch.index.search.NumericRangeFieldDataFilter;
import org.elasticsearch.index.search.geo.GeoDistanceFilter;
import org.elasticsearch.index.search.geo.GeoPolygonFilter;
@ -83,6 +82,7 @@ import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.index.query.RegexpFlag.*;
import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.factorFunction;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery;
import static org.hamcrest.Matchers.*;
@ -1473,7 +1473,7 @@ public class SimpleIndexQueryParserTests extends ElasticsearchTestCase {
@Test
public void testCustomBoostFactorQueryBuilder_withFunctionScore() throws IOException {
IndexQueryParserService queryParser = queryParser();
Query parsedQuery = queryParser.parse(functionScoreQuery(termQuery("name.last", "banon")).add(new FactorBuilder().boostFactor(1.3f))).query();
Query parsedQuery = queryParser.parse(functionScoreQuery(termQuery("name.last", "banon"), factorFunction(1.3f))).query();
assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
assertThat(((TermQuery) functionScoreQuery.getSubQuery()).getTerm(), equalTo(new Term("name.last", "banon")));