             [clojure.string :as string])
   (:import [org.bson.types ObjectId]))
 
-(def minimum-term-length 3)
+(def minimum-term-length 2)
 (def search-limit 100)
 (def not-too-short-term? #(>= (count %) minimum-term-length))
 

  (let [query-string (:q query)]
    (if (nil? query-string)
      '()
      (apply search (string/split query-string #" +")))))

(defn kms[d]
  (str "km:" (quot d 1000)))

(def re-term #"[a-zåäö#0-9\-]+")

(defn to-term-seq [^String words]
  (if words
    (re-seq re-term (.toLowerCase (strip-html words)))
    []))

(defn to-bare-term-list [exercise] 
  (concat 
   (to-term-seq (:user exercise))
   (to-term-seq (:body exercise))
   (to-term-seq (:title exercise))
   (to-term-seq (:sport exercise))
   (to-term-seq (reduce (fn [a b] 
                          (if (and b (:body b)) 
                            (str a " " (:body b)) 
                            a)) 
                        "" (:comments exercise)))
                     ))

(defn get-ex-terms [exercise]
  (let [ex-date (:creationDate exercise)
        ex-distance (:distance exercise)
        bare-term-list (to-bare-term-list exercise)
        term-list (filter not-too-short-term? bare-term-list)]
    term-list))      
          
(def inverted-index (atom {}))

(defn insert-term [assoc-fn index ex-id term]
  (if-let [postings (index term)]
    (assoc-fn index term #{ex-id})
    (let [new-val (conj (index term) ex-id)]
      (assoc-fn index term new-val))))

(defn insert-exercise-to-index [assoc-fn index ex]
  (let [terms (get-ex-terms ex)
        ex-id (str (:_id ex))]
    (reduce (partial insert-term assoc-fn index) terms)))

(def insert-exercise-inmem-index
  (partial insert-exercise-to-index assoc @inverted-index))
  
(defn rebuild-index []
  (let [new-index (transient {})]
    (count (do (reset! inverted-index (persistent! (reduce (partial insert-exercise-to-index assoc!) new-index (mc/find-maps EXERCISE {}))))))))

(defn search-ids [& terms]
  (let [filtered-terms (filter not-too-short-term? terms)]
