2016-12-07 24 views
1

我是elasticsearch的新手,並試圖實現搜索。下面是我的索引和設置curl -XPUT localhost:9200/rets_data/ -d '{ "settings":{ "index":{ "analysis":{ "analyzer":{ "analyzer_startswith":{ "tokenizer":"keyword", "filter":"lowercase" }, "analyzer_whitespacewith":{ "tokenizer":"whitespace", "filter":"lowercase" } } } } }, "mappings":{ "city":{ "properties":{ "CityName":{ "analyzer":"analyzer_startswith", "type":"string" } } }, "rets_aux_subdivision":{ "properties":{ "nn":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "field_LIST_77":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionName":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionAlias":{ "analyzer":"analyzer_whitespacewith", "type":"string" } } }, "rental_aux_subdivision":{ "properties":{ "nn":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "field_LIST_77":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionName":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionAlias":{ "analyzer":"analyzer_whitespacewith", "type":"string" } } } } }'Elasticsearch搜索模式與開始字符串

下面是搜索字符串

curl -XGET localhost:9200/rets_data/rets_aux_subdivision/_search?pretty -d '{"query":{"match_phrase_prefix":{"nn":{"query":"boca w","max_expansions":50}}},"sort":{"total":{"order":"desc"}},"size":100}' 

當我正在尋找像「博卡R」的所有文字,「博卡W」它沒有給我造成的。

我的預期結果如下。

「Boca w」應該給我開始「Boca w」的結果。即「Boca west」,「Boca Woods」,「Boca Winds」

請幫助我。

感謝

+0

您可以發佈一個示例文檔。 –

回答

1

您應該使用edgeNgram。請在elasticsearch文檔中查看。

EdgeNgram濾波器從一個這樣準備多個單詞:

伍茲 - > [W,禾,佑,木材,伍茲]

它使指數越大,但搜索會比更有效像通配符等任何其他選項下面是我簡單的索引創建與title.ngram的n-gram:

{ 
"settings" : { 
"index" : { 
"analysis" : { 
    "analyzer" : { 
     "ngram_analyzer" : { 
     "type" : "custom", 
     "tokenizer" : "standard", 
     "filter" : ["lowercase","my_ngram"] 
     } 
    }, 
    "filter" : { 
    "my_ngram" : { 
    "type" : "edge_ngram", 
    "min_gram" : 1, 
    "max_gram" : 50 
    } 
} 
} 
} 
}, 
    "mappings": 
    { 
    "post": 
    { 
    "properties": 
     { 

     "id": 
     { 
      "type": "integer", 
      "index":"no" 
     }, 
     "title": 
     { 
      "type": "text", 
      "analyzer":"ngram_analyzer" 

     } 

     } 
    } 
} 
} 

和搜索查詢:

{ 
    "from" : 0, 
    "size" : 10, 
"query" : { 
    "match" : { 
     "title": 
     { 
     "query":"press key han", 
     "operator":"or", 
     "analyzer":"standard" 
     } 
     } 
    } 
} 
+0

今天我會試一試。讓我們希望它能解決我的問題。 –

0

如果你有你的match是這樣的:

"query": { 
     "match_phrase": { 
      "text": { 
      "query": "boca w" 
      } 
     } 
     }, 
"sort":{ 
    "total":{ 
     "order":"desc" 
     } 
    }, 
"size":100 

或者你可以使用wildcard查詢:

"query": { 
     "wildcard" : { 
      "yourfield" : "boca w*" 
     } 
    } 

SO可能是有幫助的。希望能幫助到你!

相關問題