Created
March 6, 2013 08:23
-
-
Save vaughnd/5097612 to your computer and use it in GitHub Desktop.
Clojure lucene index search implementation with cache that refreshes every minute
This file contains hidden or 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 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