Skip to content

Commit

Permalink
Search endpoint now supports unlimited results, but constrain for web…
Browse files Browse the repository at this point in the history
… clients.
  • Loading branch information
wardle committed Mar 30, 2021
1 parent ec9d1ac commit 132a543
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
28 changes: 15 additions & 13 deletions src/com/eldrix/hermes/impl/search.clj
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,21 @@
[^IndexSearcher searcher ^ScoreDoc score-doc]
(doc-id->concept-id searcher (.-doc score-doc)))

(defn do-query-for-results
([^IndexSearcher searcher ^Query q]
(->> (search-all searcher q)
(map #(.doc searcher %))
(map doc->result)))
([^IndexSearcher searcher ^Query q max-hits]
(map (partial scoredoc->result searcher) (seq (.-scoreDocs (.search searcher q (int max-hits)))))))

(defn do-search
"Perform a search against the index.
Parameters:
- searcher : the IndexSearcher to use
- params : a map of search parameters, which are:
|- :s : search string to use
|- :max-hits : maximum hits (default, 200)
|- :max-hits : maximum hits (if omitted returns unlimited results)
|- :fuzzy : fuzziness (0-2, default 0)
|- :fallback-fuzzy : if no results, try again with fuzzy search?
|- :query : additional ^Query to apply
Expand All @@ -302,12 +310,14 @@
(do-search searcher {:s \"neurologist\" :properties {snomed/IsA [14679004]}})
A FSN is a fully-specified name and should generally be left out of search."
[^IndexSearcher searcher params]
[^IndexSearcher searcher {:keys [max-hits] :as params}]
(let [q1 (make-search-query params)
q2 (if-let [q (:query params)] (q-and [q1 q]) q1)
hits (seq (.-scoreDocs ^TopDocs (.search searcher ^Query q2 (int (or (:max-hits params) 200)))))]
(if hits
(map (partial scoredoc->result searcher) hits)
results (if max-hits
(do-query-for-results searcher q2 (int max-hits))
(do-query-for-results searcher q2))]
(if results
results
(let [fuzzy (or (:fuzzy params) 0)
fallback (or (:fallback-fuzzy params) 0)]
(when (and (= fuzzy 0) (> fallback 0))
Expand All @@ -328,14 +338,6 @@
(let [topdocs ^TopDocs (.search searcher query ^int max-hits)]
(topdocs->concept-ids searcher topdocs))))

(defn do-query-for-results
([^IndexSearcher searcher ^Query q]
(->> (search-all searcher q)
(map #(.doc searcher %))
(map doc->result)))
([^IndexSearcher searcher ^Query q max-hits]
(map (partial scoredoc->result searcher) (seq (.-scoreDocs (.search searcher q (int max-hits)))))))

(defn q-self
"Returns a query that will only return documents for the concept specified."
[concept-id]
Expand Down
10 changes: 6 additions & 4 deletions src/com/eldrix/hermes/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,19 @@
{:name ::get-search
:enter (fn [context]
(let [params (parse-search-params context)
svc (get-in context [:request ::service])]
(when (= (:max-hits params) 0) (throw (IllegalArgumentException. "invalid parameter: 0 maxHits")))
(assoc context :result (svc/search svc params))))})
svc (get-in context [:request ::service])
max-hits (or (:max-hits params) 200)]
(if (< 0 max-hits 10000)
(assoc context :result (svc/search svc (assoc params :max-hits max-hits)))
(throw (IllegalArgumentException. "invalid parameter: maxHits")))))})

(def common-routes [coerce-body content-neg-intc entity-render])
(def routes
(route/expand-routes
#{["/v1/snomed/concepts/:concept-id" :get (conj common-routes get-concept)]
["/v1/snomed/concepts/:concept-id/descriptions" :get (conj common-routes get-concept-descriptions)]
["/v1/snomed/concepts/:concept-id/preferred" :get (conj common-routes get-concept-preferred-description)]
["/v1/snomed/concepts/:concept-id/extended" :get (conj common-routes get-extended-concept)]
["/v1/snomed/concepts/:concept-id/extended" :get (conj common-routes get-extended-concept)]
["/v1/snomed/concepts/:concept-id/map/:refset-id" :get (conj common-routes get-map-to)]
["/v1/snomed/concepts/:concept-id/subsumed-by/:subsumer-id" :get (conj common-routes subsumed-by?)]
["/v1/snomed/crossmap/:refset-id/:code" :get (conj common-routes get-map-from)]
Expand Down

0 comments on commit 132a543

Please sign in to comment.