Search in sources :

Example 1 with Alias

use of org.elasticsearch.action.admin.indices.alias.Alias in project crate by crate.

the class TransportExecutorDDLTest method testCreateTableWithOrphanedAlias.

@Test
public void testCreateTableWithOrphanedAlias() throws Exception {
    String partitionName = new PartitionName("test", Collections.singletonList(new BytesRef("foo"))).asIndexName();
    client().admin().indices().prepareCreate(partitionName).addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS).addAlias(new Alias("test")).execute().actionGet();
    ensureGreen();
    execute("create table test (id integer, name string, names string) " + "clustered into 2 shards " + "partitioned by (id) with (number_of_replicas=0)");
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    execute("select * from information_schema.tables where table_name = 'test'");
    assertThat(response.rowCount(), is(1L));
    execute("select count(*) from information_schema.columns where table_name = 'test'");
    assertThat((Long) response.rows()[0][0], is(3L));
    // check that orphaned alias has been deleted
    assertThat(client().admin().cluster().prepareState().execute().actionGet().getState().metaData().hasAlias("test"), is(false));
    // check that orphaned partition has been deleted
    assertThat(client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Alias(org.elasticsearch.action.admin.indices.alias.Alias) BytesRef(org.apache.lucene.util.BytesRef) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 2 with Alias

use of org.elasticsearch.action.admin.indices.alias.Alias in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingCrudApis.

public void testRequiredRoutingCrudApis() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with no routing, should fail");
    logger.info("--> indexing with id [1], with no routing, should fail");
    try {
        client().prepareIndex(indexOrAlias(), "type1", "1").setSource("field", "value1").get();
        fail("index with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    logger.info("--> verifying get with routing, should find");
    for (int i = 0; i < 5; i++) {
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    logger.info("--> deleting with no routing, should fail");
    try {
        client().prepareDelete(indexOrAlias(), "type1", "1").get();
        fail("delete with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail("get with missing routing when routing is required should fail");
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").execute().actionGet();
        fail("update with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").get();
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        GetResponse getResponse = client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(true));
        assertThat(getResponse.getSourceAsMap().get("field"), equalTo("value2"));
    }
    client().prepareDelete(indexOrAlias(), "type1", "1").setRouting("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(false));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 3 with Alias

use of org.elasticsearch.action.admin.indices.alias.Alias in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingBulk.

public void testRequiredRoutingBulk() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.indexRequest(indexOrAlias()).type("type1").id("1").source(Requests.INDEX_CONTENT_TYPE, "field", "value")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.indexRequest(indexOrAlias()).type("type1").id("1").routing("0").source(Requests.INDEX_CONTENT_TYPE, "field", "value")).execute().actionGet();
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(new UpdateRequest(indexOrAlias(), "type1", "1").doc(Requests.INDEX_CONTENT_TYPE, "field", "value2")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(new UpdateRequest(indexOrAlias(), "type1", "1").doc(Requests.INDEX_CONTENT_TYPE, "field", "value2").routing("0")).execute().actionGet();
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.deleteRequest(indexOrAlias()).type("type1").id("1")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(true));
        for (BulkItemResponse bulkItemResponse : bulkResponse) {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            assertThat(bulkItemResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
            assertThat(bulkItemResponse.getFailure().getStatus(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(bulkItemResponse.getFailure().getCause(), instanceOf(RoutingMissingException.class));
            assertThat(bulkItemResponse.getFailureMessage(), containsString("routing is required for [test]/[type1]/[1]"));
        }
    }
    {
        BulkResponse bulkResponse = client().prepareBulk().add(Requests.deleteRequest(indexOrAlias()).type("type1").id("1").routing("0")).execute().actionGet();
        assertThat(bulkResponse.getItems().length, equalTo(1));
        assertThat(bulkResponse.hasFailures(), equalTo(false));
    }
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Alias(org.elasticsearch.action.admin.indices.alias.Alias) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 4 with Alias

use of org.elasticsearch.action.admin.indices.alias.Alias in project elasticsearch by elastic.

the class UpdateIT method testContextVariables.

public void testContextVariables() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("subtype1", XContentFactory.jsonBuilder().startObject().startObject("subtype1").startObject("_parent").field("type", "type1").endObject().endObject().endObject()));
    ensureGreen();
    // Index some documents
    client().prepareIndex().setIndex("test").setType("type1").setId("parentId1").setSource("field1", 0, "content", "bar").execute().actionGet();
    client().prepareIndex().setIndex("test").setType("subtype1").setId("id1").setParent("parentId1").setRouting("routing1").setSource("field1", 1, "content", "foo").execute().actionGet();
    // Update the first object and note context variables values
    UpdateResponse updateResponse = client().prepareUpdate("test", "subtype1", "id1").setRouting("routing1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    GetResponse getResponse = client().prepareGet("test", "subtype1", "id1").setRouting("routing1").execute().actionGet();
    Map<String, Object> updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("subtype1", updateContext.get("_type"));
    assertEquals("id1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertEquals("parentId1", updateContext.get("_parent"));
    assertEquals("routing1", updateContext.get("_routing"));
    // Idem with the second object
    updateResponse = client().prepareUpdate("test", "type1", "parentId1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    getResponse = client().prepareGet("test", "type1", "parentId1").execute().actionGet();
    updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("type1", updateContext.get("_type"));
    assertEquals("parentId1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertNull(updateContext.get("_parent"));
    assertNull(updateContext.get("_routing"));
    assertNull(updateContext.get("_ttl"));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Alias

use of org.elasticsearch.action.admin.indices.alias.Alias in project elasticsearch by elastic.

the class ExplainActionIT method testExplainWithFilteredAlias.

public void testExplainWithFilteredAlias() throws Exception {
    assertAcked(prepareCreate("test").addMapping("test", "field2", "type=text").addAlias(new Alias("alias1").filter(QueryBuilders.termQuery("field2", "value2"))));
    ensureGreen("test");
    client().prepareIndex("test", "test", "1").setSource("field1", "value1", "field2", "value1").get();
    refresh();
    ExplainResponse response = client().prepareExplain("alias1", "test", "1").setQuery(QueryBuilders.matchAllQuery()).get();
    assertNotNull(response);
    assertTrue(response.isExists());
    assertFalse(response.isMatch());
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse)

Aggregations

Alias (org.elasticsearch.action.admin.indices.alias.Alias)80 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)11 AnalyzeResponse (org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse)10 ClusterState (org.elasticsearch.cluster.ClusterState)8 Map (java.util.Map)7 GetResponse (org.elasticsearch.action.get.GetResponse)7 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 ExplainResponse (org.elasticsearch.action.explain.ExplainResponse)6 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)6 Settings (org.elasticsearch.common.settings.Settings)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)4 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ArrayList (java.util.ArrayList)3 PutIndexTemplateRequestBuilder (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)3 DateTime (org.joda.time.DateTime)3