【发布时间】:2017-08-02 00:35:48
【问题描述】:
我有一个要求,我需要通过电话号码查询文档。用户可以在搜索查询字符串中输入括号和破折号等字符,它们应该被忽略。所以,我创建了一个自定义分析器,它使用 char_filter 反过来使用 pattern_replace 令牌过滤器来删除除带有正则表达式的数字之外的所有内容。但似乎弹性搜索并没有过滤掉非数字。这是我正在尝试做的一个示例:
1) 索引创建
put my_test_index
{
"settings" : {
"index": {
"analysis": {
"char_filter": {
"non_digit": {
"pattern": "\\D",
"type": "pattern_replace",
"replacement": ""
}
},
"analyzer": {
"no_digits_analyzer": {
"type": "custom",
"char_filter": [
"non_digit"
],
"tokenizer": "keyword"
}
}
}
}
},
"mappings" : {
"doc_with_phone_prop" : {
"properties": {
"phone": {
"type": "text",
"analyzer": "no_digits_analyzer",
"search_analyzer": "no_digits_analyzer"
}
}
}
}
}
2) 插入一个文档
put my_test_index/doc_with_phone_prop/1
{
"phone": "3035555555"
}
3) 在手机中不带任何括号或破折号的查询
post my_test_index/doc_with_phone_prop/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "3035555555",
"fields": ["phone"]
}
}]
}
}
}
这会正确返回一个文档:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_test_index",
"_type": "doc_with_phone_prop",
"_id": "1",
"_score": 0.2876821,
"_source": {
"phone": "3035555555"
}
}
]
}
}
4) 用括号查询不会返回任何内容,但我假设我的 no_digits_analyzer 将从搜索词中删除除数字之外的所有内容。
post my_test_index/doc_with_phone_prop/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "\\(303\\)555-5555",
"fields": ["phone"]
}
}]
}
}
}
我在这里做错了什么?
我正在使用 ElasticSearch 5.3。
谢谢。
【问题讨论】:
标签: elasticsearch elasticsearch-5