SQL LIKE operator in datomic

假如想象 提交于 2019-12-06 04:51:58

This is what I use. Maybe you can adapt it to your needs.

(defn find-items "Full text search titles and descriptions for [search-term]" [search-term]

    (let [keyys [:item-id :title :description]
          rules '[[(finditem ?item ?term) [(fulltext $ :item/title ?term) [[?item ?name]]]]
                  [(finditem ?item ?term) [(fulltext $ :item/description ?term) [[?item ?name]]]]]
          items (d/q '[:find ?item ?title ?description 
                       :in $ ?term % 
                       :where 
                       (finditem ?item ?term) 
                       [?item :item/title ?title] 
                       [?item :item/description ?description]]
                     (d/db db/CONN) 
                     search-term rules)]
       (map #(zipmap keyys %) items)))

This uses rules which you can have a read about here: http://docs.datomic.com/query.html. Rules work as a pretty good SQL OR equivalent which is how I'm searching for a needle in two haystacks in the above example.

Use the fulltext function. The following example from the Datomic docs illustrates it well:

;; query
[:find ?entity ?name ?tx ?score
 :in $ ?search
 :where [(fulltext $ :artist/name ?search) [[?entity ?name ?tx ?score]]]]

;; inputs
db, "Jane"

;; result
#{[17592186047274 "Jane Birkin" 2839 0.625] 
  [17592186046687 "Jane" 2267 1.0] 
  [17592186047500 "Mary Jane Hooper" 3073 0.5]}

You'll also want to index the fields you're searching for fulltext search in your schema with :db/fulltext true

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