> For the complete documentation index, see [llms.txt](https://learnsql.gitbook.io/lernmysql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnsql.gitbook.io/lernmysql/elasticsearch-and-php-and-mysql-thuc-hanh-phan-8-mapping.md).

# ElasticSearch & php & mysql thực hành – Phần 8 – Mapping

<https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html>

## ElasticSearch & php & mysql thực hành – Phần 8 – Mapping

[August 17, 2015](https://thinhpc86.wordpress.com/2015/08/17/elasticsearch-php-mysql-thuc-hanh-phan-8-mapping/) [thinhpc86](https://thinhpc86.wordpress.com/author/thinhpc86/) [Java](https://thinhpc86.wordpress.com/tag/java/), [PHP](https://thinhpc86.wordpress.com/category/php/)[ElasticSearch](https://thinhpc86.wordpress.com/tag/elasticsearch/), [PHP](https://thinhpc86.wordpress.com/tag/php/)

Bài trước ta đã đi qua một số [thiết lập server](https://thinhpc86.wordpress.com/2015/06/12/elasticsearch-php-mysql-thuc-hanh-phan-7/) để tối ưu hoạt động của ES trên server.

Trong quá trình sử dụng ES thì Mapping là 1 kiến thức cơ bản và quan trọng cần sử dụng. Khi ta tạo document thì mặc định tất cả các field của docs mặc định là kiểu String. Nhưng trong nhiều trường hợp ta có các field kiểu integer, date, boolean…

Đó là lý do ta cần dùng đến Mapping. Ta sẽ đi qua ứng dụng của mapping api.\
1\. **Tạo, update Mapping**:\
**PUT /{index}/\_mapping/{type}**

Ví dụ:

```
curl -X PUT "localhost:9200/my-index-000001/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false
    }
  }
}
'
```

**2. Get Mapping:**

| 1 | `curl -XGET` `'`[`http://localhost:9200/twitter/_mapping/tweet`](http://localhost:9200/twitter/_mapping/tweet)`'` |
| - | ----------------------------------------------------------------------------------------------------------------- |

**3. Delete Mapping:**

| 123 | `[DELETE] /{index}/{type}[DELETE] /{index}/{type}/_mapping[DELETE] /{index}/_mapping/{type}` |
| --- | -------------------------------------------------------------------------------------------- |

| 1 | `curl -XDELETE` `'`[`http://localhost:9200/twitter/_mapping/tweet`](http://localhost:9200/twitter/_mapping/tweet)`'` |
| - | -------------------------------------------------------------------------------------------------------------------- |

Mapping với kiểu date thì phải có format cụ thể. Dưới đây là ví dụ format 1 số type dữ liệu khác nhau:

| 12345678910111213141516171819202122 | `"mappings"` `: {    "typename"` `: {        "properties"` `: {            "title"` `: {                "type"` `:` `"string"            },            "content"` `: {                "type"` `:` `"string"            }            "genre"` `: {                "type"` `:` `"integer"            }            "startTime"` `: {                "type"` `:` `"date",                "format"` `:` `"yyyy-MM-dd HH:mm:ss"            }            "is_new"` `: {                "type"` `:` `"boolean"            }        }    }}` |
| ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
