Skip to content

Instantly share code, notes, and snippets.

@omartell
Created January 24, 2018 08:57
Show Gist options
  • Save omartell/59cacde662845dae28e8a6fe09b8b46a to your computer and use it in GitHub Desktop.
Save omartell/59cacde662845dae28e8a6fe09b8b46a to your computer and use it in GitHub Desktop.
Vanilla Search Index
(ns swiftype.core
(:require [clojure.string :as string]))
(def document-db (atom {}))
(def index (atom {}))
(defn add [id document]
(let [result (swap! document-db assoc id document)
keywords (string/split document #"\s")
with-id (apply hash-map (mapcat (fn [k] [k #{id}]) keywords))]
(reset! index (merge-with (fn [v1 v2]
(set (into v1 v2)))
@index
with-id))
{id document}))
(defn legacy-search [term]
(->> (string/split term #"\s")
(map (fn [t]
(->> (filter (fn [[k v]]
(re-find (re-pattern t)
v)) @index)
(map #(apply hash-map %)))))
flatten))
(defn search [str]
(let [terms (string/split str #"\s")]
(map (fn [t]
{t (get @index t #{})}) terms)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment