|
|
|
@ -20,7 +20,6 @@
|
|
|
|
|
package org.elasticsearch.test.integration.search.customscore;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.lucene.search.Explanation;
|
|
|
|
|
import org.elasticsearch.ElasticSearchException;
|
|
|
|
|
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
|
|
|
@ -85,7 +84,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertNotNull(explanation);
|
|
|
|
|
assertThat(explanation.isMatch(), equalTo(true));
|
|
|
|
|
assertThat(explanation.getValue(), equalTo(3f));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("custom score, score mode [first]"));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("function score, score mode [first]"));
|
|
|
|
|
|
|
|
|
|
assertThat(explanation.getDetails().length, equalTo(2));
|
|
|
|
|
assertThat(explanation.getDetails()[0].isMatch(), equalTo(true));
|
|
|
|
@ -115,7 +114,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertNotNull(explanation);
|
|
|
|
|
assertThat(explanation.isMatch(), equalTo(true));
|
|
|
|
|
assertThat(explanation.getValue(), equalTo(6f));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("custom score, score mode [first]"));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("function score, score mode [first]"));
|
|
|
|
|
|
|
|
|
|
assertThat(explanation.getDetails().length, equalTo(2));
|
|
|
|
|
assertThat(explanation.getDetails()[0].isMatch(), equalTo(true));
|
|
|
|
@ -127,8 +126,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(explanation.getDetails()[1].getDetails()[2].getDescription(), equalTo("queryBoost"));
|
|
|
|
|
assertThat(explanation.getDetails()[1].getDetails()[2].getValue(), equalTo(2f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testScoreExplainBug_2283_withFunctionScore() throws Exception {
|
|
|
|
@ -159,7 +157,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertNotNull(explanation);
|
|
|
|
|
assertThat(explanation.isMatch(), equalTo(true));
|
|
|
|
|
assertThat(explanation.getValue(), equalTo(3f));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("custom score, score mode [first]"));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("function score, score mode [first]"));
|
|
|
|
|
|
|
|
|
|
assertThat(explanation.getDetails().length, equalTo(2));
|
|
|
|
|
assertThat(explanation.getDetails()[0].isMatch(), equalTo(true));
|
|
|
|
@ -185,7 +183,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertNotNull(explanation);
|
|
|
|
|
assertThat(explanation.isMatch(), equalTo(true));
|
|
|
|
|
assertThat(explanation.getValue(), equalTo(6f));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("custom score, score mode [first]"));
|
|
|
|
|
assertThat(explanation.getDescription(), equalTo("function score, score mode [first]"));
|
|
|
|
|
|
|
|
|
|
assertThat(explanation.getDetails().length, equalTo(2));
|
|
|
|
|
assertThat(explanation.getDetails()[0].isMatch(), equalTo(true));
|
|
|
|
@ -197,207 +195,206 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(explanation.getDetails()[1].getDetails()[2].getDescription(), equalTo("queryBoost"));
|
|
|
|
|
assertThat(explanation.getDetails()[1].getDetails()[2].getValue(), equalTo(2f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMultiValueCustomScriptBoost() throws ElasticSearchException, IOException {
|
|
|
|
|
client().admin().indices().prepareDelete().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
client().admin().indices().prepareCreate("test")
|
|
|
|
|
.setSettings(settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
|
|
|
|
|
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
|
|
|
|
|
.startObject("snum").field("type", "string").endObject()
|
|
|
|
|
.startObject("dnum").field("type", "double").endObject()
|
|
|
|
|
.startObject("slnum").field("type", "long").endObject()
|
|
|
|
|
.startObject("gp").field("type", "geo_point").endObject()
|
|
|
|
|
.endObject().endObject().endObject())
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
String[] values = new String[100];
|
|
|
|
|
String[] gp = new String[100];
|
|
|
|
|
client().admin().indices().prepareDelete().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
long[] lValues = new long[100];
|
|
|
|
|
double[] dValues = new double[100];
|
|
|
|
|
int offset = 1;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = ""+ (i + offset);
|
|
|
|
|
gp[i] = ""+ (i + offset) + ","+ (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("1")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
offset++;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = ""+ (i + offset);
|
|
|
|
|
gp[i] = ""+ (i + offset) + ","+ (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("2")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
client().admin().indices().prepareCreate("test")
|
|
|
|
|
.setSettings(settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
|
|
|
|
|
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
|
|
|
|
|
.startObject("snum").field("type", "string").endObject()
|
|
|
|
|
.startObject("dnum").field("type", "double").endObject()
|
|
|
|
|
.startObject("slnum").field("type", "long").endObject()
|
|
|
|
|
.startObject("gp").field("type", "geo_point").endObject()
|
|
|
|
|
.endObject().endObject().endObject())
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
logger.info("running min(doc['num1'].value)");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("c_min = 1000; foreach (x : doc['snum'].values) { c_min = min(Integer.parseInt(x), c_min) } return c_min")))
|
|
|
|
|
).actionGet();
|
|
|
|
|
String[] values = new String[100];
|
|
|
|
|
String[] gp = new String[100];
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("c_min = 1000; foreach (x : doc['lnum'].values) { c_min = min(x, c_min) } return c_min")))
|
|
|
|
|
).actionGet();
|
|
|
|
|
long[] lValues = new long[100];
|
|
|
|
|
double[] dValues = new double[100];
|
|
|
|
|
int offset = 1;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = "" + (i + offset);
|
|
|
|
|
gp[i] = "" + (i + offset) + "," + (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("1")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
offset++;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = "" + (i + offset);
|
|
|
|
|
gp[i] = "" + (i + offset) + "," + (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("2")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("c_min = 1000; foreach (x : doc['dnum'].values) { c_min = min(x, c_min) } return c_min")))
|
|
|
|
|
).actionGet();
|
|
|
|
|
logger.info("running min(doc['num1'].value)");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("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));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("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));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("c_min = 1000; foreach (x : doc['lnum'].values) { c_min = min(x, c_min) } return c_min")))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("c_min = 1000; foreach (x : doc['dnum'].values) { c_min = min(x, c_min) } return c_min")))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
|
.source(searchSource().explain(true).query(customScoreQuery(termQuery("test", "value"))
|
|
|
|
|
.script("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));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMultiValueCustomScriptBoost_withFunctionScore() throws ElasticSearchException, IOException {
|
|
|
|
|
client().admin().indices().prepareDelete().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
client().admin().indices().prepareCreate("test")
|
|
|
|
|
.setSettings(settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
|
|
|
|
|
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
|
|
|
|
|
.startObject("snum").field("type", "string").endObject()
|
|
|
|
|
.startObject("dnum").field("type", "double").endObject()
|
|
|
|
|
.startObject("slnum").field("type", "long").endObject()
|
|
|
|
|
.startObject("gp").field("type", "geo_point").endObject()
|
|
|
|
|
.endObject().endObject().endObject())
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
String[] values = new String[100];
|
|
|
|
|
String[] gp = new String[100];
|
|
|
|
|
client().admin().indices().prepareDelete().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
long[] lValues = new long[100];
|
|
|
|
|
double[] dValues = new double[100];
|
|
|
|
|
int offset = 1;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = ""+ (i + offset);
|
|
|
|
|
gp[i] = ""+ (i + offset) + ","+ (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("1")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
offset++;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = ""+ (i + offset);
|
|
|
|
|
gp[i] = ""+ (i + offset) + ","+ (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("2")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
client().admin().indices().prepareCreate("test")
|
|
|
|
|
.setSettings(settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
|
|
|
|
|
.addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
|
|
|
|
|
.startObject("snum").field("type", "string").endObject()
|
|
|
|
|
.startObject("dnum").field("type", "double").endObject()
|
|
|
|
|
.startObject("slnum").field("type", "long").endObject()
|
|
|
|
|
.startObject("gp").field("type", "geo_point").endObject()
|
|
|
|
|
.endObject().endObject().endObject())
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
|
|
|
|
|
|
|
|
|
|
logger.info("running min(doc['num1'].value)");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
String[] values = new String[100];
|
|
|
|
|
String[] gp = new String[100];
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
long[] lValues = new long[100];
|
|
|
|
|
double[] dValues = new double[100];
|
|
|
|
|
int offset = 1;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = "" + (i + offset);
|
|
|
|
|
gp[i] = "" + (i + offset) + "," + (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("1")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
offset++;
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
|
values[i] = "" + (i + offset);
|
|
|
|
|
gp[i] = "" + (i + offset) + "," + (i + offset);
|
|
|
|
|
lValues[i] = (i + offset);
|
|
|
|
|
dValues[i] = (i + offset);
|
|
|
|
|
}
|
|
|
|
|
client().index(indexRequest("test").type("type1").id("2")
|
|
|
|
|
.source(jsonBuilder().startObject().field("test", "value check")
|
|
|
|
|
.field("snum", values)
|
|
|
|
|
.field("dnum", dValues)
|
|
|
|
|
.field("lnum", lValues)
|
|
|
|
|
.field("gp", gp)
|
|
|
|
|
.endObject())).actionGet();
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
logger.info("running min(doc['num1'].value)");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
|
|
|
|
|
response = client().search(searchRequest()
|
|
|
|
|
.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"))))
|
|
|
|
|
).actionGet();
|
|
|
|
|
|
|
|
|
|
assertThat(response.getHits().totalHits(), equalTo(2l));
|
|
|
|
|
logger.info("Hit[0] {} Explanation {}", response.getHits().getAt(0).id(), response.getHits().getAt(0).explanation());
|
|
|
|
|
logger.info("Hit[1] {} Explanation {}", response.getHits().getAt(1).id(), response.getHits().getAt(1).explanation());
|
|
|
|
|
assertThat(response.getHits().getAt(0).id(), equalTo("2"));
|
|
|
|
|
assertThat(response.getHits().getAt(1).id(), equalTo("1"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ -412,7 +409,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
|
|
|
|
|
logger.info("--- QUERY_THEN_FETCH");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("running doc['num1'].value");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
@ -500,7 +497,6 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(response.getHits().getAt(1).score(), equalTo(4f)); // _score is always 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testCustomScriptBoost_withFunctionScore() throws Exception {
|
|
|
|
@ -514,7 +510,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
client().admin().indices().refresh(refreshRequest()).actionGet();
|
|
|
|
|
|
|
|
|
|
logger.info("--- QUERY_THEN_FETCH");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("running doc['num1'].value");
|
|
|
|
|
SearchResponse response = client().search(searchRequest()
|
|
|
|
|
.searchType(SearchType.QUERY_THEN_FETCH)
|
|
|
|
@ -621,7 +617,6 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(searchResponse.getHits().totalHits(), equalTo(4l));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testTriggerBooleanScorer_withFunctionScore() throws Exception {
|
|
|
|
@ -634,7 +629,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
client().prepareIndex("test", "type", "4").setSource("field", "value4", "color", "blue").execute().actionGet();
|
|
|
|
|
client().admin().indices().prepareRefresh().execute().actionGet();
|
|
|
|
|
SearchResponse searchResponse = client().prepareSearch("test")
|
|
|
|
|
.setQuery(functionScoreQuery(fuzzyQuery("field", "value")).add(FilterBuilders.idsFilter("type").addIds("1"), new FactorBuilder().boostFactor( 3)))
|
|
|
|
|
.setQuery(functionScoreQuery(fuzzyQuery("field", "value")).add(FilterBuilders.idsFilter("type").addIds("1"), new FactorBuilder().boostFactor(3)))
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0));
|
|
|
|
|
|
|
|
|
@ -852,7 +847,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(searchResponse.getHits().getAt(3).score(), equalTo(1.0f));
|
|
|
|
|
|
|
|
|
|
searchResponse = client().prepareSearch("test")
|
|
|
|
|
.setQuery(functionScoreQuery(matchAllQuery()).add(termFilter("field", "value4"), new FactorBuilder().boostFactor( 2)).add(termFilter("field", "value2"), new FactorBuilder().boostFactor( 3)))
|
|
|
|
|
.setQuery(functionScoreQuery(matchAllQuery()).add(termFilter("field", "value4"), new FactorBuilder().boostFactor(2)).add(termFilter("field", "value2"), new FactorBuilder().boostFactor(3)))
|
|
|
|
|
.setExplain(true)
|
|
|
|
|
.execute().actionGet();
|
|
|
|
|
|
|
|
|
@ -870,7 +865,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(searchResponse.getHits().getAt(3).score(), equalTo(1.0f));
|
|
|
|
|
|
|
|
|
|
searchResponse = client().prepareSearch("test")
|
|
|
|
|
.setQuery(functionScoreQuery(matchAllQuery()).scoreMode("total").add(termFilter("field", "value4"), new FactorBuilder().boostFactor( 2)).add(termFilter("field", "value1"), new FactorBuilder().boostFactor( 3)).add(termFilter("color", "red"), new FactorBuilder().boostFactor( 5)))
|
|
|
|
|
.setQuery(functionScoreQuery(matchAllQuery()).scoreMode("total").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().actionGet();
|
|
|
|
|
|
|
|
|
@ -881,7 +876,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
logger.info("--> Hit[0] {} Explanation {}", searchResponse.getHits().getAt(0).id(), searchResponse.getHits().getAt(0).explanation());
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
@ -892,7 +887,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
logger.info("--> Hit[0] {} Explanation {}", searchResponse.getHits().getAt(0).id(), searchResponse.getHits().getAt(0).explanation());
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
@ -906,7 +901,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
logger.info("--> Hit[1] {} Explanation {}", searchResponse.getHits().getAt(1).id(), searchResponse.getHits().getAt(1).explanation());
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
@ -923,7 +918,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(searchResponse.getHits().getAt(3).score(), equalTo(1.0f));
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
@ -940,7 +935,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
assertThat(searchResponse.getHits().getAt(3).score(), equalTo(1.0f));
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
@ -958,7 +953,7 @@ public class CustomScoreSearchTests extends AbstractSharedClusterTest {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
searchResponse = client().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)))
|
|
|
|
|
.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().actionGet();
|
|
|
|
|
|
|
|
|
|