---
# System prepended metadata

title: Elasticsearch 測試分詞器
tags: [Elasticsearch]

---

---
title: Elasticsearch 測試分詞器
tags: Elasticsearch
description: View the slide with "Slide Mode".
---

# \[Elasticsearch\] 測試分詞器

<!-- Put the link to this slide here so people can follow -->
### 1. 不建index，直接測試

```shell=
curl -u elastic -X POST localhost:9200/_analyze?pretty -H 'Content-Type: application/json' -d'
{
  "tokenizer": "articut_index",
  "text":      "測試中"
}'
```

- `tokenizer`可以換成 `analyzer`
- `articut_index` 可以換成 `standard` 或是`jieba_index`

### 2. 建立index，並設定分詞器

```shell=
# step1:建立index
curl -u elastic -X PUT "localhost:9200/articut_test_index?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "articut_ana": {
          "type": "custom", 
          "tokenizer": "articut_index"
        }
      }
    }
  }
}
'
# step2:查看分詞器設定是否成功
curl -u elastic -X GET localhost:9200/articut_test_index/_settings?pretty

# step3:建立文件document
curl -u elastic -X POST localhost:9200/INDEX/_doc?pretty -H 'Content-Type: application/json' -d'{
    "message" : "大成功了嗎"
    }'
    
# step4:查看分詞表
curl -u elastic -X GET localhost:9200/index_name/_termvectors/document_id?pretty
```
- [查看單筆document的tokens](elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html)

### 3. 測試成功後，應該直接設定於template上，讓ES自動去設定分詞器
- [Elasticsearch index set analyzer](https://hackmd.io/Ymc60v0BSPCTijyjBJRx7Q)
