如何在 Clojure 中从映射向量(Elasticseach 存储桶)检索给定键的 doc_count

发布于 2025-01-11 04:17:40 字数 286 浏览 4 评论 0原文

从 Clojure Elasticsearch 聚合查询中,我有一个映射的存储桶向量,如下所示,其中键是数字标识符,doc_count 是出现次数。

:buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}]

给定一个类似 14768496 的值我希望能够在这里检索 doc_count 464

From a Clojure Elasticsearch aggregation query, I have a buckets vector of maps like the following where the key is a numerical identifier and the doc_count is the number of occurrences.

:buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}]

Given a value like 14768496 I would like to be able to retrieve the doc_count, here 464.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2025-01-18 04:17:40

我对OP自己的答案提供了一些反馈,但认为它本身值得作为答案提供:

user> (def buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}])
#'user/buckets
user=> (def accounts (into {} (map (juxt :key :doc_count)) buckets))
#'user/accounts

这使用 map 的传感器生成数量作为 into 的“xform”参数 因此它可以避免创建任何中间惰性序列。

您还可以执行 (into {} (map (juxt :key :doc_count) buckets)) ,这将生成向量对(键和文档计数)的惰性序列,然后“倒入" 将其放入一个空的哈希映射中。

juxt 返回一个只有一个参数的函数,该函数从每个参数(传递给 juxt 的函数)到该参数的应用生成一个向量:

user=> ((juxt inc dec) 42)
[43 41]

I provided some feedback on the OP's own answer but figured it was worth providing as an answer in its own right:

user> (def buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}])
#'user/buckets
user=> (def accounts (into {} (map (juxt :key :doc_count)) buckets))
#'user/accounts

This uses the transducer-producing arity of map as the "xform" argument to into so it avoids creating any intermediate lazy sequences.

You could also do (into {} (map (juxt :key :doc_count) buckets)) which will produce a lazy sequence of vector pairs (of the key and the document count), and then "pour" it into an empty hash map.

juxt returns a function of one parameter that produces a vector from the application of each argument (the functions passed to juxt) to that parameter:

user=> ((juxt inc dec) 42)
[43 41]
以往的大感动 2025-01-18 04:17:40

在提出问题时,我遇到了以下解决方案,我现在想分享该解决方案,因为我花了一段时间才找到正确的方法。

(def accounts (apply hash-map
                      (mapcat
                        #(vector (% :key) (% :doc_count))
                        buckets)))

这会产生以下地图:

{14768496 464, 14761312 440, 14764921 412}

现在检索很简单:

(println (accounts 14768496))

While crafting the question I came across the following solution which I now want to share since it took me a while to find the right approach.

(def accounts (apply hash-map
                      (mapcat
                        #(vector (% :key) (% :doc_count))
                        buckets)))

This produces the following map:

{14768496 464, 14761312 440, 14764921 412}

Now the retrieval is straightforward:

(println (accounts 14768496))
说不完的你爱 2025-01-18 04:17:40

创建帐户地图的不同方式:

user> (def buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}])
#'user/buckets
user> (def accounts (zipmap (map :key buckets) (map :doc_count buckets)))
#'user/accounts

或者...

user> (defn find-doc-count [k buckets] (some #(when (= (:key %) k) (:doc_count %)) buckets))
#'user/find-doc-count
user> (find-doc-count 14768496 buckets)
464

A different way of creating your accounts map:

user> (def buckets [{:key 14768496, :doc_count 464} {:key 14761312, :doc_count 440} {:key 14764921, :doc_count 412}])
#'user/buckets
user> (def accounts (zipmap (map :key buckets) (map :doc_count buckets)))
#'user/accounts

Alternatively ...

user> (defn find-doc-count [k buckets] (some #(when (= (:key %) k) (:doc_count %)) buckets))
#'user/find-doc-count
user> (find-doc-count 14768496 buckets)
464
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文