我正在努力寻找使用nested elasticsearch aggregations进行elastich 2.2.1的示例。我已经浏览了the source tests作为示例,但是只发现了非嵌套的聚合。
我的文档是推文,每个文档都包含一个时间戳和一个情感分数。这是我的命名空间的外观:
(ns tweets.core
(:require [clojurewerkz.elastisch.rest :as es-rest]
[clojurewerkz.elastisch.rest.document :as es-doc]
[clojurewerkz.elastisch.query :as es-q]
[clojurewerkz.elastisch.rest.response :as es-rsp]
[clojurewerkz.elastisch.aggregation :as es-agg]
[clojure.pprint :as pp]))
(clojure.core/refer 'clojure.core)
这段代码按预期工作,在给定的一周内检索了带有给定关键字的一些推文:
(defn get-some-tweets
[kwd]
(let [conn (es-rest/connect local-es-host)
res (es-doc/search conn indices mapping
:query (es-q/filtered :query (es-q/query-string :query kwd)
:filter (es-q/range :timestamp :from "2015-11-19"
:to "2015-11-24")))]
而且我可以使用date_histogram来获取每周这样的推文计数:
(defn counts-by-week
[kwd]
(let [conn (es-rest/connect local-es-host)
res (es-doc/search conn indices mapping
:query (es-q/query-string :query kwd)
:aggregations {:weekly-data (es-agg/date-histogram :timestamp "week")})]
(pp/pprint (get-in res [:aggregations]))))
这是无效的嵌套聚合。我正在尝试按周分类推文,然后在每个每周分类中获取情感统计信息:
(defn avg-weekly-sentiment
[kwd]
(let [conn (es-rest/connect found-es-host {:basic-auth found-auth})
res (es-doc/search conn indices mapping
:query (es-q/query-string :query kwd)
:aggregations {:weekly-data (es-agg/date-histogram :timestamp "week")
:weekly-avg (es-agg/avg "sentiment")})]
(pp/pprint (get-in res [:aggregations]))))
这将返回一个包含按星期显示的文档数以及一个
weekly-avg
键的 map ,我确认该键的值是总体平均情绪。这种嵌套方法会导致错误:
(defn avg-weekly-sentiment2
[kwd]
(let [conn (es-rest/connect found-es-host {:basic-auth found-auth})
res (es-doc/search conn indices mapping
:query (es-q/query-string :query kwd)
:aggregations {:weekly-data (es-agg/date-histogram :timestamp "week")
:aggregations {:weekly-avg (es-agg/avg "sentiment")}})]
(pp/pprint (get-in res [:aggregations]))))
错误是
所以...我对使用Elasticsearch的DSL还是比较陌生,甚至对Clojure还是比较新。我还没有找到使用Clojure包装器嵌套聚合的任何示例,但是它看起来像this has been supported since 2.2.0。我确定我缺少一些简单的东西,但是对于我的一生,我无法弄清楚(我没有展示其他尝试)。
更新:Elastich Google Group指出使用HTTP客户端与本机客户端可能存在问题。果然,我found an example in the test repo for the native client。如果我成功了,我将修改我的代码,发布结果,并关闭问题。
最佳答案
解决的办法是切换到使用本机客户端,然后我按照Elastich测试存储库中的示例进行操作,该示例链接到问题的UPDATE中。这是代码现在的样子...
(ns tweets.core
(:require [clojurewerkz.elastisch.native :as es-native]
[clojurewerkz.elastisch.native.document :as es-doc]
[clojurewerkz.elastisch.native.response :as es-rsp
[clojurewerkz.elastisch.query :as es-q]
[clojurewerkz.elastisch.aggregation :as es-agg]
[clojure.pprint :as pp]))
(clojure.core/refer 'clojure.core)
(def ^:private local-es-nhost [["<the-IP-of-the-ES-server>" 9300]])
;; Note the use of port 9300 instead of 9200 used with the HTTP client
(def ^:private local-cluster {"cluster.name" "elasticsearch"})
;; You can get the cluster name from curl <es-server>:9200/_nodes/cluster
;; The default name is "elasticsearch"
切换到本地客户端后,每周平均情感查询如下所示:
(defn avg-weekly-sentiment
"I would have never guessed to use (merge ...) were it not for the elastich test code"
[kwd]
(let [conn (es-native/connect local-es-nhost local-cluster)
res (es-ndoc/search conn indices mapping
:query (es-q/query-string :query kwd)
:aggregations {:weekly (merge {:aggs {:avg_sentiment (es-agg/avg "sentiment")}}
(es-agg/date-histogram "timestamp" "week"))})]
(pp/pprint (get-in res [:aggregations]))))
结果如下:
{:weekly
{:buckets
[{:doc_count 3572,
:key
#object[org.elasticsearch.common.joda.time.DateTime 0x6eaa586c "2015-10-12T00:00:00.000Z"],
:avg_sentiment {:value 0.39169344904815234}}
{:doc_count 4934,
:key
#object[org.elasticsearch.common.joda.time.DateTime 0x3fed225f "2015-10-19T00:00:00.000Z"],
:avg_sentiment {:value 0.7119037292257783}}
{...
}]}}
关于elasticsearch - 通过Clojure和Elastich在Elasticsearch中进行嵌套聚合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35193357/