Skip to content

Commit

Permalink
Add transducers for processing Lucene results
Browse files Browse the repository at this point in the history
  • Loading branch information
wardle committed Mar 16, 2023
1 parent fb1fcf7 commit 1800b8f
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/com/eldrix/hermes/impl/search.clj
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@
(.get doc "term")
(.get doc "preferred-term")))

(defn xf-doc-id->-result
"Returns a transducer that maps a Lucene document id into a search result."
[^IndexSearcher searcher]
(let [stored-fields (.storedFields searcher)]
(map (fn [^long doc-id] (doc->result (.document stored-fields doc-id))))))

(defn xf-doc-id->concept-id
"Returns a transducer that maps a Lucene document id into a concept identifier."
[^IndexSearcher searcher]
(let [stored-fields (.storedFields searcher)]
(map (fn [^long doc-id] (.numericValue (.getField (.document stored-fields doc-id #{"concept-id"}) "concept-id"))))))

(defn xf-scoredoc->concept-id
"Returns a transducer that maps a Lucene ScoreDoc to a concept identifier"
[^IndexSearcher searcher]
(let [stored-fields (.storedFields searcher)]
(map (fn [^ScoreDoc score-doc] (.numericValue (.getField (.document stored-fields (.-doc score-doc) #{"concept-id"}) "concept-id"))))))

(defn do-query-for-results
"Perform a search using query 'q' returning results as a sequence of Result
items."
Expand All @@ -294,15 +312,13 @@ items."
(defn do-query-for-concept-ids
"Perform the query, returning results as a set of concept identifiers"
([^IndexSearcher searcher ^Query query]
(let [stored-fields (.storedFields searcher)]
(into #{}
(map (fn [^long doc-id] (.numericValue (.getField (.document stored-fields doc-id #{"concept-id"}) "concept-id"))))
(lucene/search-all searcher query))))
(into #{}
(xf-doc-id->concept-id searcher)
(lucene/search-all searcher query)))
([^IndexSearcher searcher ^Query query max-hits]
(let [stored-fields (.storedFields searcher)]
(into #{}
(map (fn [^ScoreDoc score-doc] (.numericValue (.getField (.document stored-fields (.-doc score-doc) #{"concept-id"}) "concept-id"))))
(seq (.-scoreDocs (.search searcher query ^int max-hits)))))))
(into #{}
(xf-scoredoc->concept-id searcher)
(seq (.-scoreDocs (.search searcher query ^int max-hits))))))

(s/fdef do-search
:args (s/cat :searcher ::searcher :params ::search-params))
Expand Down

0 comments on commit 1800b8f

Please sign in to comment.