SearchResponse as JSON and back to SearchResponse


(Muecke87) #1

Hi

We are using a Java RestHighLevelClient (v. 5.6.9) to get a SearchResponse.
For testing purpose we store this SearchResponse as JSON to our file system and read it from there to test our search (so that we don't need to read from an real ES-instance). For that we need to convert the JSON back to a SearchResponse. I tried the following code to get it done. The SearchResponse is created but all the fields in the response are empty.

Our JSON looks like this:

{
  "scrollId" : null,
  "totalShards" : 5,
  "successfulShards" : 5,
  "skippedShards" : 0,
  "shardFailures" : [ ],
  "tookInMillis" : 8,
  "hits" : {
    "hits" : [ {
      "score" : 17.641636,
      "id" : "29937491124593,deu,949",
      "type" : "security",
      "nestedIdentity" : null,
      "version" : -1,
      "source" : {
        "symbol" : "E07"
      },
      "fields" : { },
      "highlightFields" : {
        "symbol" : {
          "name" : "symbol",
          "fragments" : [ { } ],
          "fragment" : true
        }
      },
      "sortValues" : [ ],
      "matchedQueries" : [ ],
      "explanation" : null,
      "shard" : null,
      "index" : "securities_2",
      "clusterAlias" : null,
      "sourceAsMap" : {
        "symbol" : "E07"
      },
      "innerHits" : null,
      "sourceRef" : {
        "childResources" : [ ]
      },
      "sourceAsString" : "{\"id\":\"29937491124593\",\"symbol\":\"E07\"}",
      "fragment" : false
    } ],
    "totalHits" : 1,
    "maxScore" : 17.641636,
    "fragment" : true
  },
  "aggregations" : null,
  "suggest" : null,
  "timedOut" : false,
  "terminatedEarly" : null,
  "numReducePhases" : 1,
  "took" : {
    "millis" : 8,
    "hours" : 0,
    "minutes" : 0,
    "seconds" : 0,
    "days" : 0,
    "nanos" : 8000000,
    "stringRep" : "8ms",
    "micros" : 8000,
    "microsFrac" : 8000.0,
    "millisFrac" : 8.0,
    "secondsFrac" : 0.008,
    "minutesFrac" : 1.3333333333333334E-4,
    "hoursFrac" : 2.222222222222222E-6,
    "daysFrac" : 9.259259259259259E-8
  },
  "failedShards" : 0,
  "profileResults" : { },
  "fragment" : false
}

I try to convert it into a SearchResponse like this:

    JSONode fakeData = ...; // read from filesystem. fakeDate contains exakt same value as JSON above
    SearchResponse searchResponse = getSearchResponseFromJsonString(fakeData.toString());

public static SearchResponse getSearchResponseFromJsonString(String jsonResponse) throws IOException {
       
 JsonXContentParser xContentParser = new JsonXContentParser(NamedXContentRegistry.EMPTY, new JsonFactory().createParser(jsonResponse));
     return SearchResponse.fromXContent(xContentParser);
    }

What i get as SearchResonse: (from debugger)

{"took":-1,"timed_out":false,"_shards":{"total":-1,"successful":-1,"skipped":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[{"_score":"-Infinity"}]}}

What am I doing wrong?

Any help appreciated.

Regards Sandro


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.