Implemented the search, next stop: UI
This commit is contained in:
parent
24adc55a38
commit
f96cf486ab
3 changed files with 60 additions and 3 deletions
|
@ -10,8 +10,8 @@
|
|||
|
||||
(defentity entries
|
||||
(database db)
|
||||
(belongs-to competitions)
|
||||
(belongs-to users)
|
||||
(belongs-to competitions {:fk :competition_id})
|
||||
(belongs-to users {:fk :user_id})
|
||||
(has-many sources))
|
||||
|
||||
(defentity competitions
|
||||
|
|
|
@ -8,6 +8,12 @@
|
|||
(defn all []
|
||||
(select entries))
|
||||
|
||||
(defn by-id [entry-id]
|
||||
(first (select entries
|
||||
(with competitions)
|
||||
(with users)
|
||||
(where {:id entry-id}))))
|
||||
|
||||
(defn by-ld-uid [ld-uid]
|
||||
(first (select entries
|
||||
(where {:ld_uid ld-uid}))))
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
(ns luduverse.search
|
||||
(:require [clojurewerkz.elastisch.rest :as el]
|
||||
[clojurewerkz.elastisch.rest.index :as eli]
|
||||
[clojurewerkz.elastisch.rest.document :as eld]
|
||||
[clojurewerkz.elastisch.rest.response :as elresp]
|
||||
[clojurewerkz.elastisch.query :as elq]
|
||||
|
||||
[clojure.pprint :as pp]
|
||||
[luduverse.models.entry :as entry]))
|
||||
|
||||
(def connection (atom (el/connect "http://localhost:9200")))
|
||||
|
||||
(defn recreate-index []
|
||||
(eli/delete @connection "luduverse")
|
||||
(eli/create @connection "luduverse"
|
||||
:mappings {"entry" {:properties {:query {:type "string"}
|
||||
:ld_uid {:type "integer" :store "yes"}
|
||||
:id {:type "integer" :store "yes"}
|
||||
:title {:type "string" :store "yes"}
|
||||
:description {:type "string"}
|
||||
:type {:type "string" :store "yes"}
|
||||
:username {:type "string" :store "yes"}
|
||||
:theme {:type "string" :store "yes"}}}}))
|
||||
|
||||
(defn store [entry-id]
|
||||
(let [data (entry/by-id entry-id)
|
||||
query-string (str (:ld_uid data) " " (:title data) " " (:username data) " " (:type data) " " (:theme data) " " (:description data))]
|
||||
(eld/create @connection "luduverse" "entry" {:query query-string
|
||||
:ld_uid (:ld_uid data)
|
||||
:id (:id data)
|
||||
:title (:title data)
|
||||
:description (:description data)
|
||||
:type (:type data)
|
||||
:username (:username data)
|
||||
:theme (:theme data)})))
|
||||
|
||||
(defn store-all []
|
||||
; TODO: Delete all entries before
|
||||
(doseq [e (entry/all)]
|
||||
(store (:id e))))
|
||||
|
||||
(defn search [term]
|
||||
(let [res (eld/search @connection "luduverse" "entry"
|
||||
:query (elq/query-string :query term)
|
||||
:sort {:title "desc"}
|
||||
:from 0
|
||||
:size 50)
|
||||
number-of-hits (elresp/total-hits res)
|
||||
hits (elresp/hits-from res)]
|
||||
{:hit-count number-of-hits
|
||||
:results hits}))
|
Loading…
Reference in a new issue