我正在尝试使用elasticsearch 6.3.0构建一个搜索实用程序,可以在数据库中搜索任何术语。我已应用“停止分析器”来排除一些通用单词。然而,在该分析系统也不再给我提供数字术语之后。
就像如果我搜索 news24 那么它会删除 24 并仅在所有记录中搜索“新闻”术语。不确定为什么。
下面是我正在使用的查询
{
"from": 0,
"size": 10,
"explain": false,
"stored_fields": [
"_source"
],
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "news24",
"analyzer": "stop",
"fields": [
"title",
"keywords",
"url"
]
}
},
"functions": [
{
"script_score": {
"script": "( (doc['isSponsered'].value == 'y') ? 100 : 0 )"
}
},
{
"script_score": {
"script": "doc['linksCount'].value"
}
}
],
"score_mode": "sum",
"boost_mode": "sum"
}
},
"script_fields": {
"custom_score": {
"script": {
"lang": "painless",
"source": "params._source.linksArray"
}
}
},
"highlight": {
"pre_tags": [
""
],
"post_tags": [
"<\/span>"
],
"fields": {
"title": {
"type": "plain"
},
"keywords": {
"type": "plain"
},
"description": {
"type": "plain"
},
"url": {
"type": "plain"
}
}
}
}
最佳答案
这是因为停止分析器
只是 Simple Analyzer 的扩展。它利用 Lowercase Tokenizer如果遇到不是字母的字符,它会简单地将术语分解为标记(当然也会将所有术语小写)。
所以基本上,如果你有像 news24
这样的东西,它会在遇到 2
时将其分解为 news
。
这是停止分析器
的默认行为。如果您打算使用停用词并且仍然希望将数字保留在图片中,那么您将需要创建一个自定义分析器,如下所示:
映射:
POST sometestindex
{
"settings":{
"analysis":{
"analyzer":{
"my_english_analyzer":{
"type":"standard",
"stopwords":"_english_"
}
}
}
}
}
它的作用是利用内部使用 Standard Tokenizer 的标准分析器
并且还忽略停用词。
分析查询以测试
POST sometestindex/_analyze
{
"analyzer": "my_english_analyzer",
"text": "the name of the channel is news24"
}
查询结果
{
"tokens": [
{
"token": "name",
"start_offset": 4,
"end_offset": 8,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "channel",
"start_offset": 16,
"end_offset": 23,
"type": "<ALPHANUM>",
"position": 4
},
{
"token": "news24",
"start_offset": 27,
"end_offset": 33,
"type": "<ALPHANUM>",
"position": 6
}
]
}
您可以在上面的 token 中看到,news24
被保留为 token 。
希望对你有帮助!
关于Elasticsearch - 停止分析器不允许数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53129877/