# How to set mapping before indexing records?

**URL:** <https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848>\
**Category:** Elasticsearch\
**Created:** [July 5, 2017, 7:01am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848 "2017-07-05T07:01:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chanpiseth\_Ros](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@Chanpiseth\_Ros](https://discuss.elastic.co/u/Chanpiseth_Ros)\
**Post date:** [July 5, 2017, 7:01am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/1 "2017-07-05T07:01:03Z")

</div>

I want to set my custom mapping before index, how can i use it?

My mapping:  
\> $client = \Elasticsearch\ClientBuilder::create()-\>build();

> ```
> $mappingFields = [
> 'index' => 'staff',
> 'body' => [
> 'mapping' => [
> 'person' => [
> 'properties' => [
> 'first-name' => [
> 'type' => 'string'
> ],
> ]
> ]
> ]
> ]
> ]
> $client->indices()->create($mappingFields);
> $params = [
> '_index' => 'staff',
> '_type' => 'person',
> '_id' => 1,
> 'body' => [
> 'first-name' => 'ABC',
> ]
> ];
> $client->index($params);
> 
> ```

And i always get this error message "error":"IndexAlreadyExistsException[[staff] already exists]","status":400}", Do i config or write something wrong? Could someone help me?

Thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 5, 2017, 7:04am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/2 "2017-07-05T07:04:05Z")

</div>

Does the index exist? Check with `_cat/indices`.

---

<div class="post-metadata">

**Author:** ![Chanpiseth\_Ros](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@Chanpiseth\_Ros](https://discuss.elastic.co/u/Chanpiseth_Ros)\
**Post date:** [July 5, 2017, 7:06am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/3 "2017-07-05T07:06:05Z")

</div>

Yes, it does.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 5, 2017, 7:50am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/4 "2017-07-05T07:50:16Z")

</div>

Then you need to delete it, you cannot update an existing mapping like that.

---

<div class="post-metadata">

**Author:** ![Chanpiseth\_Ros](https://avatars.discourse-cdn.com/v4/letter/c/c4cdca/32.png) [@Chanpiseth\_Ros](https://discuss.elastic.co/u/Chanpiseth_Ros)\
**Post date:** [July 5, 2017, 7:55am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/5 "2017-07-05T07:55:17Z")

</div>

Yes, i have delete it already, but it errors when i work with new index

---

<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:** [August 2, 2017, 7:55am UTC](https://discuss.elastic.co/t/how-to-set-mapping-before-indexing-records/91848/6 "2017-08-02T07:55:20Z")

</div>

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