2014-11-23 64 views
0

我試圖建立使用elasticsearch一個建議者提取結果,我構建的查詢類和執行後,我得到這個Suggest結果,我無法從中提取實際文本。我現在正在scala控制檯上嘗試這個。如何從org.elasticsearch.search.suggest.Suggest對象斯卡拉

scala> result 
res75: org.elasticsearch.search.suggest.Suggest = 
{ 
    "first_name" : [ { 
    "text" : "hari", 
    "offset" : 0, 
    "length" : 4, 
    "options" : [ { 
     "text" : "HARIA", 
     "score" : 1.0 
    }, { 
     "text" : "HARID", 
     "score" : 1.0 
    }, { 
     "text" : "HARIDAS", 
     "score" : 1.0 
    }, { 
     "text" : "HARIDASN", 
     "score" : 1.0 
    }, { 
     "text" : "HARIDASNSS", 
     "score" : 1.0 
    }] 
    } ] 
} 

scala> result.getSuggestion() 
<console>:25: error: not enough arguments for method getSuggestion: (x$1: String)T. 
Unspecified value parameter x$1. 
       result.getSuggestion() 

如何從上述結果中提取text。我可以看到result.getSuggestion()是,這將給這個方法,但我必須通過一些地圖或其它功能參數傳遞給它。我不知道如何做到這一點..你能幫忙嗎?

謝謝。

+0

https://gist.github.com/evanwong/6511366 - 這是有點兒節省我的時間。如果我完成它將發佈結果。 – 2014-11-23 07:42:28

回答

0

我從這個環節的工作溶液中 - https://groups.google.com/forum/#!topic/elasticsearch/FwRv0D5qIi8

的解決方案是 -

import org.elasticsearch.node.NodeBuilder._ 
    import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder 
    import org.elasticsearch.search.suggest.completion.CompletionSuggestion 

    // Build query for searching the client. 
    var suggester = new CompletionSuggestionBuilder("first_name") 
    suggester.text(query) 
    suggester.field("suggest") 
    suggester.size(5) 
    var suggestions = client.prepareSuggest("autocomplete") 
         .addSuggestion(suggester) 
         .execute() 
         .actionGet() 

    val suggest = suggestions.getSuggest() 

    if (suggest.size > 0) { 
     val optIter = suggest 
        .iterator() 
        .next() 
        .getEntries() 
        .get(0) 
        .iterator() 

     while(optIter.hasNext()){ 
      println(optIter.next().getText()) 
     } 
    } 

我知道這個問題是很少針對特定API的使用。我不接受我的答案,現在我自己:)