所以我有一些标记的帖子,我希望用户能够添加标签,然后我希望能够进行查询并获取标签的汇总列表特别的帖子。 (目前行为只返回一个实体列表,每个实体都有一个不同的: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}
有关如何查询此问题的任何提示,或者我如何将所有查询结果混合在一起?提前致谢
答案 0 :(得分:3)
我个人喜欢将sort-by
partition-by
和merge-with
合并为这样的事情,但在这种情况下,因为条目之间的一切都是相同的,除了你可以跳过的标签合并步骤,只需将正确的:标签值粘贴到任意列表条目中(我选择了第一个)
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})