2017-07-31 25 views
0

我有一個需求,我需要通過電話號碼查詢文檔。用戶可以在搜索查詢字符串中輸入括號和破折號等字符,並且應該忽略它們。因此,我創建了一個使用char_filter的自定義分析器,該分析器依次使用pattern_replace標記過濾器刪除除正數之外的數字。但它似乎並沒有像彈性搜索那樣過濾出非數字。這裏是什麼,我試圖做一個樣本:ElasticSearch 5.3 filterer 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。

謝謝。

回答

0

只需要閱讀一些文檔。顯然,我用錯誤的方式查詢索引,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"] 
       } 
      }] 
     } 
    } 
}