【问题标题】:elasticsearch json java api [duplicate]elasticsearch json java api [重复]
【发布时间】:2014-05-12 11:35:40
【问题描述】:

我正在尝试将 elasticsearch 与 android 应用程序集成。

我需要获取 jsonobject 并打印出每个 id 的详细信息。首先我需要尝试编写java类。

我可以只使用 json api 还是有一个更简单的 elasticsearch api。

网址:http://www.dummy.com:9200/contacts/index/_search

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.0,
    "hits": [
      {
        "_index": "contacts",
        "_type": "index",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "id": "c201",
          "name": "Johnny Depp",
          "email": "johnny_depp@gmail.com",
          "address": "xx-xx-xxxx,x - street, x - country",
          "gender": "male",
          "phone": {
            "mobile": "+91 0000000000",
            "home": "00 000000",
            "office": "00 000000"
          }
        }
      },
      {
        "_index": "contacts",
        "_type": "index",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "id": "c200",
          "name": "Ravi Tamada",
          "email": "ravi@gmail.com",
          "address": "xx-xx-xxxx,x - street, x - country",
          "gender": "male",
          "phone": {
            "mobile": "+91 0000000000",
            "home": "00 000000",
            "office": "00 000000"
          }
        }
      }
    ]
  }
}

【问题讨论】:

  • 我已经设法从 url 获取 json 对象。但是现在要遍历每个联系人,我得到的 org.json.JSONObject 不能转换为 org.json.JSONArray。 @BlackPOP
  • 感觉elasticsearch返回的json对象和正常的json响应字符串不一样。你能建议吗

标签: java json elasticsearch


【解决方案1】:

有几种方法可以创建 JSON 对象.. 像字符串一样手动制作它,使用 MAP。是的,ES 中有一个帮助程序类是 XContentBuilder 。有关 @refer 的更多信息。

根据我的考虑,最好使用任何 JSON API 来创建有效的对象并且易于使用。refer

【讨论】:

  • 这是带有死链接的仅链接答案,这就是为什么仅链接答案很烂的原因
  • @JarrodRoberson 答案不仅有链接,还提到您可以使用 Elasticsearch 提供的任何 json builder、Map 和 XContentBuilder。链接仅供参考..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 2021-06-04
  • 2015-02-10
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
相关资源
最近更新 更多