Created
January 24, 2018 08:57
-
-
Save omartell/59cacde662845dae28e8a6fe09b8b46a to your computer and use it in GitHub Desktop.
Vanilla Search Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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