Create Index using RestHighLevelClient

IndexRequest indexRequest = new IndexRequest("my_index");
        indexRequest.create(true);
        indexRequest.source("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 1,\n" +
                "        \"number_of_replicas\" : 0\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"properties\" : {\n" +
                "            \"message\" : { \"type\" : \"text\" }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", XContentType.JSON);
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

this code snippet returns

ElasticsearchStatusException[Elasticsearch exception [type=invalid_type_name_exception, reason=mapping type name [_create] can't start with '_' unless it is called [_doc]]
]

error.

The ES 7.8 I am using still creates the index for me but it lacks the mapping part. I wonder if there is a way to fix this problem. Or is it better to use the following code instead?

CreateIndexRequest createIndexRequest = new CreateIndexRequest("twitter");
        createIndexRequest.source("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 1,\n" +
                "        \"number_of_replicas\" : 0\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"properties\" : {\n" +
                "            \"message\" : { \"type\" : \"text\" }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", XContentType.JSON);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

The first piece of code is meant to create a document. The second piece of code is meant to create an index.

1 Like
IndexRequest indexRequest = new IndexRequest("twitter");
        indexRequest.create(true);
        indexRequest.source("{\n" +
                "    \"settings\" : {\n" +
                "        \"number_of_shards\" : 1,\n" +
                "        \"number_of_replicas\" : 0\n" +
                "    },\n" +
                "    \"mappings\" : {\n" +
                "        \"properties\" : {\n" +
                "            \"message\" : { \"type\" : \"text\" }\n" +
                "        }\n" +
                "    },\n" +
                "    \"aliases\" : {\n" +
                "        \"twitter_alias\" : {}\n" +
                "    }\n" +
                "}", XContentType.JSON);
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

Using this code after creating the index, returns error.

ElasticsearchStatusException[Elasticsearch exception [type=invalid_type_name_exception, reason=mapping type name [_create] can't start with '_' unless it is called [_doc]]
]

I just want to create a document that has index "twitter" which is already created. What needs to be changed?

What does your document look like?

Some examples here:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.