1.配置分词策略
PUT /my_index HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 647
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
2.验证分词器
GET /my_index/_analyze HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 62
{
"analyzer":"autocomplete",
"text":"quick brown"
}
3设置索引字段对分词器生效 ,类似mysql 声明表结构
PUT /my_index/_mapping/my_type?include_type_name=true HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 184
{
"my_type": {
"properties": {
"name": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
}
4.批量插入验证数据
PUT /my_index/my_type/_bulk HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 135
{ "index": { "_id": 1 }}
{ "name": "Brown foxes" }
{ "index": { "_id": 2 }}
{ "name": "Yellow furballs" }
5.执行查询
POST /my_index/my_type/_search HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 90
{
"query": {
"match": {
"name": "brown fo"
}
}
}
6.优化,使用standard 分析器效果更好
POST /my_index/my_type/_search HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 175
{
"query": {
"match": {
"name": {
"query": "brown fox",
"analyzer": "standard"
}
}
}
}
7.使用explain 分析
POST /my_index/my_type/_validate/query?explain=null HTTP/1.1
Host: localhost:9200
Content-Type: application/json
Content-Length: 90
{
"query": {
"match": {
"name": "brown fo"
}
}
}