gpt4 book ai didi

python - 仅搜索混合字段中的数字(elasticsearch)

转载 作者:行者123 更新时间:2023-12-03 01:37:18 25 4
gpt4 key购买 nike

我有一个电话号码字段,格式为XXX-XXX-XXXX或XXXXXXXXXX(它是合并表)。

我希望能够搜索XXXXXXXXXX并从两种格式中获取结果。

我尝试使用十进制数字过滤器,但没有用。
这是我尝试过的设置,如下所示:

mapping = {
'mappings': {
DOC_TYPE: {
'properties': {
'first_name': {
'type': 'text',
'analyzer': 'word_splitter'
},
'last_name': {
'type': 'text',
'analyzer': 'word_splitter'
},
'email': {
'type': 'text',
'analyzer': 'email'
},
'gender': {
'type': 'text'
},
'ip_address': {
'type': 'text'
},
'language': {
'type': 'text'
},
'phone': {
'type': 'text',
'analyzer': 'digits'
},
'id': {
'type': 'long'
}

}
}
},
'settings': {
'analysis': {
'analyzer': {
'my_analyzer': {
'type': 'whitespace'
},
'better': {
'type': 'standard'
},
'word_splitter': {
'type': 'custom',
'tokenizer': 'nGram',
'min_gram': 5,
'max_gram': 5,
'filter': [
'lowercase'
]
},
'email': {
'type': 'custom',
'tokenizer': 'uax_url_email'
},
'digits': {
'type': 'custom',
'tokenizer': 'whitespace',
'filter': [
'decimal_digit'
]
}
}
}
}
}

有任何想法吗 ?

最佳答案

在建立索引之前,请使用char_filter除去连字符。作为一个简单的例子:

设置自定义分析器并将其应用于电话字段。

PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"phone_analyzer": {
"tokenizer": "standard",
"char_filter": [
"phone_char_filter"
]
}
},
"char_filter": {
"phone_char_filter": {
"type": "mapping",
"mappings": [
"- => "
]
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"phone": {
"type": "text",
"analyzer": "phone_analyzer"
}
}
}
}
}

添加一些文档
POST my_index/_doc
{"phone": "123-456-7890"}

POST my_index/_doc
{"phone": "2345678901"}

以xxx-xxx-xxxx格式搜索
GET my_index/_search
{
"query": {
"match": {
"phone": "123-456-7890"
}
}
}

以xxxxxxxxxx格式搜索
GET my_index/_search
{
"query": {
"match": {
"phone": "1234567890"
}
}
}

关于python - 仅搜索混合字段中的数字(elasticsearch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com