Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns api.db.search
- (:require [clojurewerkz.elastisch.rest :as es]
- [clojurewerkz.elastisch.rest.index :as ei]
- [clojurewerkz.elastisch.rest.document :as ed]
- [clojurewerkz.elastisch.query :as q]
- [environ-plus.core :refer [env]]
- [api.db.user :as user]
- [api.db.work :as work]
- [taoensso.timbre :refer [info]]))
- (def conn (es/connect (:elastic env)))
- (defonce DB "hoolay")
- (def ik-type {:indexAnalyzer "ik"
- :searchAnalyzer "ik"
- :term_vector "no"
- :store "false"})
- (def id-type {:type "string"
- :analyzer "keyword"
- :include_in_all true})
- (defn pinyin-type
- [field]
- {:type "multi_field"
- :fields {field {:type "string"
- :store "no"
- :term_vector "with_positions_offsets"
- :analyzer "pinyin_analyzer"
- :boost 10}}
- :primitive {:type "string"
- :store "yes"
- :analyzer "keyword"}})
- (def user-mapping {"user" {:_all ik-type
- :properties {:_id id-type
- :id {:type "string"}
- :identity {:type "string"}
- :name (pinyin-type :name)
- :avatar {:type "string"}
- :type {:type "string"}
- :location (pinyin-type :location)}}})
- (def work-mapping {"work" {:_all ik-type
- :properties {:_id id-type
- :id {:type "string"}
- :title (pinyin-type :title)
- :style (pinyin-type :style)
- :tags (pinyin-type :tags)
- :source {:type "string"}}}})
- (defn string-query
- [m opts]
- (let [[k v] (first m)]
- (merge
- {:query
- {:query_string
- {:query v
- :default_field (name k)}}}
- opts)))
- (defn ws
- [type query]
- (ed/search conn DB type query))
- (defmulti query-by-mode (fn [mode q] mode))
- (defmethod query-by-mode "user" [mode q]
- {:bool
- {:should
- [{:query_string
- {:query q
- :default_field "name"}}
- {:prefix {:identity q}}]}})
- (defmethod query-by-mode "work" [mode q]
- {:query_string {:query q}})
- (defn scroll
- [{:keys [q mode scroll-id size] :or {scroll-id "" size 10}}]
- (let [results (if (not= "" scroll-id)
- (ed/scroll conn scroll-id :scroll "1m")
- (ws mode {:query (query-by-mode mode q)
- :search_type "query_then_fetch"
- :scroll "1m"
- :size size}))]
- (if (seq results)
- {:scroll-id (:_scroll_id results)
- :hits (map :_source (get-in results [:hits :hits]))})))
- (defn transact!
- [{:keys [op type id] :as m}]
- (let [op (keyword op)]
- (cond
- (and (= op :put) (= type "user"))
- (let [u (-> id
- user/get
- (select-keys [:id :identity :name :avatar :type :location]))]
- (ed/delete-by-query conn DB "user" {:term {:id id}})
- (ed/put conn DB "user" (:identity u) u))
- (and (= op :delete) (= type "user"))
- (ed/delete conn DB "user" (:identity (user/get id)))
- (and (= op :put) (= type "work"))
- (let [w (-> id
- work/get
- (->
- (select-keys [:id :title :style :tags :source])))]
- (ed/put conn DB "work" id w))
- (and (= op :delete) (= type "work"))
- (ed/delete conn DB "work" id)
- :else
- ;; log
- (info "Es-index wrong: " m))))
- (defn search-init
- []
- (let [c (atom 0)]
- (ei/delete conn DB)
- (ei/create conn DB :mappings (merge user-mapping work-mapping))
- (let [users (clojure.java.jdbc/query (:db env) ["select id, identity, name, avatar, type, location from users"])]
- (doseq [user users]
- (prn @c)
- (ed/put conn DB "user" (:identity user) user)
- (swap! c inc)))
- (let [works (clojure.java.jdbc/query (:db env) ["select id, title, style, tags, source from works"])]
- (prn "works beginning")
- (reset! c 0)
- (doseq [work works]
- (prn @c)
- (ed/put conn DB "work" (str (:id work)) work)
- (swap! c inc)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement