ElasticSearch - 使用连字符搜索 [英] ElasticSearch - Searching with hyphens
问题描述
弹性搜索 1.6
我想索引包含连字符的文本,例如 U-12、U-17、WU-12、t 恤……并且能够使用简单查询字符串"查询来搜索它们.
I want to index text that contains hyphens, for example U-12, U-17, WU-12, t-shirt... and to be able to use a "Simple Query String" query to search on them.
数据样本(简化):
{"title":"U-12 Soccer",
"comment": "the t-shirts are dirty"}
由于已经有很多关于连字符的问题,我已经尝试了以下解决方案:
As there are quite a lot of questions already about hyphens, I tried the following solution already:
使用字符过滤器:ElasticSearch - 在名称中使用连字符搜索.
所以我选择了这个映射:
So I went for this mapping:
{
"settings":{
"analysis":{
"char_filter":{
"myHyphenRemoval":{
"type":"mapping",
"mappings":[
"-=>"
]
}
},
"analyzer":{
"default":{
"type":"custom",
"char_filter": [ "myHyphenRemoval" ],
"tokenizer":"standard",
"filter":[
"standard",
"lowercase"
]
}
}
}
},
"mappings":{
"test":{
"properties":{
"title":{
"type":"string"
},
"comment":{
"type":"string"
}
}
}
}
}
使用以下查询完成搜索:
Searching is done with the following query:
{"_source":true,
"query":{
"simple_query_string":{
"query":"<Text>",
"default_operator":"AND"
}
}
}
什么有效:
What works:
U-12"、U*"、t*"、ts*"
"U-12", "U*", "t*", "ts*"
无效的地方:
"U-*", "u-1*", "t-*", "t-sh*", ...
"U-*", "u-1*", "t-*", "t-sh*", ...
所以似乎没有对搜索字符串执行字符过滤器?我能做些什么来完成这项工作?
So it seems the char filter is not executed on search strings? What could I do to make this work?
推荐答案
答案很简单:
引自 Igor Motov:配置标准分词器
Quote from Igor Motov: Configuring the standard tokenizer
默认情况下 simple_query_string 查询不分析单词带通配符.结果它搜索所有以我是一个.单词 i-mac 与此请求不匹配,因为在分析它分为两个标记 i 和 mac 并且这两个都不是代币以 i-ma 开头.为了使这个查询找到 i-mac 你需要让它分析通配符:
By default the simple_query_string query doesn't analyze the words with wildcards. As a result it searches for all tokens that start with i-ma. The word i-mac doesn't match this request because during analysis it's split into two tokens i and mac and neither of these tokens starts with i-ma. In order to make this query find i-mac you need to make it analyze wildcards:
{
"_source":true,
"query":{
"simple_query_string":{
"query":"u-1*",
"analyze_wildcard":true,
"default_operator":"AND"
}
}
}
这篇关于ElasticSearch - 使用连字符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!