# Create Index using RestHighLevelClient

**URL:** <https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 2, 2020, 3:46am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302 "2020-12-02T03:46:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete\_Watcharawit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pete_watcharawit/32/74140_2.png) [@Pete\_Watcharawit](https://discuss.elastic.co/u/Pete_Watcharawit)\
**Post date:** [December 2, 2020, 3:46am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302/1 "2020-12-02T03:46:21Z")

</div>

```auto
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

```auto
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?

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 2, 2020, 6:22am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302/2 "2020-12-02T06:22:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Pete\_Watcharawit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pete_watcharawit/32/74140_2.png) [@Pete\_Watcharawit](https://discuss.elastic.co/u/Pete_Watcharawit)\
**Post date:** [December 2, 2020, 7:13am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302/3 "2020-12-02T07:13:14Z")

</div>

```auto
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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 2, 2020, 7:42am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302/4 "2020-12-02T07:42:34Z")

</div>

What does your document look like?

Some examples here:

> **[dadoonet/elasticsearch-java-client-demo](https://github.com/dadoonet/elasticsearch-java-client-demo)**
>
> Elasticsearch Client Java Sample project. Contribute to dadoonet/elasticsearch-java-client-demo development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 30, 2020, 7:43am UTC](https://discuss.elastic.co/t/create-index-using-resthighlevelclient/257302/5 "2020-12-30T07:43:06Z")

</div>

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