elsearch autocomplete

Elsearch自动补全

1. 创建索引、实例化token过滤器和分析器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -XPUT localhost:9200/autosuggest -d '
{
"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. 测试分析器是否正确

1
2
3
4
5
6
curl -XGET localhost:9200/autosuggest/_analyze -d '                             
{
"analyzer": "autocomplete",
"text": "quick brown"
}
'

3. 使用update-mappingAPI将这个分析器应用到具体字段

1
2
3
4
5
6
7
8
9
10
11
12
curl -XPUT localhost:9200/autosuggest/_mapping/my_type -d '                             
{
"my_type": {
"properties": {
"name": {
"type": "string",
"analyzer": "autocomplete"
}
}
}
}
'

4. 创建测试文档

1
2
3
4
5
6
curl -XPOST localhost:9200/autosuggest/my_type/_bulk -d '                             
{ "index": { "_id": 1 }}
{ "name": "Brown foxes" }
{ "index": { "_id": 2 }}
{ "name": "Yellow furballs" }
'

5. 如果使用简单的match查询测试brown fo

1
2
3
4
5
6
7
8
9
curl -XGET localhost:9200/autosuggest/my_type/_search -d '                             
{
"query": {
"match": {
"name": "brown fo"
}
}
}
'
  • 可以看到两个文档都能匹配,尽管 Yellow furballs 这个文档并不包含 brownfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"took": 42,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.7102298,
"hits": [
{
"_index": "autosuggest",
"_type": "my_type",
"_id": "1",
"_score": 1.7102298,
"_source": {
"name": "Brown foxes"
}
},
{
"_index": "autosuggest",
"_type": "my_type",
"_id": "2",
"_score": 0.2688388,
"_source": {
"name": "Yellow furballs"
}
}
]
}
}

6. 分析

1
2
3
4
5
6
7
8
9
curl -XGET localhost:9200/autosuggest/my_type/_validate/query?explain -d ' 
{
"query": {
"match": {
"name": "brown fo"
}
}
}
'
  • name:f条件可以满足第二个文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

{
"valid": true,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"explanations": [
{
"index": "autosuggest",
"valid": true,
"explanation": "+(Synonym(name:b name:br name:bro name:brow name:brown) Synonym(name:f name:fo)) #_type:my_type"
}
]
}

7. 匹配用户输入的完整词组

  • 可以通过在索引时使用 autocomplete 分析器,并在搜索时使用 standard 标准分析器来实现这种想法,只要改变查询使用的搜索分析器 analyzer 参数即可:
1
2
3
4
5
6
7
8
9
10
11
12
curl -XGET localhost:9200/autosuggest/my_type/_search -d '
{
"query": {
"match": {
"name": {
"query": "brown fo",
"analyzer": "standard"
}
}
}
}
'

8. 改变`search_analyzer `这里只要更新现有的映射不用对数据重新创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
curl -XPUT localhost:9200/autosuggest/my_type/_mapping -d '
{
"my_type": {
"properties": {
"name": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
'

9. 再次请求validate-queryAPI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -XGET localhost:9200/autosuggest/my_type/_validate/query?explain -d ' 
{
"query": {
"match": {
"name": "brown fo"
}
}
}
'

Response:
{
"valid": true,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"explanations": [
{
"index": "autosuggest",
"valid": true,
"explanation": "+(name:brown name:fo) #_type:my_type"
}
]
}