2015-12-18 83 views
0

我有一個問題,如果'ford'在數據庫中,我搜索'forddddddd'它返回一個匹配。對於'fo'這樣的查詢,我有ngrams的子部分匹配,for',ford'但'forddddd'不應該匹配。可能是什麼問題?以下是我的設置,映射和查詢。ElasticSearch問題與匹配結果

設置:

settings: { 
    number_of_shards: 1, 
    analysis: { 
     filter: { 
     ngram_filter: { 
      type: 'edge_ngram', 
      min_gram: 2, 
      max_gram: 15 
     } 
     }, 
     analyzer: { 
     ngram_analyzer: { 
      type: 'custom', 
      tokenizer: 'standard', 
      filter: [ 
      'lowercase', 
      'ngram_filter' 
      ] 
     } 
     } 
    } 
    } 

映射:

mappings: [ 
    { 
     name: 'car', 
     _all: { 
     type: 'string', 
     analyzer: 'ngram_analyzer' 
     }, 
     properties: { 
     description: { 
      properties: { 
      name: { 
       type: 'string', 
       include_in_all: true, 
       term_vector: 'yes', 
       analyzer: 'ngram_analyzer' 
      }, 
      model: { 
       type: 'string', 
       include_in_all: true, 
       term_vector: 'yes', 
       analyzer: 'ngram_analyzer' 
      } 
      } 
     }   
     } 
    } 
    ] 

查詢:

GET car/_search 
{ 
    "query": { 
     "match": { 
      "_all": { 
       "query": "Forddddddd" 
      } 
     } 
    } 
} 

回答

1

這裏的問題是您使用的是相同的analyzerindexingsearching

當你搜索 Forddddd

ngram_analyzer應用和它所產生的令牌FO,爲福特因此它給你的結果,你應該搜索指定standard analyzer,你會得到想要的結果。

properties: { 
    name: { 
     type: 'string', 
     include_in_all: true, 
     term_vector: 'yes', 
     analyzer: 'ngram_analyzer', 
     search_analyzer: 'standard' <--- here 
    }, 
    model: { 
     type: 'string', 
     include_in_all: true, 
     term_vector: 'yes', 
     analyzer: 'ngram_analyzer', 
     search_analyzer: 'standard' <--- here 
    } 
} 
+0

這是行得通!謝謝:) – emarel

+0

很高興我能幫忙! – ChintanShah25