2015-06-29 79 views
0

的部分記錄我試圖使用elasticsearch-rails gem實現使用Rails和彈性搜索的自動完成。Elastic Search Rails查找ID爲

說我有以下記錄:

[{id: 1, name: "John White"}, 
{id:2, name: "Betty Johnson"}] 

我可以使用哪種彈性搜索方法在搜索「約翰」這兩個返回的記錄。

自動完成只會返回「John White」,並且它不會返回id:1。

回答

1

其中一個辦法就是使用edgeNgram filter

PUT office 
{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "default_index":{ 
      "type":"custom", 
      "tokenizer":"standard", 
      "filter":["lowercase","edgeNgram_"] 
     } 
     }, 
     "filter": { 
     "edgeNgram_":{ 
      "type":"edgeNgram", 
      "min_gram":"2", 
      "max_gram":"10" 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "employee":{ 
     "properties": { 
     "name":{ 
      "type": "string" 
     } 
     } 
    } 
    } 
} 

PUT office/employee/1 
{ 
    "name": "John White" 
} 
PUT office/employee/2 
{ 
    "name": "Betty Johnson" 
} 
GET office/employee/_search 
{ 
    "query": { 
    "match": { 
     "name": "John" 
    } 
    } 
} 

其結果將是:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 2, 
     "max_score": 0.19178301, 
     "hits": [ 
     { 
      "_index": "office", 
      "_type": "employee", 
      "_id": "1", 
      "_score": 0.19178301, 
      "_source": { 
       "name": "John White" 
      } 
     }, 
     { 
      "_index": "office", 
      "_type": "employee", 
      "_id": "2", 
      "_score": 0.19178301, 
      "_source": { 
       "name": "Betty Johnson" 
      } 
     } 
     ] 
    } 
} 
+0

感謝一大堆!我得到了這個工作,我想我的結局。唯一的問題是如果你搜索「John Black」。爲什麼會返回結果?任何方式排除這樣的非匹配? – jdkealy