Skip to content

Commit

Permalink
function_score: fix explanation, [ was missing
Browse files Browse the repository at this point in the history
  • Loading branch information
brwe authored and areek committed Sep 8, 2014
1 parent 5a710b7 commit d2ea7f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Expand Up @@ -381,6 +381,7 @@ protected double distance(int docId) {
protected String getDistanceString(int docId) {

StringBuilder values = new StringBuilder(mode.name());
values.append("[");
doubleValues.setDocument(docId);
final int num = doubleValues.count();
if (num > 0) {
Expand Down
Expand Up @@ -45,6 +45,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.client.Requests.indexRequest;
import static org.elasticsearch.client.Requests.searchRequest;
Expand Down Expand Up @@ -938,4 +939,33 @@ public void testMissingFunctionThrowsElasticsearchParseException() throws IOExce
assertTrue(failure.getMessage().contains("function must not be null"));
}
}

@Test
public void testExplainString() throws IOException, ExecutionException, InterruptedException {
assertAcked(prepareCreate("test").addMapping(
"type1",
jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "string")
.endObject().startObject("num").field("type", "double").endObject().endObject().endObject().endObject()));
ensureYellow();


client().prepareIndex().setType("type1").setId("1").setIndex("test")
.setSource(jsonBuilder().startObject().field("test", "value").array("num", 0.5, 0.7).endObject()).get();

refresh();

SearchResponse response = client().search(
searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(
searchSource().explain(true)
.query(functionScoreQuery(termQuery("test", "value"))
.add(gaussDecayFunction("num", 1.0, 5.0).setOffset(1.0))
.add(linearDecayFunction("num", 1.0, 5.0).setOffset(1.0))
.add(exponentialDecayFunction("num", 1.0, 5.0).setOffset(1.0))
.boostMode(CombineFunction.REPLACE.getName())))).get();
String explanation = response.getHits().getAt(0).getExplanation().toString();
assertThat(explanation, containsString(" 1.0 = -exp(-0.5*pow(MIN[Math.max(Math.abs(0.5(=doc value) - 1.0(=origin))) - 1.0(=offset), 0), Math.max(Math.abs(0.7(=doc value) - 1.0(=origin))) - 1.0(=offset), 0)],2.0)/18.033688011112044)"));
assertThat(explanation, containsString("1.0 = max(0.0, ((10.0 - MIN[Math.max(Math.abs(0.5(=doc value) - 1.0(=origin))) - 1.0(=offset), 0), Math.max(Math.abs(0.7(=doc value) - 1.0(=origin))) - 1.0(=offset), 0)])/10.0)"));
assertThat(explanation, containsString("1.0 = exp(- MIN[Math.max(Math.abs(0.5(=doc value) - 1.0(=origin))) - 1.0(=offset), 0), Math.max(Math.abs(0.7(=doc value) - 1.0(=origin))) - 1.0(=offset), 0)] * 0.13862943611198905)"));

}
}

0 comments on commit d2ea7f8

Please sign in to comment.