【问题标题】:ElasticSearch 5.3 filterer char_filter. pattern_replace not workingElasticSearch 5.3 过滤器 char_filter。模式_替换不起作用
【发布时间】: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


    【解决方案1】:

    只需要阅读更多文档。显然,我使用了错误的方式来查询索引,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"]
                    }
                }]
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2015-12-30
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2022-01-17
      • 2013-07-06
      相关资源
      最近更新 更多