Search in sources :

Example 51 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testGetFieldsMetaData.

public void testGetFieldsMetaData() throws Exception {
    assertAcked(prepareCreate("test").addMapping("parent").addMapping("my-type1", "_parent", "type=parent", "field1", "type=keyword,store=true").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
    client().prepareIndex("test", "my-type1", "1").setRouting("1").setParent("parent_1").setSource(jsonBuilder().startObject().field("field1", "value").endObject()).get();
    GetResponse getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1").setRouting("1").setStoredFields("field1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
    assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
    assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
    assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));
    assertThat(getResponse.getField("_parent").isMetadataField(), equalTo(true));
    assertThat(getResponse.getField("_parent").getValue().toString(), equalTo("parent_1"));
    flush();
    getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1").setStoredFields("field1").setRouting("1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
    assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
    assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
    assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));
    assertThat(getResponse.getField("_parent").isMetadataField(), equalTo(true));
    assertThat(getResponse.getField("_parent").getValue().toString(), equalTo("parent_1"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 52 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testGetFieldsComplexField.

public void testGetFieldsComplexField() throws Exception {
    assertAcked(prepareCreate("my-index").setSettings(Settings.builder().put("index.refresh_interval", -1)).addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties").startObject("field1").field("type", "object").startObject("properties").startObject("field2").field("type", "object").startObject("properties").startObject("field3").field("type", "object").startObject("properties").startObject("field4").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject()));
    BytesReference source = jsonBuilder().startObject().startArray("field1").startObject().startObject("field2").startArray("field3").startObject().field("field4", "value1").endObject().endArray().endObject().endObject().startObject().startObject("field2").startArray("field3").startObject().field("field4", "value2").endObject().endArray().endObject().endObject().endArray().endObject().bytes();
    logger.info("indexing documents");
    client().prepareIndex("my-index", "my-type1", "1").setSource(source, XContentType.JSON).get();
    client().prepareIndex("my-index", "my-type2", "1").setSource(source, XContentType.JSON).get();
    logger.info("checking real time retrieval");
    String field = "field1.field2.field3.field4";
    GetResponse getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    logger.info("waiting for recoveries to complete");
    // Flush fails if shard has ongoing recoveries, make sure the cluster is settled down
    ensureGreen();
    logger.info("flushing");
    FlushResponse flushResponse = client().admin().indices().prepareFlush("my-index").setForce(true).get();
    if (flushResponse.getSuccessfulShards() == 0) {
        StringBuilder sb = new StringBuilder("failed to flush at least one shard. total shards [").append(flushResponse.getTotalShards()).append("], failed shards: [").append(flushResponse.getFailedShards()).append("]");
        for (ShardOperationFailedException failure : flushResponse.getShardFailures()) {
            sb.append("\nShard failure: ").append(failure);
        }
        fail(sb.toString());
    }
    logger.info("checking post-flush retrieval");
    getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
    getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
    assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
    assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
    assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 53 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testGetDocWithMultivaluedFields.

public void testGetDocWithMultivaluedFields() throws Exception {
    String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().string();
    String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type2").startObject("properties").startObject("field").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().string();
    assertAcked(prepareCreate("test").addMapping("type1", mapping1, XContentType.JSON).addMapping("type2", mapping2, XContentType.JSON).setSettings(Settings.builder().put("index.refresh_interval", -1)));
    ensureGreen();
    GetResponse response = client().prepareGet("test", "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
    response = client().prepareGet("test", "type2", "1").get();
    assertThat(response.isExists(), equalTo(false));
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
    client().prepareIndex("test", "type2", "1").setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
    response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getType(), equalTo("type1"));
    Set<String> fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(singleton("field")));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
    assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
    assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
    response = client().prepareGet("test", "type2", "1").setStoredFields("field").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getType(), equalTo("type2"));
    assertThat(response.getId(), equalTo("1"));
    fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(singleton("field")));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
    assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
    assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
    // Now test values being fetched from stored fields.
    refresh();
    response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(singleton("field")));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
    assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
    assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
    response = client().prepareGet("test", "type2", "1").setStoredFields("field").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(singleton("field")));
    assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
    assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
    assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) HashSet(java.util.HashSet)

Example 54 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class RecoveryWhileUnderLoadIT method iterateAssertCount.

private void iterateAssertCount(final int numberOfShards, final int iterations, final Set<String> ids) throws Exception {
    final long numberOfDocs = ids.size();
    SearchResponse[] iterationResults = new SearchResponse[iterations];
    boolean error = false;
    for (int i = 0; i < iterations; i++) {
        SearchResponse searchResponse = client().prepareSearch().setSize((int) numberOfDocs).setQuery(matchAllQuery()).addSort("id", SortOrder.ASC).get();
        logSearchResponse(numberOfShards, numberOfDocs, i, searchResponse);
        iterationResults[i] = searchResponse;
        if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
            error = true;
        }
    }
    if (error) {
        //Printing out shards and their doc count
        IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats().get();
        for (ShardStats shardStats : indicesStatsResponse.getShards()) {
            DocsStats docsStats = shardStats.getStats().docs;
            logger.info("shard [{}] - count {}, primary {}", shardStats.getShardRouting().id(), docsStats.getCount(), shardStats.getShardRouting().primary());
        }
        ClusterService clusterService = clusterService();
        final ClusterState state = clusterService.state();
        for (int shard = 0; shard < numberOfShards; shard++) {
            for (String id : ids) {
                ShardId docShard = clusterService.operationRouting().shardId(state, "test", id, null);
                if (docShard.id() == shard) {
                    for (ShardRouting shardRouting : state.routingTable().shardRoutingTable("test", shard)) {
                        GetResponse response = client().prepareGet("test", "type", id).setPreference("_only_nodes:" + shardRouting.currentNodeId()).get();
                        if (response.isExists()) {
                            logger.info("missing id [{}] on shard {}", id, shardRouting);
                        }
                    }
                }
            }
        }
        //if there was an error we try to wait and see if at some point it'll get fixed
        logger.info("--> trying to wait");
        assertTrue(awaitBusy(() -> {
            boolean errorOccurred = false;
            for (int i = 0; i < iterations; i++) {
                SearchResponse searchResponse = client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get();
                if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
                    errorOccurred = true;
                }
            }
            return !errorOccurred;
        }, 5, TimeUnit.MINUTES));
        assertEquals(numberOfDocs, ids.size());
    }
    //lets now make the test fail if it was supposed to fail
    for (int i = 0; i < iterations; i++) {
        assertHitCount(iterationResults[i], numberOfDocs);
    }
}
Also used : ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.elasticsearch.cluster.ClusterState) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) DocsStats(org.elasticsearch.index.shard.DocsStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 55 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class SimpleRecoveryIT method testSimpleRecovery.

public void testSimpleRecovery() throws Exception {
    assertAcked(prepareCreate("test", 1).execute().actionGet());
    NumShards numShards = getNumShards("test");
    client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
    FlushResponse flushResponse = client().admin().indices().flush(flushRequest("test")).actionGet();
    assertThat(flushResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(flushResponse.getFailedShards(), equalTo(0));
    client().index(indexRequest("test").type("type1").id("2").source(source("2", "test"), XContentType.JSON)).actionGet();
    RefreshResponse refreshResponse = client().admin().indices().refresh(refreshRequest("test")).actionGet();
    assertThat(refreshResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(refreshResponse.getFailedShards(), equalTo(0));
    allowNodes("test", 2);
    logger.info("Running Cluster Health");
    ensureGreen();
    GetResponse getResult;
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
    // now start another one so we move some primaries
    allowNodes("test", 3);
    Thread.sleep(200);
    logger.info("Running Cluster Health");
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
        getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
        assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
}
Also used : FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)84 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)22 Test (org.junit.Test)18 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 HashMap (java.util.HashMap)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 Script (org.elasticsearch.script.Script)9 Settings (org.elasticsearch.common.settings.Settings)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 CompiledScript (org.elasticsearch.script.CompiledScript)7 ExecutableScript (org.elasticsearch.script.ExecutableScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)5 Path (java.nio.file.Path)4