Elasticsearch:查找带和不带空格的值
Elasticsearch: Find values with and without whitespaces
我们有一些带有文章编号的字段。这篇文章编号看起来像 AB 987 g567 323。当我搜索“AB 987 g”时,我找到了合适的产品,但是当我没有空格搜索时,我找不到任何东西。我尝试了 pattern_replace,但它不起作用。
"whitespace_filter": {
"alphabets_char_filter": {
"type": "pattern_replace",
"pattern": " ",
"replacement": ""
}
如何搜索带空格和不带空格的文章编号?
您需要使用edge_ngram along with char_filter,来实现您的用例
添加一个工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer",
"char_filter": [
"replace_whitespace"
]
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter",
"digit"
]
}
},
"char_filter": {
"replace_whitespace": {
"type": "mapping",
"mappings": [
"\u0020=>"
]
}
}
}
},
"mappings": {
"properties": {
"articlenumbers": {
"type": "text",
"fields": {
"analyzed": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
索引数据:
{
"articlenumbers": "AB 987 g567 323"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "AB987g",
"fields": [
"articlenumbers",
"articlenumbers.analyzed"
]
}
}
}
搜索结果:
"hits": [
{
"_index": "65936531",
"_type": "_doc",
"_id": "1",
"_score": 1.4384104,
"_source": {
"articlenumbers": "AB 987 g567 323"
}
}
]
我们有一些带有文章编号的字段。这篇文章编号看起来像 AB 987 g567 323。当我搜索“AB 987 g”时,我找到了合适的产品,但是当我没有空格搜索时,我找不到任何东西。我尝试了 pattern_replace,但它不起作用。
"whitespace_filter": {
"alphabets_char_filter": {
"type": "pattern_replace",
"pattern": " ",
"replacement": ""
}
如何搜索带空格和不带空格的文章编号?
您需要使用edge_ngram along with char_filter,来实现您的用例
添加一个工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer",
"char_filter": [
"replace_whitespace"
]
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter",
"digit"
]
}
},
"char_filter": {
"replace_whitespace": {
"type": "mapping",
"mappings": [
"\u0020=>"
]
}
}
}
},
"mappings": {
"properties": {
"articlenumbers": {
"type": "text",
"fields": {
"analyzed": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
索引数据:
{
"articlenumbers": "AB 987 g567 323"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "AB987g",
"fields": [
"articlenumbers",
"articlenumbers.analyzed"
]
}
}
}
搜索结果:
"hits": [
{
"_index": "65936531",
"_type": "_doc",
"_id": "1",
"_score": 1.4384104,
"_source": {
"articlenumbers": "AB 987 g567 323"
}
}
]