Search in sources :

Example 86 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class QueryProfilerIT method testProfileMatchesRegular.

/**
     * This test generates 1-10 random queries and executes a profiled and non-profiled
     * search for each query.  It then does some basic sanity checking of score and hits
     * to make sure the profiling doesn't interfere with the hits being returned
     */
public void testProfileMatchesRegular() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    List<String> stringFields = Arrays.asList("field1");
    List<String> numericFields = Arrays.asList("field2");
    indexRandom(true, docs);
    refresh();
    int iters = between(1, 10);
    for (int i = 0; i < iters; i++) {
        QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
        logger.info("Query: {}", q);
        SearchRequestBuilder vanilla = client().prepareSearch("test").setQuery(q).setProfile(false).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        SearchRequestBuilder profile = client().prepareSearch("test").setQuery(q).setProfile(true).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).execute().actionGet().getResponses();
        SearchResponse vanillaResponse = responses[0].getResponse();
        SearchResponse profileResponse = responses[1].getResponse();
        float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
        float profileMaxScore = profileResponse.getHits().getMaxScore();
        if (Float.isNaN(vanillaMaxScore)) {
            assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore));
        } else {
            assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
        }
        assertThat("Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
        SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
        SearchHit[] profileHits = profileResponse.getHits().getHits();
        for (int j = 0; j < vanillaHits.length; j++) {
            assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

Example 87 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class MoreLikeThisIT method testSimpleMoreLikeThisIds.

public void testSimpleMoreLikeThisIds() throws Exception {
    logger.info("Creating index test");
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("text").field("type", "text").endObject().endObject().endObject().endObject()));
    logger.info("Running Cluster Health");
    assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
    logger.info("Indexing...");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene").setId("1"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene release").setId("2"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "apache lucene").setId("3"));
    indexRandom(true, builders);
    logger.info("Running MoreLikeThis");
    MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery(new String[] { "text" }, null, ids("1")).include(true).minTermFreq(1).minDocFreq(1);
    SearchResponse mltResponse = client().prepareSearch().setTypes("type1").setQuery(queryBuilder).execute().actionGet();
    assertHitCount(mltResponse, 3L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 88 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class SimpleSearchIT method testSimpleTerminateAfterCount.

public void testSimpleTerminateAfterCount() throws Exception {
    prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0).get();
    ensureGreen();
    int max = randomIntBetween(3, 29);
    List<IndexRequestBuilder> docbuilders = new ArrayList<>(max);
    for (int i = 1; i <= max; i++) {
        String id = String.valueOf(i);
        docbuilders.add(client().prepareIndex("test", "type1", id).setSource("field", i));
    }
    indexRandom(true, docbuilders);
    ensureGreen();
    refresh();
    SearchResponse searchResponse;
    for (int i = 1; i <= max; i++) {
        searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte(1).lte(max)).setTerminateAfter(i).execute().actionGet();
        assertHitCount(searchResponse, i);
        assertTrue(searchResponse.isTerminatedEarly());
    }
    searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.rangeQuery("field").gte(1).lte(max)).setTerminateAfter(2 * max).execute().actionGet();
    assertHitCount(searchResponse, max);
    assertFalse(searchResponse.isTerminatedEarly());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 89 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class DuelScrollIT method create.

private TestContext create(SearchType... searchTypes) throws Exception {
    assertAcked(prepareCreate("index").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "long").endObject().startObject("field2").field("type", "keyword").endObject().startObject("nested").field("type", "nested").startObject("properties").startObject("field3").field("type", "long").endObject().startObject("field4").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject()));
    int numDocs = 2 + randomInt(512);
    int scrollRequestSize = randomIntBetween(1, rarely() ? numDocs : numDocs / 2);
    boolean unevenRouting = randomBoolean();
    int numMissingDocs = scaledRandomIntBetween(0, numDocs / 100);
    IntHashSet missingDocs = new IntHashSet(numMissingDocs);
    for (int i = 0; i < numMissingDocs; i++) {
        while (!missingDocs.add(randomInt(numDocs))) {
        }
    }
    for (int i = 1; i <= numDocs; i++) {
        IndexRequestBuilder indexRequestBuilder = client().prepareIndex("index", "type", String.valueOf(i));
        if (missingDocs.contains(i)) {
            indexRequestBuilder.setSource("x", "y");
        } else {
            indexRequestBuilder.setSource(jsonBuilder().startObject().field("field1", i).field("field2", String.valueOf(i)).startObject("nested").field("field3", i).field("field4", String.valueOf(i)).endObject().endObject());
        }
        if (unevenRouting && randomInt(3) <= 2) {
            indexRequestBuilder.setRouting("a");
        }
        indexRandom(false, indexRequestBuilder);
    }
    refresh();
    final SortBuilder sort;
    if (randomBoolean()) {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("field1").missing(1);
        } else {
            sort = SortBuilders.fieldSort("field2").missing("1");
        }
    } else {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("nested.field3").missing(1);
        } else {
            sort = SortBuilders.fieldSort("nested.field4").missing("1");
        }
    }
    sort.order(randomBoolean() ? SortOrder.ASC : SortOrder.DESC);
    SearchType searchType = RandomPicks.randomFrom(random(), Arrays.asList(searchTypes));
    logger.info("numDocs={}, scrollRequestSize={}, sort={}, searchType={}", numDocs, scrollRequestSize, sort, searchType);
    return new TestContext(numDocs, scrollRequestSize, sort, searchType);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SortBuilder(org.elasticsearch.search.sort.SortBuilder) IntHashSet(com.carrotsearch.hppc.IntHashSet) SearchType(org.elasticsearch.action.search.SearchType)

Example 90 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class QueryStringIT method testWithLotsOfTypes.

public void testWithLotsOfTypes() throws Exception {
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f_date", "2015/09/02", "f_float", "1.7", "f_ip", "127.0.0.1"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar", "f_date", "2015/09/01", "f_float", "1.8", "f_ip", "127.0.0.2"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(queryStringQuery("foo bar")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(queryStringQuery("\"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1");
    assertHitCount(resp, 1L);
    resp = client().prepareSearch("test").setQuery(queryStringQuery("127.0.0.2 \"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(queryStringQuery("127.0.0.1 1.8")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15