我已经使用Spring开发了一个REST API条目,它在Elasticsearch中进行搜索,现在我想返回ES找到的任何结果作为响应 . 我不关心搜索结果,我不知道其中的JSON结构 . 我只是想把它还给客户 .
我希望这样的东西能起作用:
@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());
}
但是最后一行有一个编译错误,因为两个OutputStream不兼容 . 所以我的问题是,将Elasticsearch的结果连接到Spring的响应中的最简单方法是什么?
3 回答
您可以更改搜索方法的签名以返回String,然后直接将结果作为有效JSON返回,而不是尝试写入响应输出流 . 就像是:
我自己设法找到了解决方案:
使用ResponseEntity类,您可以返回任何响应,而无需关心您的结果类型 .