簡體   English   中英

使用Java傳輸客戶端連接到Elasticsearch。 獲取請求不返回任何內容

[英]Connecting to Elasticsearch with java transport client. Get request returns nothing

我是Elasticsearch的新手,但是我試圖從elasticsearch文檔中實現基本的Java Get API。 https://www.elastic.co/guide/zh-CN/elasticsearch/client/java-api/1.x/get.html

我正在使用TransportClient連接到elasticsearch。 據我了解,TransportClient使用端口9300。Elasticsearch在端口9200上。當我使用指定的集群,索引,類型和ID調用get請求時,我只獲得了一個空字段列表。 我已經使用Sense插件在elasticsearch中創建了索引,並通過head插件對其進行了驗證。 但是,當我嘗試通過我的java實現檢索這些值時,我什么也沒回來。 這是我下面的代碼的一部分。

public String transportClientToElasticSearch()
{
   Settings settings = ImmutableSettings.settingsBuilder()
           .put("client.transport.sniff", "true")
           .put("cluster.name", "elasticsearch").build();

    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
            //.addTransportAddress(new InetSocketTransportAddress("localhost", 9200));

    client.admin().indices().prepareRefresh("index").execute();

   GetResponse response = client.prepareGet("index", "type", "id")
           .execute()
           .actionGet();

   String elasticResponse = response.getFields().toString();
   return elasticResponse;
}

我最初的想法是,這是由於TransportClient和ES的端口號不同所致。 有什么想法嗎? 謝謝。

您應該只通過response.getSource()檢索字段,這將產生Map<String, Object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM