如何在 Clojure 中从映射向量(Elasticseach 存储桶)检索给定键的 doc_count
从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对OP自己的答案提供了一些反馈,但认为它本身值得作为答案提供:
这使用
map
的传感器生成数量作为into 的“xform”参数
因此它可以避免创建任何中间惰性序列。您还可以执行
(into {} (map (juxt :key :doc_count) buckets))
,这将生成向量对(键和文档计数)的惰性序列,然后“倒入" 将其放入一个空的哈希映射中。juxt
返回一个只有一个参数的函数,该函数从每个参数(传递给juxt
的函数)到该参数的应用生成一个向量:I provided some feedback on the OP's own answer but figured it was worth providing as an answer in its own right:
This uses the transducer-producing arity of
map
as the "xform" argument tointo
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 tojuxt
) to that parameter:在提出问题时,我遇到了以下解决方案,我现在想分享该解决方案,因为我花了一段时间才找到正确的方法。
这会产生以下地图:
现在检索很简单:
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.
This produces the following map:
Now the retrieval is straightforward:
创建
帐户
地图的不同方式:或者...
A different way of creating your
accounts
map:Alternatively ...