Search in sources :

Example 11 with IndexRequestBuilder

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

the class SearchScrollWithFailingNodesIT method testScanScrollWithShardExceptions.

public void testScanScrollWithShardExceptions() throws Exception {
    internalCluster().startNode();
    internalCluster().startNode();
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), 1)));
    List<IndexRequestBuilder> writes = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        writes.add(client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject().field("field", i).endObject()));
    }
    indexRandom(false, writes);
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
    assertAllSuccessful(searchResponse);
    long numHits = 0;
    do {
        numHits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
        assertAllSuccessful(searchResponse);
    } while (searchResponse.getHits().getHits().length > 0);
    assertThat(numHits, equalTo(100L));
    clearScroll("_all");
    internalCluster().stopRandomNonMasterNode();
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
    assertThat(searchResponse.getSuccessfulShards(), lessThan(searchResponse.getTotalShards()));
    numHits = 0;
    int numberOfSuccessfulShards = searchResponse.getSuccessfulShards();
    do {
        numHits += searchResponse.getHits().getHits().length;
        searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
        assertThat(searchResponse.getSuccessfulShards(), equalTo(numberOfSuccessfulShards));
    } while (searchResponse.getHits().getHits().length > 0);
    assertThat(numHits, greaterThan(0L));
    clearScroll(searchResponse.getScrollId());
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 12 with IndexRequestBuilder

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

the class FieldSortIT method testSimpleSorts.

public void testSimpleSorts() throws Exception {
    Random random = random();
    assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("str_value").field("type", "keyword").endObject().startObject("boolean_value").field("type", "boolean").endObject().startObject("byte_value").field("type", "byte").endObject().startObject("short_value").field("type", "short").endObject().startObject("integer_value").field("type", "integer").endObject().startObject("long_value").field("type", "long").endObject().startObject("float_value").field("type", "float").endObject().startObject("double_value").field("type", "double").endObject().endObject().endObject().endObject()));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        IndexRequestBuilder builder = client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("str_value", new String(new char[] { (char) (97 + i), (char) (97 + i) })).field("boolean_value", true).field("byte_value", i).field("short_value", i).field("integer_value", i).field("long_value", i).field("float_value", 0.1 * i).field("double_value", 0.1 * i).endObject());
        builders.add(builder);
    }
    Collections.shuffle(builders, random);
    for (IndexRequestBuilder builder : builders) {
        builder.execute().actionGet();
        if (random.nextBoolean()) {
            if (random.nextInt(5) != 0) {
                refresh();
            } else {
                client().admin().indices().prepareFlush().execute().actionGet();
            }
        }
    }
    refresh();
    // STRING
    int size = 1 + random.nextInt(10);
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(searchResponse.getHits().getAt(i).getSortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + i), (char) (97 + i) })));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(searchResponse.getHits().getAt(i).getSortValues()[0].toString(), equalTo(new String(new char[] { (char) (97 + (9 - i)), (char) (97 + (9 - i)) })));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // BYTE
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("byte_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).byteValue(), equalTo((byte) i));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("byte_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).byteValue(), equalTo((byte) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // SHORT
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("short_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).shortValue(), equalTo((short) i));
    }
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("short_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).shortValue(), equalTo((short) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // INTEGER
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("integer_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).intValue(), equalTo(i));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("integer_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).intValue(), equalTo((9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // LONG
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("long_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).longValue(), equalTo((long) i));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("long_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).longValue(), equalTo((long) (9 - i)));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // FLOAT
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("float_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("float_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    // DOUBLE
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("double_value", SortOrder.ASC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * i, 0.000001d));
    }
    assertThat(searchResponse.toString(), not(containsString("error")));
    size = 1 + random.nextInt(10);
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("double_value", SortOrder.DESC).execute().actionGet();
    assertHitCount(searchResponse, 10L);
    assertThat(searchResponse.getHits().getHits().length, equalTo(size));
    for (int i = 0; i < size; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(Integer.toString(9 - i)));
        assertThat(((Number) searchResponse.getHits().getAt(i).getSortValues()[0]).doubleValue(), closeTo(0.1d * (9 - i), 0.000001d));
    }
    assertNoFailures(searchResponse);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Random(java.util.Random) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 13 with IndexRequestBuilder

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

the class SimpleQueryStringIT method testWithDate.

public void testWithDate() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f_date", "2015/09/02"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar", "f_date", "2015/09/01"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo bar")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("\"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1");
    assertHitCount(resp, 1L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar \"2015/09/02\"")).get();
    assertHits(resp.getHits(), "1", "2");
    assertHitCount(resp, 2L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("\"2015/09/02\" \"2015/09/01\"")).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) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 14 with IndexRequestBuilder

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

the class SimpleQueryStringIT method testKeywordWithWhitespace.

public void testKeywordWithWhitespace() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f2", "Foo Bar"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar"));
    reqs.add(client().prepareIndex("test", "doc", "3").setSource("f1", "foo bar"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo")).get();
    assertHits(resp.getHits(), "3");
    assertHitCount(resp, 1L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar")).get();
    assertHits(resp.getHits(), "2", "3");
    assertHitCount(resp, 2L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 15 with IndexRequestBuilder

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

the class SimpleQueryStringIT method testExplicitAllFieldsRequested.

public void testExplicitAllFieldsRequested() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index-with-all.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f1", "foo", "f2", "eggplant"));
    indexRandom(true, false, reqs);
    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND)).get();
    assertHitCount(resp, 0L);
    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo eggplent").defaultOperator(Operator.AND).useAllFields(true)).get();
    assertHits(resp.getHits(), "1");
    assertHitCount(resp, 1L);
    Exception e = expectThrows(Exception.class, () -> client().prepareSearch("test").setQuery(simpleQueryStringQuery("blah").field("f1").useAllFields(true)).get());
    assertThat(ExceptionsHelper.detailedMessage(e), containsString("cannot use [all_fields] parameter in conjunction with [fields]"));
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) 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