Fork me on GitHub
#datalevin
<
2023-04-30
>
rafaeldelboni19:04:28

Hi folks, I've been playing around with Datalevin's fulltext search and I found this:

(require '[datalevin.core :as d])

(let [db (-> (d/empty-db "/tmp/mydb"
                         {:text {:db/valueType :db.type/string
                                 :db/fulltext  true}})
             (d/db-with
              [{:db/id 1 :text "assoc!"}
               {:db/id 2 :text "assoc"}
               {:db/id 3 :text "assoc-in"}
               {:db/id 4 :text "assoc-dom"}
               {:db/id 5 :text "assoc-meta"}
               {:db/id 6 :text "associative?"}]))]
  (d/q '[:find ?e ?a ?v
         :in $ ?q
         :where [(fulltext $ ?q) [[?e ?a ?v]]]]
       db
       "assoc"))

;=> #{[1 :text "assoc!"] [2 :text "assoc"]}
Is possible to tweak the data/query/index to return all the assocs?

2
rafaeldelboni22:04:38

I was reading the search doc and well I just understood the hole idea of fulltext search wrong, I could just use regex for this, sorry about that. For reference:

(let [db (-> (d/empty-db "/tmp/mydb"
                           {:text {:db/valueType :db.type/string}})
               (d/db-with
                [{:db/id 1 :text "assoc!"}
                 {:db/id 2 :text "assoc"}
                 {:db/id 3 :text "assoc-in"}
                 {:db/id 4 :text "assoc-dom"}
                 {:db/id 5 :text "assoc-meta"}
                 {:db/id 6 :text "associative?"}]))]
    (d/q '[:find (pull ?e [*])
           :in $ ?q
           :where
           [(str ".*" ?q ".*") ?pattern]
           [(re-pattern ?pattern) ?regex]
           [(re-matches ?regex ?name)]
           [?e :text ?name]]
         db
         "assoc"))