根据弹性搜索的JSON响应形成自定义JSON对象

时间:2018-06-26 11:01:07

标签: java json elasticsearch

从ElasticSearch得到响应后,我想从该响应中形成另一个字段有限的JSON(例如自定义JSONObject)。

请找到通过弹性搜索得到的响应。

{  
   "took":93,
   "timed_out":false,
   "_shards":{  
      "total":5,
      "successful":5,
      "skipped":0,
      "failed":0
   },
   "hits":{  
      "total":1,
      "max_score":1.0,
      "hits":[  
         {  
            "_index":"attachment",
            "_type":"doc",
            "_id":"87740",
            "_score":1.0,
            "_source":{  
               "app_language":"ES",
               "filetype":"PB",
               "attachment":{  
                  "date":"2006-05-03T15:17:53Z",
                  "content_type":"application/pdf",
                  "author":"JJamesN",
                  "language":"en",
                  "title":"Microsoft Word - te7000pb.doc",
                  "content":"European Electronic Controls Catalog ",
                  "content_length":12267
               },
               "ext":"pdf",
               "fileContent":"JVBERi0xLjQNJeLjz9MNCjQ3ID"
            }
         }
      ]
   }
}

请找到我的Java代码,该Java代码试图处理响应JSON以创建具有有限字段的单独JSON。

JSONObject jsonObject = new JSONObject(responseBody);
JSONObject  hits = jsonObject.getJSONObject("hits");
JSONArray hitsArray=hits.getJSONArray("hits");
System.out.println("Hits---->"+hitsArray.toString());

从响应JSON中,我只想创建一个具有以下结构和字段的新JSON。

 {
     "app_language":"ES",
     "filetype":"PB",
     "attachment.content" : "European Electronic Controls Catalog ",
     "ext":"pdf",
 }

1 个答案:

答案 0 :(得分:1)

尝试一下:

JSONObject jsonBody = new JSONObject();
                jsonBody.put("app_language","value");
                jsonBody.put("filetype","value");
                jsonBody.put("attachment.content","value");
                jsonBody.put("ext",ext);

//String requestBody = jsonBody.toString();

只需从响应中提取您的值,然后在任何需要的地方使用它即可。 希望这会有所帮助。