如何让 Elasticsearch 忽略由 char_filter 清空的术语?
How do I get Elasticsearch to ignore terms emptied by a char_filter?
我有一组已编入索引的美国街道地址。源数据不完善,有时字段包含垃圾。具体来说,我有 zip5
和 zip4
字段以及一个 pattern_replace
char_filter
来去除任何非数字字符。当 char_filter
最终替换所有内容(产生一个空字符串)时,匹配似乎仍在查看该字段。如果原始字段只是一个空字符串(而不是 null),也会发生同样的情况。我如何设置它以便忽略空字符串字段(按来源或按 char_filter 的结果)?
例子
首先,让我们用 digits_only
模式替换器和使用它的分析器创建一个索引:
curl -XPUT "http://localhost:9200/address_bug" -d'
{
"settings": {
"index": {
"number_of_shards": "4",
"number_of_replicas": "1"
},
"analysis": {
"char_filter" : {
"digits_only" : {
"type" : "pattern_replace",
"pattern" : "([^0-9])",
"replacement" : ""
}
},
"analyzer" : {
"zip" : {
"type" : "custom",
"tokenizer" : "keyword",
"char_filter" : [
"digits_only"
]
}
}
}
}
}'
现在,让我们创建一个使用分析器的映射(注意:我正在使用 with_positions_offsets
突出显示 ):
curl -XPUT "http://localhost:9200/address_bug/_mapping/address" -d'
{
"address": {
"properties": {
"zip5": {
"type" : "string",
"analyzer" : "zip",
"term_vector" : "with_positions_offsets"
},
"zip4": {
"type" : "string",
"analyzer" : "zip",
"term_vector" : "with_positions_offsets"
}
}
}
}'
现在我们的索引和类型已经设置好了,让我们索引一些不完美的数据:
curl -XPUT "http://localhost:9200/address_bug/address/1234" -d'
{
"zip5" : "02144",
"zip4" : "ABCD"
}'
好吧,让我们搜索它并要求它自己explain
。在这种情况下,搜索词是 Street 因为在我的实际应用程序中,我有一个用于完整地址搜索的字段。
curl -XGET "http://localhost:9200/address_bug/address/_search?explain" -d'
{
"query": {
"match": {
"zip4": "Street"
}
}
}'
而且,这是 结果 中有趣的部分:
"_explanation": {
"value": 0.30685282,
"description": "weight(zip4: in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 0.30685282,
"description": "fieldWeight in 0, product of:",
"details": [
{
"value": 1,
"description": "tf(freq=1.0), with freq of:",
"details": [
{
"value": 1,
"description": "termFreq=1.0"
}
]
},
{
"value": 0.30685282,
"description": "idf(docFreq=1, maxDocs=1)"
},
{
"value": 1,
"description": "fieldNorm(doc=0)"
}
]
}
]
}
(完整回复为 in this gist。)
预期结果
我没想到会有任何成功。如果我改为使用 "zip4" : null
索引文档,它会产生预期的结果:没有命中。
帮忙?我什至在这里采取了正确的方法吗?在我的完整应用程序中,我对 phone
字段使用了相同的技术,并怀疑我对结果有同样的问题。
如@plmaheu 所述,您可以使用停止令牌过滤器完全删除
空字符串,例如,这是我测试过的配置
作品:
POST /myindex
{
"settings": {
"analysis": {
"char_filter" : {
"digits_only" : {
"type" : "pattern_replace",
"pattern" : "[^0-9]+",
"replacement" : ""
}
},
"filter": {
"remove_empty": {
"type": "stop",
"stopwords": [""]
}
},
"analyzer" : {
"zip" : {
"type" : "custom",
"tokenizer" : "keyword",
"char_filter" : [
"digits_only"
],
"filter": ["remove_empty"]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"zip": {
"type": "string",
"analyzer": "zip"
}
}
}
}
}
此处 remove_empty
过滤器删除停用词“”,如果您使用分析
API 在字符串 "abcd" 上,你得到响应 {"tokens":[]}
,所以没有
如果邮政编码完全无效,令牌将被编入索引。
我在搜索"foo"时也测试了这个作品,没有找到结果。
您可以像这样使用 length token filter:
"filter": {
"remove_empty": {
"type": "length",
"min": 1
}
}
我有一组已编入索引的美国街道地址。源数据不完善,有时字段包含垃圾。具体来说,我有 zip5
和 zip4
字段以及一个 pattern_replace
char_filter
来去除任何非数字字符。当 char_filter
最终替换所有内容(产生一个空字符串)时,匹配似乎仍在查看该字段。如果原始字段只是一个空字符串(而不是 null),也会发生同样的情况。我如何设置它以便忽略空字符串字段(按来源或按 char_filter 的结果)?
例子
首先,让我们用 digits_only
模式替换器和使用它的分析器创建一个索引:
curl -XPUT "http://localhost:9200/address_bug" -d'
{
"settings": {
"index": {
"number_of_shards": "4",
"number_of_replicas": "1"
},
"analysis": {
"char_filter" : {
"digits_only" : {
"type" : "pattern_replace",
"pattern" : "([^0-9])",
"replacement" : ""
}
},
"analyzer" : {
"zip" : {
"type" : "custom",
"tokenizer" : "keyword",
"char_filter" : [
"digits_only"
]
}
}
}
}
}'
现在,让我们创建一个使用分析器的映射(注意:我正在使用 with_positions_offsets
突出显示 ):
curl -XPUT "http://localhost:9200/address_bug/_mapping/address" -d'
{
"address": {
"properties": {
"zip5": {
"type" : "string",
"analyzer" : "zip",
"term_vector" : "with_positions_offsets"
},
"zip4": {
"type" : "string",
"analyzer" : "zip",
"term_vector" : "with_positions_offsets"
}
}
}
}'
现在我们的索引和类型已经设置好了,让我们索引一些不完美的数据:
curl -XPUT "http://localhost:9200/address_bug/address/1234" -d'
{
"zip5" : "02144",
"zip4" : "ABCD"
}'
好吧,让我们搜索它并要求它自己explain
。在这种情况下,搜索词是 Street 因为在我的实际应用程序中,我有一个用于完整地址搜索的字段。
curl -XGET "http://localhost:9200/address_bug/address/_search?explain" -d'
{
"query": {
"match": {
"zip4": "Street"
}
}
}'
而且,这是 结果 中有趣的部分:
"_explanation": {
"value": 0.30685282,
"description": "weight(zip4: in 0) [PerFieldSimilarity], result of:",
"details": [
{
"value": 0.30685282,
"description": "fieldWeight in 0, product of:",
"details": [
{
"value": 1,
"description": "tf(freq=1.0), with freq of:",
"details": [
{
"value": 1,
"description": "termFreq=1.0"
}
]
},
{
"value": 0.30685282,
"description": "idf(docFreq=1, maxDocs=1)"
},
{
"value": 1,
"description": "fieldNorm(doc=0)"
}
]
}
]
}
(完整回复为 in this gist。)
预期结果
我没想到会有任何成功。如果我改为使用 "zip4" : null
索引文档,它会产生预期的结果:没有命中。
帮忙?我什至在这里采取了正确的方法吗?在我的完整应用程序中,我对 phone
字段使用了相同的技术,并怀疑我对结果有同样的问题。
如@plmaheu 所述,您可以使用停止令牌过滤器完全删除 空字符串,例如,这是我测试过的配置 作品:
POST /myindex
{
"settings": {
"analysis": {
"char_filter" : {
"digits_only" : {
"type" : "pattern_replace",
"pattern" : "[^0-9]+",
"replacement" : ""
}
},
"filter": {
"remove_empty": {
"type": "stop",
"stopwords": [""]
}
},
"analyzer" : {
"zip" : {
"type" : "custom",
"tokenizer" : "keyword",
"char_filter" : [
"digits_only"
],
"filter": ["remove_empty"]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"zip": {
"type": "string",
"analyzer": "zip"
}
}
}
}
}
此处 remove_empty
过滤器删除停用词“”,如果您使用分析
API 在字符串 "abcd" 上,你得到响应 {"tokens":[]}
,所以没有
如果邮政编码完全无效,令牌将被编入索引。
我在搜索"foo"时也测试了这个作品,没有找到结果。
您可以像这样使用 length token filter:
"filter": {
"remove_empty": {
"type": "length",
"min": 1
}
}