Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. (ns api.db.search
  2. (:require [clojurewerkz.elastisch.rest :as es]
  3. [clojurewerkz.elastisch.rest.index :as ei]
  4. [clojurewerkz.elastisch.rest.document :as ed]
  5. [clojurewerkz.elastisch.query :as q]
  6. [environ-plus.core :refer [env]]
  7. [api.db.user :as user]
  8. [api.db.work :as work]
  9. [taoensso.timbre :refer [info]]))
  10.  
  11. (def conn (es/connect (:elastic env)))
  12.  
  13. (defonce DB "hoolay")
  14.  
  15. (def ik-type {:indexAnalyzer "ik"
  16. :searchAnalyzer "ik"
  17. :term_vector "no"
  18. :store "false"})
  19.  
  20. (def id-type {:type "string"
  21. :analyzer "keyword"
  22. :include_in_all true})
  23.  
  24. (defn pinyin-type
  25. [field]
  26. {:type "multi_field"
  27. :fields {field {:type "string"
  28. :store "no"
  29. :term_vector "with_positions_offsets"
  30. :analyzer "pinyin_analyzer"
  31. :boost 10}}
  32. :primitive {:type "string"
  33. :store "yes"
  34. :analyzer "keyword"}})
  35.  
  36. (def user-mapping {"user" {:_all ik-type
  37. :properties {:_id id-type
  38. :id {:type "string"}
  39. :identity {:type "string"}
  40. :name (pinyin-type :name)
  41. :avatar {:type "string"}
  42. :type {:type "string"}
  43. :location (pinyin-type :location)}}})
  44.  
  45. (def work-mapping {"work" {:_all ik-type
  46. :properties {:_id id-type
  47. :id {:type "string"}
  48. :title (pinyin-type :title)
  49. :style (pinyin-type :style)
  50. :tags (pinyin-type :tags)
  51. :source {:type "string"}}}})
  52.  
  53. (defn string-query
  54. [m opts]
  55. (let [[k v] (first m)]
  56. (merge
  57. {:query
  58. {:query_string
  59. {:query v
  60. :default_field (name k)}}}
  61. opts)))
  62.  
  63. (defn ws
  64. [type query]
  65. (ed/search conn DB type query))
  66.  
  67. (defmulti query-by-mode (fn [mode q] mode))
  68.  
  69. (defmethod query-by-mode "user" [mode q]
  70. {:bool
  71. {:should
  72. [{:query_string
  73. {:query q
  74. :default_field "name"}}
  75. {:prefix {:identity q}}]}})
  76.  
  77. (defmethod query-by-mode "work" [mode q]
  78. {:query_string {:query q}})
  79.  
  80. (defn scroll
  81. [{:keys [q mode scroll-id size] :or {scroll-id "" size 10}}]
  82. (let [results (if (not= "" scroll-id)
  83. (ed/scroll conn scroll-id :scroll "1m")
  84. (ws mode {:query (query-by-mode mode q)
  85. :search_type "query_then_fetch"
  86. :scroll "1m"
  87. :size size}))]
  88. (if (seq results)
  89. {:scroll-id (:_scroll_id results)
  90. :hits (map :_source (get-in results [:hits :hits]))})))
  91.  
  92. (defn transact!
  93. [{:keys [op type id] :as m}]
  94. (let [op (keyword op)]
  95. (cond
  96. (and (= op :put) (= type "user"))
  97. (let [u (-> id
  98. user/get
  99. (select-keys [:id :identity :name :avatar :type :location]))]
  100. (ed/delete-by-query conn DB "user" {:term {:id id}})
  101. (ed/put conn DB "user" (:identity u) u))
  102. (and (= op :delete) (= type "user"))
  103. (ed/delete conn DB "user" (:identity (user/get id)))
  104. (and (= op :put) (= type "work"))
  105. (let [w (-> id
  106. work/get
  107. (->
  108. (select-keys [:id :title :style :tags :source])))]
  109. (ed/put conn DB "work" id w))
  110. (and (= op :delete) (= type "work"))
  111. (ed/delete conn DB "work" id)
  112. :else
  113. ;; log
  114. (info "Es-index wrong: " m))))
  115.  
  116. (defn search-init
  117. []
  118. (let [c (atom 0)]
  119. (ei/delete conn DB)
  120. (ei/create conn DB :mappings (merge user-mapping work-mapping))
  121. (let [users (clojure.java.jdbc/query (:db env) ["select id, identity, name, avatar, type, location from users"])]
  122. (doseq [user users]
  123. (prn @c)
  124. (ed/put conn DB "user" (:identity user) user)
  125. (swap! c inc)))
  126.  
  127. (let [works (clojure.java.jdbc/query (:db env) ["select id, title, style, tags, source from works"])]
  128. (prn "works beginning")
  129. (reset! c 0)
  130. (doseq [work works]
  131. (prn @c)
  132. (ed/put conn DB "work" (str (:id work)) work)
  133. (swap! c inc)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement