Elicsearch Suggestion Payload Access

I am currently using the Java API for Elasticsearch.

Request for proposals, returns several sentences "Offer", which I want to be able to iterate and access variables. This is done by:

val builder = es.prepareSuggest("companies").addSuggestion(new CompletionSuggestionBuilder("companies").field("name_suggest").text(text).size(count()) )   
val suggestResponse = builder.execute().actionGet()
val it = suggestResponse.getSuggest.iterator()

Now

  while (it.hasNext()){
         val info = it.next()
         info.....
  }

I need to have access to information from useful data from "info". A return example looks like this:

{
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "companies": [
    {
      "text": "wells",
      "offset": 0,
      "length": 16,
      "options": [
        {
          "text": "Wells Fargo",
          "score": 123.0,
          "payload": {
            "industry_id": 130,
            "id": 776,
            "popularity": 123
          }
        },
        {
          "text": "Wells Real Estate Funds",
          "score": 140.0,
          "payload": {
            "industry_id": 100,
            "id": 778,
            "popularity": 123
          }
        },
        {
          "text": "Wellstar Health System",
          "score": 126.0,
          "payload": {
            "industry_id": 19,
            "id": 1964,
            "popularity": 123
          }
        }
      ]
    }
  ]
}

When repeating each sentence, I seem to be unable to get the payload. Any ideas on how I can do this?

+4
source share
1 answer

, . ( )

import org.elasticsearch.search.suggest.Suggest.Suggestion
import org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry

...

// Get your "companies" suggestion
val suggestions: Suggestion[Entry] = suggestResponse.getSuggest.getSuggestion("companies")
val it = suggestions.getEntries.iterator()

// Iterate over all entries in the "company" suggestion
while(it.hasNext) {
  val entry = it.next()
  val optionsIt = entry.getOptions.iterator()

  // Iterate over the options of the entry
  while (optionsIt.hasNext) {
    val option = optionsIt.next()

    // As soon as you have the option-object, you can access the payload with various methods
    val payloadAsMap = option.getPayloadAsMap
  }
}

ElasticSearch Github

+3

Source: https://habr.com/ru/post/1526240/


All Articles