Skip to content

Instantly share code, notes, and snippets.

@vaughnd
Created March 6, 2013 08:23
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 vaughnd/5097612 to your computer and use it in GitHub Desktop.
Save vaughnd/5097612 to your computer and use it in GitHub Desktop.
Clojure lucene index search implementation with cache that refreshes every minute
(ns search-index
(:require [clucy.core :as clucy]
[clojure.core.cache :as cache]))
(def my-data [{:name "bob" :age 32 }
{:name "Susan" :age 24}])
;; refresh cache every minute
(def cache (atom (cache/ttl-cache-factory {} :ttl (* 60 1000))))
(defn build-index
[]
(println "Building index")
(let [mem-idx (clucy/memory-index)]
(doseq [data my-data]
(clucy/add mem-idx my-data))
mem-idx))
(defn get-index
[]
(if (cache/has? @cache :index)
(:index (cache/hit @cache :index))
(let [new-cache (cache/miss @cache :index (build-index))]
(swap! cache (fn [x] new-cache))
(:index new-cache))))
(defn search
[query & {:keys [results] :or {results 15}}]
(clucy/search (get-index) query results ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment