在数据查询结果中合并某些属性
merging certain attributes in datomic query results
所以我有一些帖子被标记了,我希望用户能够添加标签,然后我希望能够进行查询并获得特定帖子的标签聚合列表. (目前的行为只是 returns 一个实体列表,每个实体都有不同的 :tags 属性。)
({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ...",
:tags "art", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "snips", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges", <<< ^ How to combine?
:eid 1759})
我的查询看起来像
(defn get-post-by-eid [eid]
(->> (d/q '[:find ?title ?content ?tags ?eid
:in $ ?eid
:where
[?eid post/title ?title]
[?eid post/content ?content]
[?eid post/tag ?tags]] (d/db conn) eid)
(map (fn [[title content tags eid]]
{:title title
:content content
:tags tags
:eid eid}))
(sort-by :eid)))
期望的结果类似于
({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding, art, scissor-less edges, snips", ;;combination
:eid 1759}
关于如何查询此内容或如何将所有查询结果合并在一起的任何提示?提前致谢
我个人喜欢将 sort-by
partition-by
和 merge-with
组合起来做这样的事情,尽管在这种情况下,因为条目之间的所有内容都是相同的,除了您可以跳过的标签合并步骤并将正确的 :tags 值粘贴到任意列表条目中(我选择了第一个)
user> (->>
'({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ...",
:tags "art",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "snips",
:eid 1759}
{:title "other post edges ...",
:content "If you ever ... ",
:tags "example",
:eid 9999}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges",
:eid 1759})
(sort-by :eid)
(partition-by :eid)
(map #(assoc (first %)
:tags (clojure.string/join ", " (map :tags %))))
pprint)
这会生成一系列带有卷起标签的帖子:
({:title
"Straight edges ...",
:content "If you ever ... ",
:tags "folding, art, scissor-less edges, snips, scissor-less edges",
:eid 1759}
{:title "other post edges ...",
:content "If you ever ... ",
:tags "example",
:eid 9999})
所以我有一些帖子被标记了,我希望用户能够添加标签,然后我希望能够进行查询并获得特定帖子的标签聚合列表. (目前的行为只是 returns 一个实体列表,每个实体都有不同的 :tags 属性。)
({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ...",
:tags "art", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "snips", <<< +
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges", <<< ^ How to combine?
:eid 1759})
我的查询看起来像
(defn get-post-by-eid [eid]
(->> (d/q '[:find ?title ?content ?tags ?eid
:in $ ?eid
:where
[?eid post/title ?title]
[?eid post/content ?content]
[?eid post/tag ?tags]] (d/db conn) eid)
(map (fn [[title content tags eid]]
{:title title
:content content
:tags tags
:eid eid}))
(sort-by :eid)))
期望的结果类似于
({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding, art, scissor-less edges, snips", ;;combination
:eid 1759}
关于如何查询此内容或如何将所有查询结果合并在一起的任何提示?提前致谢
我个人喜欢将 sort-by
partition-by
和 merge-with
组合起来做这样的事情,尽管在这种情况下,因为条目之间的所有内容都是相同的,除了您可以跳过的标签合并步骤并将正确的 :tags 值粘贴到任意列表条目中(我选择了第一个)
user> (->>
'({:title "Straight edges ...",
:content "If you ever ... ",
:tags "folding",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ...",
:tags "art",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges",
:eid 1759}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "snips",
:eid 1759}
{:title "other post edges ...",
:content "If you ever ... ",
:tags "example",
:eid 9999}
{:title "Straight edges ...",
:content "If you ever ... ",
:tags "scissor-less edges",
:eid 1759})
(sort-by :eid)
(partition-by :eid)
(map #(assoc (first %)
:tags (clojure.string/join ", " (map :tags %))))
pprint)
这会生成一系列带有卷起标签的帖子:
({:title
"Straight edges ...",
:content "If you ever ... ",
:tags "folding, art, scissor-less edges, snips, scissor-less edges",
:eid 1759}
{:title "other post edges ...",
:content "If you ever ... ",
:tags "example",
:eid 9999})