我用Spring開發了一個REST API條目,它在Elasticsearch中進行搜索,現在我想返回ES發現的任何結果作爲響應。我不關心搜索結果,我不知道其中的JSON結構。我只是想把它還給客戶端。如何將Elasticsarch的結果連接到Spring的REST API響應
我希望像這樣的工作:
@RequestMapping(value = "/search/{index:.*}", method = RequestMethod.GET) public void search(@PathVariable String index, @RequestParam Map allRequestParams, HttpServletResponse response) throws IOException { BoolQueryBuilder query = QueryBuilders.boolQuery(); for (Map.Entry entry : allRequestParams.entrySet()) { query.should(QueryBuilders.fuzzyQuery(entry.getKey(), entry.getValue())); } SearchResponse results = esClient.prepareSearch("nyc_visionzero") .setTypes("logs") .setQuery(query) .execute() .actionGet(); SearchHits hits = results.getHits(); hits.writeTo(response.getOutputStream()); }
但最後一行有一個編譯錯誤,因爲這兩個OutputStreams是不兼容的。所以我的問題是,將Elasticsearch的結果連接到Spring的最簡單方法是什麼?
謝謝,但我已經找到了更好的解決方案,我將在秒後它。 – Mehran
@Mehran,是的,這是一個不錯的。 Upvoted! – alainlompo