ElasticSearch 5.3 过滤器 char_filter。 pattern_replace 不工作

ElasticSearch 5.3 filterer char_filter. pattern_replace not working

我有一个要求,我需要通过 phone 号码查询文档。用户可以在搜索查询字符串中输入括号和破折号等字符,它们应该是 ignored.So,我创建了一个使用 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) phone

中没有任何括号或破折号的查询
post my_test_index/doc_with_phone_prop/_search
{
    "query": {
        "bool": {
            "must": [
            {
                "query_string": {
                    "query": "3035555555",
                    "fields": ["phone"]
                }
            }]
        }
    }
}

这 return 正确的一个文档:

{
   "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) 带括号的查询不会 return 任何东西,但我假设我的 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。

谢谢。

只需要阅读更多文档。显然,我使用了错误的方式来查询索引,query_string 没有转义特殊字符。我需要将 multi_match 与查询参数一起使用。

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

下面的查询有效并且应用了字符过滤器

post my_test_index/doc_with_phone_prop/_search
{
    "query": {
        "bool": {
            "must": [
            {
                "multi_match": {
                    "query": "(303) 555- 5555",
                    "fields": ["phone"]
                }
            }]
        }
    }
}