Search in sources :

Example 11 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class SimpleGetMappingsIT method testGetMappingsWithBlocks.

public void testGetMappingsWithBlocks() throws IOException {
    client().admin().indices().prepareCreate("test").addMapping("typeA", getMappingForType("typeA")).addMapping("typeB", getMappingForType("typeB")).execute().actionGet();
    ensureGreen();
    for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY)) {
        try {
            enableIndexBlock("test", block);
            GetMappingsResponse response = client().admin().indices().prepareGetMappings().execute().actionGet();
            assertThat(response.mappings().size(), equalTo(1));
            assertThat(response.mappings().get("test").size(), equalTo(2));
        } finally {
            disableIndexBlock("test", block);
        }
    }
    try {
        enableIndexBlock("test", SETTING_BLOCKS_METADATA);
        assertBlocked(client().admin().indices().prepareGetMappings(), INDEX_METADATA_BLOCK);
    } finally {
        disableIndexBlock("test", SETTING_BLOCKS_METADATA);
    }
}
Also used : GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 12 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project graylog2-server by Graylog2.

the class IndicesTest method testIndexTemplateCanBeOverridden.

@Test
public void testIndexTemplateCanBeOverridden() throws Exception {
    final String customTemplateName = "custom-template";
    final IndicesAdminClient client = this.client.admin().indices();
    // Create custom index template
    final Map<String, Object> customMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
    final PutIndexTemplateResponse putIndexTemplateResponse = client.preparePutTemplate(customTemplateName).setTemplate(indexSet.getIndexWildcard()).setOrder(1).addMapping(IndexMapping.TYPE_MESSAGE, customMapping).get();
    assertThat(putIndexTemplateResponse.isAcknowledged()).isTrue();
    // Validate existing index templates
    final GetIndexTemplatesResponse getTemplatesResponse = client.prepareGetTemplates().get();
    final List<IndexTemplateMetaData> indexTemplates = getTemplatesResponse.getIndexTemplates();
    assertThat(indexTemplates).extracting(IndexTemplateMetaData::getName).containsExactly(customTemplateName);
    // Create index with custom template
    final String testIndexName = "graylog_override_template";
    indices.create(testIndexName, indexSet);
    // Check index mapping
    final GetMappingsResponse indexMappingResponse = client.prepareGetMappings(testIndexName).get();
    final String mapping = indexMappingResponse.getMappings().get(testIndexName).get(IndexMapping.TYPE_MESSAGE).source().string();
    final ReadContext ctx = JsonPath.parse(mapping);
    final boolean sourceEnabled = ctx.read("$.message._source.enabled");
    assertThat(sourceEnabled).isFalse();
    final String messageField = ctx.read("$.message.properties.message.index");
    assertThat(messageField).isEqualTo("not_analyzed");
    // Clean up
    final DeleteIndexTemplateResponse deleteResponse = client.prepareDeleteTemplate(customTemplateName).get();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete(testIndexName);
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) ReadContext(com.jayway.jsonpath.ReadContext) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) Test(org.junit.Test)

Example 13 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project pyramid by cheng-li.

the class ESIndex method listAllFields.

public Set<String> listAllFields() throws Exception {
    GetMappingsResponse response = client.admin().indices().prepareGetMappings(this.indexName).execute().actionGet();
    MappingMetaData mappingMetaData = response.getMappings().get(this.indexName).get(this.documentType);
    Map map = (Map) mappingMetaData.getSourceAsMap().get("properties");
    Set<String> fields = new HashSet<>();
    for (Object field : map.keySet()) {
        fields.add(field.toString());
    }
    return fields;
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 14 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class RestGetMappingAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices).types(types);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
            ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
            if (mappingsByIndex.isEmpty()) {
                if (indices.length != 0 && types.length != 0) {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                } else if (indices.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new IndexNotFoundException(indices[0]));
                } else if (types.length != 0) {
                    builder.close();
                    return new BytesRestResponse(channel, new TypeMissingException("_all", types[0]));
                } else {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                }
            }
            builder.startObject();
            for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
                if (indexEntry.value.isEmpty()) {
                    continue;
                }
                builder.startObject(indexEntry.key);
                builder.startObject(Fields.MAPPINGS);
                for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
                    builder.field(typeEntry.key);
                    builder.map(typeEntry.value.sourceAsMap());
                }
                builder.endObject();
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) RestResponse(org.elasticsearch.rest.RestResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) TypeMissingException(org.elasticsearch.indices.TypeMissingException) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) TypeMissingException(org.elasticsearch.indices.TypeMissingException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) IOException(java.io.IOException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TypeMissingException(org.elasticsearch.indices.TypeMissingException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 15 with GetMappingsResponse

use of org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse in project elasticsearch by elastic.

the class MetaDataWriteDataNodesIT method testMetaWrittenWhenIndexIsClosedAndMetaUpdated.

public void testMetaWrittenWhenIndexIsClosedAndMetaUpdated() throws Exception {
    String masterNode = internalCluster().startMasterOnlyNode(Settings.EMPTY);
    final String dataNode = internalCluster().startDataOnlyNode(Settings.EMPTY);
    final String index = "index";
    assertAcked(prepareCreate(index).setSettings(Settings.builder().put("index.number_of_replicas", 0)));
    logger.info("--> wait for green index");
    ensureGreen();
    logger.info("--> wait for meta state written for index");
    assertIndexInMetaState(dataNode, index);
    assertIndexInMetaState(masterNode, index);
    logger.info("--> close index");
    client().admin().indices().prepareClose(index).get();
    // close the index
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
    assertThat(clusterStateResponse.getState().getMetaData().index(index).getState().name(), equalTo(IndexMetaData.State.CLOSE.name()));
    // update the mapping. this should cause the new meta data to be written although index is closed
    client().admin().indices().preparePutMapping(index).setType("doc").setSource(jsonBuilder().startObject().startObject("properties").startObject("integer_field").field("type", "integer").endObject().endObject().endObject()).get();
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("doc").get();
    assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get(index).get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
    // make sure it was also written on red node although index is closed
    ImmutableOpenMap<String, IndexMetaData> indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertNotNull(((LinkedHashMap) (indicesMetaData.get(index).getMappings().get("doc").getSourceAsMap().get("properties"))).get("integer_field"));
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.CLOSE));
    /* Try the same and see if this also works if node was just restarted.
         * Each node holds an array of indices it knows of and checks if it should
         * write new meta data by looking up in this array. We need it because if an
         * index is closed it will not appear in the shard routing and we therefore
         * need to keep track of what we wrote before. However, when the node is
         * restarted this array is empty and we have to fill it before we decide
         * what we write. This is why we explicitly test for it.
         */
    internalCluster().restartNode(dataNode, new RestartCallback());
    client().admin().indices().preparePutMapping(index).setType("doc").setSource(jsonBuilder().startObject().startObject("properties").startObject("float_field").field("type", "float").endObject().endObject().endObject()).get();
    getMappingsResponse = client().admin().indices().prepareGetMappings(index).addTypes("doc").get();
    assertNotNull(((LinkedHashMap) (getMappingsResponse.getMappings().get(index).get("doc").getSourceAsMap().get("properties"))).get("float_field"));
    // make sure it was also written on red node although index is closed
    indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertNotNull(((LinkedHashMap) (indicesMetaData.get(index).getMappings().get("doc").getSourceAsMap().get("properties"))).get("float_field"));
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.CLOSE));
    // finally check that meta data is also written of index opened again
    assertAcked(client().admin().indices().prepareOpen(index).get());
    // make sure index is fully initialized and nothing is changed anymore
    ensureGreen();
    indicesMetaData = getIndicesMetaDataOnNode(dataNode);
    assertThat(indicesMetaData.get(index).getState(), equalTo(IndexMetaData.State.OPEN));
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)26 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)11 Map (java.util.Map)8 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 IOException (java.io.IOException)4 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Client (org.elasticsearch.client.Client)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 TypeMissingException (org.elasticsearch.indices.TypeMissingException)2 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 ReadContext (com.jayway.jsonpath.ReadContext)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1