如何从scala上的org.elasticsearch.search.suggest.Suggest对象中提取结果

时间:2014-11-23 07:24:37

标签: scala playframework elasticsearch

我尝试使用elasticsearch构建一个建议器,我构建了查询类,并在执行后得到了这个Suggest结果,我无法提取实际文本从中。我现在在scala控制台上尝试这个。

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()是给出这个的方法,但为此我必须将一些map或其他函数作为参数传递给它。我不知道怎么做。你能帮忙吗?

谢谢。

1 个答案:

答案 0 :(得分: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使用并不具体。我现在不接受我的回答:)