【问题标题】:When Trying to access Elastic Search get using java getting Empty Response尝试访问 Elastic Search 时使用 java 获取空响应
【发布时间】:2019-03-13 21:54:17
【问题描述】:

按照 Elastic Search 的文档,能够为其 API 设置 Java 环境。 但是,当尝试从名为 twitter 的索引访问时,在名为 tweet 的类型下,id 值为 1,响应为 null,控制台中没有错误。

以上代码

Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch").build();

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        GetResponse response = client.prepareGet("twitter", "tweet", "2").get();
        System.out.println(response.getFields());

        client.close();

Same 的 Kibana 控制台数据

{
        "_index": "twitter",
        "_type": "tweet",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": "Rahul",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch"
        }
      },
      {
        "_index": "twitter",
        "_type": "tweet",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "kimchy",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch"
        }
      }

【问题讨论】:

  • 可以加个链接来准备Get函数吗?
  • curl -XGET 'localhost:9200/twitter/tweet/0?pretty'
  • @Lax 只有当我尝试通过 java 访问它时,数据才能在 kibana 控制台中正常运行,它失败了,甚至检查了集群名称

标签: java elasticsearch kibana


【解决方案1】:

请尝试这样的事情希望它有所帮助

RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http")).build();

        Response response = restClient.performRequest("GET", "/twitter/_search",
               Collections.singletonMap("pretty", "true"));


/*
        JsonObject user = new JsonObject();
        user.addProperty("user","Yash");
        user.addProperty("post_date","2009-11-15T14:12:12");
        user.addProperty("message","Checking json");

        HttpEntity entity = new NStringEntity(user.toString());

        Response indexResponse = restClient.performRequest(
                "PUT",
                "/twitter/tweet/3",
                Collections.<String, String>emptyMap(),
                entity);
*/





        System.out.println(EntityUtils.toString(response.getEntity()));

        restClient.close();

【讨论】:

    【解决方案2】:

    确保您使用的是 HTTP,而不是 HTTPs - 如果您发出简单的纯文本 HTTP 请求,服务器可能会挂断您?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2018-06-04
      • 2020-03-13
      • 1970-01-01
      • 2016-07-07
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多