在Elasticsearch 2.x中,我如何区分首字母缩写"CAN“和常见的英语单词"can”,同时仍然在我的分析器中使用“小写”筛选器(用来使搜索不区分大小写)?
我使用的自定义分析器是:
"analyzer": {
"tight": {
"type": "custom",
"tokenizer": "standard",
"stopwords": "_english_",
"filter": ["lowercase", "asciifolding"]
}
}在索引时,当大写首字母缩写"can“命中我的分析器时,它就变成了英语单词"CAN”。然后,当我搜索"CAN“时,我会得到所有包含英文单词"can”的文档。我只想要包含大写单词"CAN“的文档。可能还有其他缩写词属于类似的模式。
解决这个问题的最好方法是什么?
发布于 2016-08-15 13:52:12
实现它的一种方法是创建另一个不带lowercase令牌过滤器的分析器,并在主字段的子字段上使用该分析器。它是这样的:
使用两个分析器tight和tight_acronym创建索引。前者被分配给field,后者被分配给field.acronyms子字段:
PUT index
{
"settings": {
"analysis": {
"analyzer": {
"tight": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
},
"tight_acronym": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"asciifolding"
]
}
}
}
},
"mappings": {
"test": {
"properties": {
"field": {
"type": "string",
"analyzer": "tight",
"fields": {
"acronyms": {
"type": "string",
"analyzer": "tight_acronym"
}
}
}
}
}
}
}然后我们索引两个文档:
PUT index/test/1
{ "field": "It is worth CAN 300" }
PUT index/test/2
{ "field": "can you do it?" }然后,如果您搜索CAN (在子字段上),您将得到第一个文档
POST index/test/_search
{
"query": {
"match": {
"field.acronyms": "CAN"
}
}
}如果您搜索can (在主字段上),您将得到第二个文档
POST index/test/_search
{
"query": {
"match": {
"field": "can"
}
}
}https://stackoverflow.com/questions/38778499
复制相似问题