【问题标题】:How track json request sent to Elasticsearch via elastic4s client?如何跟踪通过 elastic4s 客户端发送到 Elasticsearch 的 json 请求?
【发布时间】:2015-09-01 02:38:17
【问题描述】:

说我用这样的代码:

ElasticClient client = ...
client.execute{search in "places"->"cities" query "paris" start 5 limit 10}

如何查看发送到 Elasticsearch 的 json 请求是什么?

【问题讨论】:

    标签: json scala http elasticsearch elastic4s


    【解决方案1】:

    在 Elastic4s 1.6.2 中,您可以在多个请求上使用 show typeclass 以获取等效的 JSON。

    这很简单。

    val req = search in "index" / "type" query "kate bush"
    logger.debug(s"Search request ${req.show}")
    

    .show 方法将呈现 JSON 输出。它适用于大多数请求类型。

    在 Elastic4s 5.2.0+ 中,您在客户端使用 show 方法。

    val req = search("index" / "type").query("kate bush")
    client.show(req)
    

    【讨论】:

      【解决方案2】:

      我没有找到通过 elastic4s 客户端跟踪每个请求的内置功能,但是在 elastic4s 中有一个 _builder 变量,您可以在执行它之前使用它来打印请求:

      println(search in "places"->"cities" query "paris" start 5 limit 10 _builder) toString
      
      {
        "from" : 5,
        "size" : 10,
        "query" : {
          "query_string" : {
            "query" : "paris"
          }
        }
      }
      

      【讨论】:

      • 这不适用于每种请求类型。 1.6 版将具有记录所有查询(或大部分查询)的功能。原因是java客户端不会把东西转成json,而是反过来。 REST 接口在内部将 json 转换为 java 调用。
      • 能否提供非工作请求的示例?
      • 我不记得了,但是一些不太常见的不输出 json (或者至少当我 2 年前第一次启动 elastic4s 时它们没有输出。也许现在它们都输出了)。无论如何,我将添加一些日志来公开这些内容。
      • UpdateDsl 是您不能简单地调用 toString 的示例。
      猜你喜欢
      • 2023-01-13
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多