 (defsafe create-document!
   "Put document into elastic"
   [doc]
-  (let [raw-db (mg/get-db (get-dep :raw-db-connection) (env :raw-db-name))]
+  (let [raw-db (get-raw-db)]
     (mc/insert raw-db "subtitle" doc))
   (esd/create (get-dep :db-connection)
               (env :index-name) "subtitle" (prepare-to-index doc)))


(defsafe create-raw-index!
  []
  (let [raw-db (mg/get-db (get-dep :raw-db-connection) (env :raw-db-name))]
    (mc/ensure-index raw-db "subtitle" (array-map :url 1) {:uniquer true})))

(defn prepare-to-index

  (register! :raw-db-connection (mg/connect {:host (env :raw-db-host)
                                             :port (env :raw-db-port)})))

(defn get-raw-db
  []
  (mg/get-db (get-dep :raw-db-connection) (env :raw-db-name)))

(defn get-total-count
  "Update total count of subtitles"
  []
  (-> (esd/search (get-dep :db-connection)
                  (env :index-name) "subtitle")
      :hits
      :total))

