我从elasticsearch 2.0移到5.2,ngram搜索现在坏了!
elasticsearch设置就在下面,它只是用于标题和摘要字段的简单ngram标记器。
settings = {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"ngram_filter": {
"type": "nGram",
"min_gram": 3,
"max_gram": 10
}
},
"analyzer": {
"search_ngram_analyzer": {
"tokenizer": "standard",
"type": "custom",
"filter": ["standard", "lowercase", "stop", "asciifolding"]
},
"index_ngram_analyzer": {
"tokenizer": "standard",
"type": "custom",
"filter": ["standard", "lowercase", "stop", "asciifolding", "ngram_filter"]
}
}
},
},
"mappings": {
"docs": {
"properties": {
'title': {
'boost': 100.0,
'search_analyzer': 'search_ngram_analyzer',
'analyzer': 'index_ngram_analyzer',
'type': 'text',
},
'summary': {
'boost': 20.0,
'search_analyzer': 'search_ngram_analyzer',
'analyzer': 'index_ngram_analyzer',
'type': 'text',
}
}
}
}
}
http://localhost:9200/my_index/_search?q=example
返回其中包含单词“example”的文档。作为常规查询。然而,
http://localhost:9200/my_index/_search?q=exampl
(例如,带有“e”)返回一个空对象!我在安装程序中找不到错误。这是API中断吗?
最佳答案
您确定这在以前的版本中有效吗?
如果您使用URI搜索并且未指定字段(就像在http://localhost:9200/my_index/_search?q=exampl
中所做的那样),那么将使用_all
字段。它使用标准分析器,因此没有ngram。您要使用的查询是/my_index/_search?q=title:exampl
为了重现性,这是Console整个示例的转储:
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "nGram",
"min_gram": 3,
"max_gram": 10
}
},
"analyzer": {
"search_ngram_analyzer": {
"tokenizer": "standard",
"type": "custom",
"filter": [
"standard",
"lowercase",
"stop",
"asciifolding"
]
},
"index_ngram_analyzer": {
"tokenizer": "standard",
"type": "custom",
"filter": [
"standard",
"lowercase",
"stop",
"asciifolding",
"ngram_filter"
]
}
}
}
},
"mappings": {
"docs": {
"properties": {
"title": {
"boost": 100,
"search_analyzer": "search_ngram_analyzer",
"analyzer": "index_ngram_analyzer",
"type": "text"
},
"summary": {
"boost": 20,
"search_analyzer": "search_ngram_analyzer",
"analyzer": "index_ngram_analyzer",
"type": "text"
}
}
}
}
}
GET /my_index/_analyze
{
"analyzer": "index_ngram_analyzer",
"text": "example exampl"
}
GET /my_index/_analyze
{
"analyzer": "search_ngram_analyzer",
"text": "example exampl"
}
POST /my_index/docs
{
"title": "This is an example",
"summary": "Some more text"
}
GET /my_index/_search?q=example
GET /my_index/_search?q=exampl
GET /my_index/_search?q=title:exampl
DELETE /my_index