How to sort result in a Datalog query

喜欢而已 提交于 2019-12-20 12:36:37

问题


I am using datomic with play framework. Play is amazing and datomic is fast. So a good combination overall. Since, I am new to datomic (and datalog i.e. query language datomic uses), I am unable to sort my result ( like we do, order by in sql). For example.

if my query is :

q= [:find ?title 
:where 
[?e :movie/title ?title]
[?e :movie/director "Dave Swag"]
[?e :movie/year ?year]
[(sort ?year)] //here I am trying to sort by year
]

It should return titles of the movies whose director was Dave Swag and result is ordered by year in which image was released. Thankyou :)


回答1:


If you want to sort your result set, you will need to do this outside of the query, on the returned result set. There is an example of this on a Datomic blog post about querying in listing 20:

(def hist (d/history db))

(->> (d/q '[:find ?tx ?v ?op
            :in $ ?e ?attr
            :where [?e ?attr ?v ?tx ?op]]
        hist
        editor-id
        :user/firstName)
   (sort-by first))

=> ([13194139534319 "Ed" true]
    [13194139534335 "Ed" false]
    [13194139534335 "Edward" true])


来源:https://stackoverflow.com/questions/29621159/how-to-sort-result-in-a-datalog-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!