Skip to content

Instantly share code, notes, and snippets.

@ulises
Last active December 21, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulises/6255940 to your computer and use it in GitHub Desktop.
Save ulises/6255940 to your computer and use it in GitHub Desktop.
(ns batcher.search
(:require [clojure.string :as string]))
(defn tokenise [text]
(string/split text #" "))
(defn index-document [{:keys [id text] :as document}]
(let [tokens (tokenise text)]
(map hash-map tokens (repeat (set [id])))))
(defn index [documents]
(apply merge-with into
(apply concat (map index-document documents))))
(defn search [index query]
(let [query-tokens (tokenise query)]
(reduce into #{} (map #(get index % #{}) query-tokens))))
(defn run []
(let [d1 {:id :foo :text "hello there"}
d2 {:id :bar :text "hello world"}
idx (index [d1 d2])]
[(search idx "hello there")
(search idx "world")]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment