我有一個在elasticsearch中索引的數據集。 (索引名稱:demoindex1,類型名稱:SearchTech)。我想使用Java將所有結果以JSON格式返回。我的Java代碼如下:如何使用Java返回Elasticsearch中的所有結果?
import java.net.InetAddress;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;
public class App {
public static void main(String[] args) throws Exception {
Client client = TransportClient.builder().build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("localhost"),
9300));
SearchResponse scrollResp = client.prepareSearch("demoindex1")
.setScroll(new TimeValue(60000)).setTypes("SearchTech").setSize(1000)
.execute().actionGet();
// Scroll until no hits are returned
while (true) {
for (SearchHit hit : scrollResp.getHits()) {
System.out.println(hit.getSourceAsString()); //hit.get
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId())
.setScroll(new TimeValue(60000)).execute().actionGet();
// Break condition: No hits are returned
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
}
}
我的java程序返回每個命中的源代碼部分。但是我想要使用查詢GET index/type/_search獲取所有結果,因爲我們使用查詢GET index/type/_search獲取Elasticsearch。 我需要返回JSON格式的結果數據集。我在這裏做錯了什麼?
- 我已閱讀elasticsearch的JAVA文檔,但找不到任何有關獲取所有結果數據集的內容。
謝謝。
感謝您的回覆。是的,我需要輸出中的整個Json樹。我會嘗試你所說的。謝謝你的回答。 –
歡迎您! – Arad