Skip to content

Instantly share code, notes, and snippets.

@coxchen
Last active August 29, 2015 14:15
Show Gist options
  • Save coxchen/d61388442d1be949ddb3 to your computer and use it in GitHub Desktop.
Save coxchen/d61388442d1be949ddb3 to your computer and use it in GitHub Desktop.
testing ElasticSearch _ttl using restbot
;;;;;;;;;;;;;;;;;;;;
;; MAPPINGs
;;;;;;;;;;;;;;;;;;;;
(def mapping-log
{:log {:_ttl {:enabled true
:default "2h"}
:_timestamp {:enabled true
:path "log_time"}
:properties {:log_time {:type "date"}
:log_count {:type "integer"}}}})
(def mapping-ttl
{:ttl
{:properties
{:timestamp {:type "date"}
:id {:type "string" :analyzer "keyword"}
:ttl {:type "long"}}}})
;;;;;;;;;;;;;;;;;;;;
;; APIs
;;;;;;;;;;;;;;;;;;;;
(def-api root :GET "/")
(def-api create-index :POST "/{index}")
(def-api check-mapping :GET "/{index}/_mapping/?pretty")
(def-api del-index :DEL "/{index}")
(def-api create-doc :POST "/{index}/{type}")
(def-api index-doc :POST "/{index}/{type}/{docid}")
(def-api update-doc :POST "/{index}/{type}/{docid}/_update")
(def-api view-doc :GET "/{index}/{type}/{docid}/?pretty&fields=_ttl")
;;;;;;;;;;;;;;;;;;;;
;; SERVERs
;;;;;;;;;;;;;;;;;;;;
(def-server local "http://localhost:9200")
;;;;;;;;;;;;;;;;;;;;
;; TASKs
;;;;;;;;;;;;;;;;;;;;
(def-task sanity
:check-root [] (root))
(def-task init-index
:init-idx [] (create-index :params {:index "activity"}
:body (map2json {:settings {:number_of_replicas 0}
:mappings (merge mapping-log mapping-ttl)})))
(def-task check-index
:mapping [] (check-mapping :params {:index "activity"}))
(def-task delete-index
:del [] (del-index :params {:index "activity"}))
(defn now [] (.getTime (new java.util.Date)))
(def-task add-logs
:add1 [] (index-doc :params {:index "activity" :type "log" :docid "1"}
:body (map2json {:log_time (now)
:log_count 1}))
:add2 [] (index-doc :params {:index "activity" :type "log" :docid "2"}
:body (map2json {:log_time (now)
:log_count 1})))
(def-task view-logs
:view1 [] (view-doc :params {:index "activity" :type "log" :docid "1"})
:view2 [] (view-doc :params {:index "activity" :type "log" :docid "2"}))
(defn extract-ttl
[resp] (get-in resp [:resp :content :fields :_ttl]))
(def-task check-ttl
:up1 [] (update-doc :params {:index "activity" :type "log" :docid "2"}
:body (map2json {:script "ctx._source.log_count+=1"}))
:view1 [up1] (view-doc :params {:index "activity" :type "log" :docid "1"})
:ttl1 [view1] (create-doc :params {:index "activity" :type "ttl"}
:body (map2json {:id "1" :timestamp (now) :ttl (extract-ttl view1)}))
:view2 [ttl1] (view-doc :params {:index "activity" :type "log" :docid "2"})
:ttl2 [view2] (create-doc :params {:index "activity" :type "ttl"}
:body (map2json {:id "2" :timestamp (now) :ttl (extract-ttl view2)})))
(def-task check-ttl2
:up1 [] (update-doc :params {:index "activity" :type "log" :docid "2"}
:body (map2json {:script "ctx._source.log_count+=1"}))
:up2 [up1] (update-doc :params {:index "activity" :type "log" :docid "2"}
:body (map2json {:script "ctx._ttl=\"2h\""}))
:view1 [up2] (view-doc :params {:index "activity" :type "log" :docid "1"})
:ttl1 [view1] (create-doc :params {:index "activity" :type "ttl"}
:body (map2json {:id "1" :timestamp (now) :ttl (extract-ttl view1)}))
:view2 [ttl1] (view-doc :params {:index "activity" :type "log" :docid "2"})
:ttl2 [view2] (create-doc :params {:index "activity" :type "ttl"}
:body (map2json {:id "2" :timestamp (now) :ttl (extract-ttl view2)})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment