如何在嵌入式 ElasticSearch 中记录所有查询?
How do I log all queries in embedded ElasticSearch?
我正在尝试调试 ElasticSearch 查询。我已经为有问题的查询启用了解释,这表明该查询正在做中间分数的乘积,而它应该做的是求和。 (我正在使用 elastic4s 创建查询请求。)
问题是我看不到生成的查询实际上是什么。我想确定错误是在elastic4s(错误生成查询请求),在我的代码中,还是在elasticsearch中。因此,我使用以下代码为测试中使用的嵌入式 elasticsearch 实例启用了日志记录:
ESLoggerFactory.setDefaultFactory(new Slf4jESLoggerFactory())
val settings = Settings.settingsBuilder
.put("path.data", dataDirPath)
.put("path.home", "/var/elastic/")
.put("cluster.name", clusterName)
.put("http.enabled", httpEnabled)
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
.put("discovery.zen.ping.multicast.enabled", false)
.put("index.refresh_interval", "10ms")
.put("script.engine.groovy.inline.search", true)
.put("script.engine.groovy.inline.update", true)
.put("script.engine.groovy.inline.mapping", true)
.put("index.search.slowlog.threshold.query.debug", "0s")
.put("index.search.slowlog.threshold.fetch.debug", "0s")
.build
但我在 logback.xml 中配置的日志文件中找不到任何记录的查询。来自 elasticsearch 的其他日志消息出现在那里,只是不是实际的查询。
你不能,至少不能直接,至少不能在当前可用的 ES 版本中。这是已经讨论了很长时间的东西(例如 https://github.com/elastic/elasticsearch/issues/9172 and https://github.com/elastic/elasticsearch/issues/12187) it seems like this may change soon, with the rewrite of the tasks API. In the meantime, you can use things like ES Restlog (https://github.com/etsy/es-restlog) and/or 将 nginx 放在 ES 前面并在 nginx 日志中捕获查询。您还可以使用 tcpdump(例如 tcpdump -vvv -x -X -i any port 9200
)并捕获服务器上的 运行 查询。最后一种选择是修改您的应用程序并回显查询而不是执行它(and/or 在执行之前将查询插入 ES 本身,因为查询本身是 JSON)。
在 elastic4s 的特定情况下,它提供了在 elastic4s 查询对象上调用 .show
以生成请求的 JSON 正文部分 的能力 如果 JSON-over-HTTP 协议已用于发送请求,对于大多数类型的请求。然后可以将其记录在代码中方便的位置,例如如果您有一种方法可以生成所有 ES 搜索查询。 Elasticsearch 中生成假 JSON 的代码当然仍然可能存在错误,因此不应完全信任它。但是,值得尝试通过 HTTP 对真实的 Elasticsearch 集群使用 Sense 来重现 .show
的输出问题 - 如果可以,您 (a) 知道这不是 elastic4s 错误,并且 (b) 可以轻松操纵 JSON 试图找出导致问题的原因。
show
在某些情况下会调用 toString
,因此使用普通的 Elasticsearch API 或其他基于 JVM 的包装器,您可以调用它来获取 JSON 要记录的字符串。
使用嵌入式 Elasticsearch,就日志记录而言,这与您将获得的一样好 - 除了在构建器调用上放置断点并观察实际创建的 Java Elasticsearch 请求对象(这是最准确的方法)。
我正在尝试调试 ElasticSearch 查询。我已经为有问题的查询启用了解释,这表明该查询正在做中间分数的乘积,而它应该做的是求和。 (我正在使用 elastic4s 创建查询请求。)
问题是我看不到生成的查询实际上是什么。我想确定错误是在elastic4s(错误生成查询请求),在我的代码中,还是在elasticsearch中。因此,我使用以下代码为测试中使用的嵌入式 elasticsearch 实例启用了日志记录:
ESLoggerFactory.setDefaultFactory(new Slf4jESLoggerFactory())
val settings = Settings.settingsBuilder
.put("path.data", dataDirPath)
.put("path.home", "/var/elastic/")
.put("cluster.name", clusterName)
.put("http.enabled", httpEnabled)
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
.put("discovery.zen.ping.multicast.enabled", false)
.put("index.refresh_interval", "10ms")
.put("script.engine.groovy.inline.search", true)
.put("script.engine.groovy.inline.update", true)
.put("script.engine.groovy.inline.mapping", true)
.put("index.search.slowlog.threshold.query.debug", "0s")
.put("index.search.slowlog.threshold.fetch.debug", "0s")
.build
但我在 logback.xml 中配置的日志文件中找不到任何记录的查询。来自 elasticsearch 的其他日志消息出现在那里,只是不是实际的查询。
你不能,至少不能直接,至少不能在当前可用的 ES 版本中。这是已经讨论了很长时间的东西(例如 https://github.com/elastic/elasticsearch/issues/9172 and https://github.com/elastic/elasticsearch/issues/12187) it seems like this may change soon, with the rewrite of the tasks API. In the meantime, you can use things like ES Restlog (https://github.com/etsy/es-restlog) and/or 将 nginx 放在 ES 前面并在 nginx 日志中捕获查询。您还可以使用 tcpdump(例如 tcpdump -vvv -x -X -i any port 9200
)并捕获服务器上的 运行 查询。最后一种选择是修改您的应用程序并回显查询而不是执行它(and/or 在执行之前将查询插入 ES 本身,因为查询本身是 JSON)。
在 elastic4s 的特定情况下,它提供了在 elastic4s 查询对象上调用 .show
以生成请求的 JSON 正文部分 的能力 如果 JSON-over-HTTP 协议已用于发送请求,对于大多数类型的请求。然后可以将其记录在代码中方便的位置,例如如果您有一种方法可以生成所有 ES 搜索查询。 Elasticsearch 中生成假 JSON 的代码当然仍然可能存在错误,因此不应完全信任它。但是,值得尝试通过 HTTP 对真实的 Elasticsearch 集群使用 Sense 来重现 .show
的输出问题 - 如果可以,您 (a) 知道这不是 elastic4s 错误,并且 (b) 可以轻松操纵 JSON 试图找出导致问题的原因。
show
在某些情况下会调用 toString
,因此使用普通的 Elasticsearch API 或其他基于 JVM 的包装器,您可以调用它来获取 JSON 要记录的字符串。
使用嵌入式 Elasticsearch,就日志记录而言,这与您将获得的一样好 - 除了在构建器调用上放置断点并观察实际创建的 Java Elasticsearch 请求对象(这是最准确的方法)。