+ private void buildSuggestResult(SearchOperationResult result, String index)
+ throws DocumentStoreOperationException {
+
+ JSONParser parser = new JSONParser ();
+ JSONObject root;
+ try {
+ root = (JSONObject) parser.parse ( result.getResult () );
+ if (result.getResultCode () >= 200 && result.getResultCode () <= 299) {
+ JSONArray hitArray = (JSONArray) root.get ( "suggest-vnf" );
+ JSONObject hitdata = (JSONObject) hitArray.get ( 0 );
+ JSONArray optionsArray = (JSONArray) hitdata.get ( "options" );
+ SuggestHits suggestHits = new SuggestHits ();
+ suggestHits.setTotalHits ( String.valueOf ( optionsArray.size () ) );
+
+ ArrayList<SuggestHit> suggestHitArray = new ArrayList<SuggestHit> ();
+
+ for (int i = 0; i < optionsArray.size (); i++) {
+ JSONObject hit = (JSONObject) optionsArray.get ( i );
+
+ SuggestHit suggestHit = new SuggestHit ();
+ suggestHit.setScore ( (hit.get ( "score" ) != null) ? hit.get ( "score" ).toString () : "" );
+ suggestHit.setText ( (hit.get ( "text" ) != null) ? hit.get ( "text" ).toString () : "" );
+ Document doc = new Document ();
+ if (hit.get ( "_version" ) != null) {
+ doc.setEtag ( (hit.get ( "_version" ) != null) ? hit.get ( "_version" ).toString () : "" );
+ }
+ doc.setUrl ( buildDocumentResponseUrl ( index,
+ (hit.get ( "_id" ) != null) ? hit.get ( "_id" ).toString () : "" ) );
+
+ doc.setContent ( (JSONObject) hit.get ( "payload" ) );
+ suggestHit.setDocument ( doc );
+ suggestHitArray.add ( suggestHit );
+ }
+ suggestHits.setHits ( suggestHitArray.toArray ( new SuggestHit[suggestHitArray.size ()] ) );
+ result.setSuggestResult ( suggestHits );
+
+ JSONObject aggregations = (JSONObject) root.get ( "aggregations" );
+ if (aggregations != null) {
+ AggregationResult[] aggResults =
+ AggregationParsingUtil.parseAggregationResults ( aggregations );
+ AggregationResults aggs = new AggregationResults ();
+ aggs.setAggregations ( aggResults );
+ result.setAggregationResult ( aggs );
+ }
+
+ // success
+ } else {
+ JSONObject error = (JSONObject) root.get ( "error" );
+ if (error != null) {
+ result.setError (
+ new ErrorResult ( error.get ( "type" ).toString (), error.get ( "reason" ).toString () ) );
+ }
+ }
+ } catch (Exception e) {
+ throw new DocumentStoreOperationException (
+ "Failed to parse Elastic Search response." + result.getResult () );
+ }
+ }
+
+
+ }