【问题标题】:merging certain attributes in datomic query results在 datomic 查询结果中合并某些属性
【发布时间】:2015-05-20 15:31:44
【问题描述】:

所以我有一些被标记的帖子,我希望用户能够添加标签,然后我希望能够进行查询并获得特定帖子的标签汇总列表。 (目前该行为只返回一个实体列表,每个实体都有不同的 :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} 

关于如何查询此内容或如何将所有查询结果混合在一起的任何提示?提前致谢

【问题讨论】:

    标签: clojure datomic data-retrieval


    【解决方案1】:

    我个人喜欢将 sort-by partition-bymerge-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})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多