Python 仅在混合字段中搜索数字(elasticsearch)
我有一个带有电话号码的字段,其格式为XXX-XXX-XXXX或XXXXXXXXX,这是一个合并表 我希望能够搜索XXXXXXXXX,并从这两种格式的结果 我试着用十进制数字过滤器,但没用。 以下是我尝试过的设置,如下所示:Python 仅在混合字段中搜索数字(elasticsearch),python,
elasticsearch,nosql,Python,
elasticsearch,Nosql,我有一个带有电话号码的字段,其格式为XXX-XXX-XXXX或XXXXXXXXX,这是一个合并表 我希望能够搜索XXXXXXXXX,并从这两种格式的结果 我试着用十进制数字过滤器,但没用。 以下是我尝试过的设置,如下所示: mapping = { 'mappings': { DOC_TYPE: { 'properties': { 'first_name': { 'type':
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'
]
}
}
}
}
}
有什么想法吗 在编制索引之前,请使用删除连字符。举个简单的例子:
设置自定义分析器并将其应用于电话字段
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"
}
}
}
GET my_index/_search
{
"query": {
"match": {
"phone": "1234567890"
}
}
}
以XXXXXXXXX格式搜索
GET my_index/_search
{
"query": {
"match": {
"phone": "123-456-7890"
}
}
}
GET my_index/_search
{
"query": {
"match": {
"phone": "1234567890"
}
}
}
到目前为止你试过什么?你能发布你的代码吗?我是elasticsearch的新手,因此如果您对设置有任何意见,请随时联系。