Search in sources :

Example 21 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testDateWithoutOrigin.

public void testDateWithoutOrigin() throws Exception {
    DateTime dt = new DateTime(DateTimeZone.UTC);
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num1").field("type", "date").endObject().endObject().endObject().endObject()));
    DateTime docDate = dt.minusDays(1);
    String docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    docDate = dt.minusDays(2);
    docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    docDate = dt.minusDays(3);
    docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    refresh();
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(QueryBuilders.matchAllQuery(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(linearDecayFunction("num1", null, "7000d")), new FilterFunctionBuilder(gaussDecayFunction("num1", null, "1d")), new FilterFunctionBuilder(exponentialDecayFunction("num1", null, "7000d")) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY))));
    SearchResponse sr = response.actionGet();
    assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(3));
    double[] scores = new double[4];
    for (int i = 0; i < sh.getHits().length; i++) {
        scores[Integer.parseInt(sh.getAt(i).getId()) - 1] = sh.getAt(i).getScore();
    }
    assertThat(scores[1], lessThan(scores[0]));
    assertThat(scores[2], lessThan(scores[1]));
}
Also used : FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) DateTime(org.joda.time.DateTime) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testParseGeoPoint.

public void testParseGeoPoint() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().endObject().endObject().endObject()));
    client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 20).field("lon", 11).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    FunctionScoreQueryBuilder baseQuery = functionScoreQuery(constantScoreQuery(termQuery("test", "value")), ScoreFunctionBuilders.weightFactorFunction(randomIntBetween(1, 10)));
    GeoPoint point = new GeoPoint(20, 11);
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", point, "1000km")).boostMode(CombineFunction.REPLACE))));
    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).getScore(), closeTo(1.0, 1.e-5));
    // this is equivalent to new GeoPoint(20, 11); just flipped so scores must be same
    float[] coords = { 11, 20 };
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", coords, "1000km")).boostMode(CombineFunction.REPLACE))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(1.0f, 1.e-5));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 23 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testDistanceScoreGeoLinGaussExpWithOffset.

public void testDistanceScoreGeoLinGaussExpWithOffset() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num").field("type", "double").endObject().endObject().endObject().endObject()));
    // add tw docs within offset
    List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
    indexBuilders.add(client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 0.5).endObject()));
    indexBuilders.add(client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 1.7).endObject()));
    // add docs outside offset
    int numDummyDocs = 20;
    for (int i = 0; i < numDummyDocs; i++) {
        indexBuilders.add(client().prepareIndex().setType("type1").setId(Integer.toString(i + 3)).setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 3.0 + i).endObject()));
    }
    indexRandom(true, indexBuilders);
    // Test Gauss
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 1.0, 5.0, 1.0)).boostMode(CombineFunction.REPLACE))));
    SearchResponse sr = response.actionGet();
    SearchHits sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
    for (int i = 0; i < numDummyDocs; i++) {
        assertThat(sh.getAt(i + 2).getId(), equalTo(Integer.toString(i + 3)));
    }
    // Test Exp
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), exponentialDecayFunction("num", 1.0, 5.0, 1.0)).boostMode(CombineFunction.REPLACE))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
    for (int i = 0; i < numDummyDocs; i++) {
        assertThat(sh.getAt(i + 2).getId(), equalTo(Integer.toString(i + 3)));
    }
    // Test Lin
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), linearDecayFunction("num", 1.0, 20.0, 1.0)).boostMode(CombineFunction.REPLACE))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
    assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
    assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 24 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testValueMissingLin.

public void testValueMissingLin() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num1").field("type", "date").endObject().startObject("num2").field("type", "double").endObject().endObject().endObject().endObject()));
    client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-27").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("test", "value").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-30").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("4").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-30").endObject())).actionGet();
    refresh();
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, new FilterFunctionBuilder[] { new FilterFunctionBuilder(linearDecayFunction("num1", "2013-05-28", "+3d")), new FilterFunctionBuilder(linearDecayFunction("num2", "0.0", "1")) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY))));
    SearchResponse sr = response.actionGet();
    assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(4));
    double[] scores = new double[4];
    for (int i = 0; i < sh.getHits().length; i++) {
        scores[Integer.parseInt(sh.getAt(i).getId()) - 1] = sh.getAt(i).getScore();
    }
    assertThat(scores[0], lessThan(scores[1]));
    assertThat(scores[2], lessThan(scores[3]));
}
Also used : FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 25 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class ExplainableScriptIT method testNativeExplainScript.

public void testNativeExplainScript() throws InterruptedException, IOException, ExecutionException {
    List<IndexRequestBuilder> indexRequests = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        indexRequests.add(client().prepareIndex("test", "type").setId(Integer.toString(i)).setSource(jsonBuilder().startObject().field("number_field", i).field("text", "text").endObject()));
    }
    indexRandom(true, true, indexRequests);
    client().admin().indices().prepareRefresh().execute().actionGet();
    ensureYellow();
    SearchResponse response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().explain(true).query(functionScoreQuery(termQuery("text", "text"), scriptFunction(new Script(ScriptType.INLINE, "native", "native_explainable_script", Collections.emptyMap()))).boostMode(CombineFunction.REPLACE)))).actionGet();
    ElasticsearchAssertions.assertNoFailures(response);
    SearchHits hits = response.getHits();
    assertThat(hits.getTotalHits(), equalTo(20L));
    int idCounter = 19;
    for (SearchHit hit : hits.getHits()) {
        assertThat(hit.getId(), equalTo(Integer.toString(idCounter)));
        assertThat(hit.getExplanation().toString(), containsString(Double.toString(idCounter) + " = This script returned " + Double.toString(idCounter)));
        assertThat(hit.getExplanation().toString(), containsString("freq=1.0 = termFreq=1.0"));
        assertThat(hit.getExplanation().getDetails().length, equalTo(2));
        idCounter--;
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript) Script(org.elasticsearch.script.Script) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)136 SearchResponse (org.elasticsearch.action.search.SearchResponse)92 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)24 Map (java.util.Map)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 HashMap (java.util.HashMap)19 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 SearchRequest (org.elasticsearch.action.search.SearchRequest)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 TotalHits (org.apache.lucene.search.TotalHits)12 Test (org.junit.Test)12 ScoreDoc (org.apache.lucene.search.ScoreDoc)11 Text (org.elasticsearch.common.text.Text)11 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)10