Search in sources :

Example 61 with SearchHit

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

the class RandomScoreFunctionIT method testScoreAccessWithinScript.

public void testScoreAccessWithinScript() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type", "body", "type=text", "index", "type=" + randomFrom("short", "float", "long", "integer", "double")));
    int docCount = randomIntBetween(100, 200);
    for (int i = 0; i < docCount; i++) {
        client().prepareIndex("test", "type", "" + i).setSource("body", randomFrom(Arrays.asList("foo", "bar", "baz")), "index", i + 1).get();
    }
    refresh();
    Map<String, Object> params = new HashMap<>();
    params.put("factor", randomIntBetween(2, 4));
    // Test for accessing _score
    Script script = new Script(ScriptType.INLINE, NAME, "log(doc['index'].value + (factor * _score))", params);
    SearchResponse resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("body", "foo"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueFactorFunction("index").factor(2)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(scriptFunction(script)) })).get();
    assertNoFailures(resp);
    SearchHit firstHit = resp.getHits().getAt(0);
    assertThat(firstHit.getScore(), greaterThan(1f));
    // Test for accessing _score.intValue()
    script = new Script(ScriptType.INLINE, NAME, "log(doc['index'].value + (factor * _score.intValue()))", params);
    resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("body", "foo"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueFactorFunction("index").factor(2)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(scriptFunction(script)) })).get();
    assertNoFailures(resp);
    firstHit = resp.getHits().getAt(0);
    assertThat(firstHit.getScore(), greaterThan(1f));
    // Test for accessing _score.longValue()
    script = new Script(ScriptType.INLINE, NAME, "log(doc['index'].value + (factor * _score.longValue()))", params);
    resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("body", "foo"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueFactorFunction("index").factor(2)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(scriptFunction(script)) })).get();
    assertNoFailures(resp);
    firstHit = resp.getHits().getAt(0);
    assertThat(firstHit.getScore(), greaterThan(1f));
    // Test for accessing _score.floatValue()
    script = new Script(ScriptType.INLINE, NAME, "log(doc['index'].value + (factor * _score.floatValue()))", params);
    resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("body", "foo"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueFactorFunction("index").factor(2)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(scriptFunction(script)) })).get();
    assertNoFailures(resp);
    firstHit = resp.getHits().getAt(0);
    assertThat(firstHit.getScore(), greaterThan(1f));
    // Test for accessing _score.doubleValue()
    script = new Script(ScriptType.INLINE, NAME, "log(doc['index'].value + (factor * _score.doubleValue()))", params);
    resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchQuery("body", "foo"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueFactorFunction("index").factor(2)), new FunctionScoreQueryBuilder.FilterFunctionBuilder(scriptFunction(script)) })).get();
    assertNoFailures(resp);
    firstHit = resp.getHits().getAt(0);
    assertThat(firstHit.getScore(), greaterThan(1f));
}
Also used : Script(org.elasticsearch.script.Script) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 62 with SearchHit

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

the class RandomScoreFunctionIT method testConsistentHitsWithSameSeed.

public void testConsistentHitsWithSameSeed() throws Exception {
    createIndex("test");
    // make sure we are done otherwise preference could change?
    ensureGreen();
    int docCount = randomIntBetween(100, 200);
    for (int i = 0; i < docCount; i++) {
        index("test", "type", "" + i, jsonBuilder().startObject().endObject());
    }
    flush();
    refresh();
    int outerIters = scaledRandomIntBetween(10, 20);
    for (int o = 0; o < outerIters; o++) {
        final int seed = randomInt();
        // at least one char!!
        String preference = randomRealisticUnicodeOfLengthBetween(1, 10);
        // randomPreference should not start with '_' (reserved for known preference types (e.g. _shards, _primary)
        while (preference.startsWith("_")) {
            preference = randomRealisticUnicodeOfLengthBetween(1, 10);
        }
        int innerIters = scaledRandomIntBetween(2, 5);
        SearchHit[] hits = null;
        for (int i = 0; i < innerIters; i++) {
            SearchResponse searchResponse = client().prepareSearch().setSize(// get all docs otherwise we are prone to tie-breaking
            docCount).setPreference(preference).setQuery(functionScoreQuery(matchAllQuery(), randomFunction(seed))).execute().actionGet();
            assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, CoreMatchers.equalTo(0));
            final int hitCount = searchResponse.getHits().getHits().length;
            final SearchHit[] currentHits = searchResponse.getHits().getHits();
            ArrayUtil.timSort(currentHits, (o1, o2) -> {
                // for tie-breaking we have to resort here since if the score is
                // identical we rely on collection order which might change.
                int cmp = Float.compare(o1.getScore(), o2.getScore());
                return cmp == 0 ? o1.getId().compareTo(o2.getId()) : cmp;
            });
            if (i == 0) {
                assertThat(hits, nullValue());
                hits = currentHits;
            } else {
                assertThat(hits.length, equalTo(searchResponse.getHits().getHits().length));
                for (int j = 0; j < hitCount; j++) {
                    assertThat("" + j, currentHits[j].getScore(), equalTo(hits[j].getScore()));
                    assertThat("" + j, currentHits[j].getId(), equalTo(hits[j].getId()));
                }
            }
            // randomly change some docs to get them in different segments
            int numDocsToChange = randomIntBetween(20, 50);
            while (numDocsToChange > 0) {
                // watch out this is inclusive the max values!
                int doc = randomInt(docCount - 1);
                index("test", "type", "" + doc, jsonBuilder().startObject().endObject());
                --numDocsToChange;
            }
            flush();
            refresh();
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 63 with SearchHit

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

the class GeoBoundingBoxIT method testSimpleBoundingBoxTest.

public void testSimpleBoundingBoxTest() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_point");
    xContentBuilder.endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "New York").startObject("location").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject()).execute().actionGet();
    // to NY: 5.286 km
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("name", "Times Square").startObject("location").field("lat", 40.759011).field("lon", -73.9844722).endObject().endObject()).execute().actionGet();
    // to NY: 0.4621 km
    client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().field("name", "Tribeca").startObject("location").field("lat", 40.718266).field("lon", -74.007819).endObject().endObject()).execute().actionGet();
    // to NY: 1.055 km
    client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().field("name", "Wall Street").startObject("location").field("lat", 40.7051157).field("lon", -74.0088305).endObject().endObject()).execute().actionGet();
    // to NY: 1.258 km
    client().prepareIndex("test", "type1", "5").setSource(jsonBuilder().startObject().field("name", "Soho").startObject("location").field("lat", 40.7247222).field("lon", -74).endObject().endObject()).execute().actionGet();
    // to NY: 2.029 km
    client().prepareIndex("test", "type1", "6").setSource(jsonBuilder().startObject().field("name", "Greenwich Village").startObject("location").field("lat", 40.731033).field("lon", -73.9962255).endObject().endObject()).execute().actionGet();
    // to NY: 8.572 km
    client().prepareIndex("test", "type1", "7").setSource(jsonBuilder().startObject().field("name", "Brooklyn").startObject("location").field("lat", 40.65).field("lon", -73.95).endObject().endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchResponse searchResponse = // from NY
    client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99)).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
    }
    searchResponse = // from NY
    client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99).type("indexed")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(2));
    for (SearchHit hit : searchResponse.getHits()) {
        assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Version(org.elasticsearch.Version) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 64 with SearchHit

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

the class DuelScrollIT method testDuelIndexOrder.

private void testDuelIndexOrder(SearchType searchType, boolean trackScores, int numDocs) throws Exception {
    final int size = scaledRandomIntBetween(5, numDocs + 5);
    final SearchResponse control = client().prepareSearch("test").setSearchType(searchType).setSize(numDocs).setQuery(QueryBuilders.matchQuery("foo", "true")).addSort(SortBuilders.fieldSort("_doc")).setTrackScores(trackScores).get();
    assertNoFailures(control);
    SearchResponse scroll = client().prepareSearch("test").setSearchType(searchType).setSize(size).setQuery(QueryBuilders.matchQuery("foo", "true")).addSort(SortBuilders.fieldSort("_doc")).setTrackScores(trackScores).setScroll("10m").get();
    int scrollDocs = 0;
    try {
        while (true) {
            assertNoFailures(scroll);
            assertEquals(control.getHits().getTotalHits(), scroll.getHits().getTotalHits());
            assertEquals(control.getHits().getMaxScore(), scroll.getHits().getMaxScore(), 0.01f);
            if (scroll.getHits().getHits().length == 0) {
                break;
            }
            for (int i = 0; i < scroll.getHits().getHits().length; ++i) {
                SearchHit controlHit = control.getHits().getAt(scrollDocs + i);
                SearchHit scrollHit = scroll.getHits().getAt(i);
                assertEquals(controlHit.getId(), scrollHit.getId());
            }
            scrollDocs += scroll.getHits().getHits().length;
            scroll = client().prepareSearchScroll(scroll.getScrollId()).setScroll("10m").get();
        }
        assertEquals(control.getHits().getTotalHits(), scrollDocs);
    } catch (AssertionError e) {
        logger.info("Control:\n{}", control);
        logger.info("Scroll size={}, from={}:\n{}", size, scrollDocs, scroll);
        throw e;
    } finally {
        clearScroll(scroll.getScrollId());
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 65 with SearchHit

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

the class SearchQueryIT method testConstantScoreQuery.

// see #3521
public void testConstantScoreQuery() throws Exception {
    Random random = random();
    createIndex("test");
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox"));
    SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get();
    assertHitCount(searchResponse, 2L);
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        assertSearchHit(searchHit, hasScore(1.0f));
    }
    searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat()))).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    client().prepareSearch("test").setQuery(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat())).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat()))))).get();
    assertHitCount(searchResponse, 2L);
    assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        assertSearchHit(searchHit, hasScore(1.0f));
    }
    int num = scaledRandomIntBetween(100, 200);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[num];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test_1", "type", "" + i).setSource("f", English.intToEnglish(i));
    }
    createIndex("test_1");
    indexRandom(true, builders);
    int queryRounds = scaledRandomIntBetween(10, 20);
    for (int i = 0; i < queryRounds; i++) {
        MatchQueryBuilder matchQuery = matchQuery("f", English.intToEnglish(between(0, num)));
        searchResponse = client().prepareSearch("test_1").setQuery(constantScoreQuery(matchQuery)).setSize(num).get();
        long totalHits = searchResponse.getHits().getTotalHits();
        SearchHits hits = searchResponse.getHits();
        for (SearchHit searchHit : hits) {
            assertSearchHit(searchHit, hasScore(1.0f));
        }
        searchResponse = client().prepareSearch("test_1").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get();
        hits = searchResponse.getHits();
        assertThat(hits.getTotalHits(), equalTo(totalHits));
        if (totalHits > 1) {
            float expected = hits.getAt(0).getScore();
            for (SearchHit searchHit : hits) {
                assertSearchHit(searchHit, hasScore(expected));
            }
        }
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Random(java.util.Random) SearchHit(org.elasticsearch.search.SearchHit) ElasticsearchAssertions.assertSearchHit(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)166 SearchResponse (org.elasticsearch.action.search.SearchResponse)114 SearchHits (org.elasticsearch.search.SearchHits)52 ArrayList (java.util.ArrayList)31 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)15 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)13 Map (java.util.Map)12 Test (org.junit.Test)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)9 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)9