Skip to content

Instantly share code, notes, and snippets.

@twillis
Created March 22, 2014 20:02
Show Gist options
  • Save twillis/9713363 to your computer and use it in GitHub Desktop.
Save twillis/9713363 to your computer and use it in GitHub Desktop.
code is data is code is data to index. some basic interaction with elasticsearch
(ns spazzerr.db
(:require [clojurewerkz.elastisch.rest :as esrest]
[clojurewerkz.elastisch.rest.index :as esindex]
[clojurewerkz.elastisch.rest.document :as esdoc]
[clojurewerkz.elastisch.query :as esq]))
(def mapped-type "file")
(def music-mapping {mapped-type {:properties
{:canonical-path {:type "string" :store "yes" :index "not_analyzed"}
:last-modified {:type "date", :store "yes"}
:artist {:type "multi_field"
:fields {:artist {:type "string" :store "yes"}
:original {:type "string" :store "yes" :index "not_analyzed"}}}
:year {:type "integer" :store "yes"}
:title {:type "multi_field"
:fields {:title {:type "string" :store "yes" :index "not_analyzed"}
:original {:type "string" :store "yes" :index "not_analyzed"}}}
:album {:type "multi_field"
:fields {:album {:type "string" :store "yes"}
:original {:type "string" :store "yes" :index "not_analyzed"}}}
:track {:type "integer" :store "yes"}
;;add more later
}}})
(def index-name "spazzerr_tracks")
(defn create-index []
(esindex/create index-name :mappings music-mapping))
(defn get-doc-for-path [path]
(first(:hits(:hits (esdoc/search index-name mapped-type :size 1 :query {:term {:canonical-path path}})))))
(defn upsert-file-doc
"create or update file document based on canonical-path as key if
exists, only updates if last-modified doesnt match"
[data]
(let [doc (get-doc-for-path (:canonical-path data))]
(if (nil? doc)
(do (esdoc/create index-name mapped-type data) data)
(if (not (= (:last-modified data) (:last-modified doc)))
(do (esdoc/replace index-name mapped-type (:_id doc) (merge doc data)) data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment