Created
August 7, 2015 14:14
-
-
Save agazzarini/5cd4cec02c168f4b9854 to your computer and use it in GitHub Desktop.
Querying a SolRDF index with Clojure and Lucene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clojene.core | |
(:import | |
(org.apache.lucene.store FSDirectory) | |
(org.apache.lucene.index IndexReader) | |
(org.apache.lucene.search IndexSearcher) | |
(org.apache.lucene.queryparser.classic.QueryParser) | |
(org.apache.lucene.analysis.core.SimpleAnalyzer) | |
(java.io File))) | |
(defn searcher [path] | |
(IndexSearcher. | |
(IndexReader/open | |
(FSDirectory/open (File. path))))) | |
(defn search [searcher q] | |
(let [ | |
parser (QueryParser. "o" (SimpleAnalyzer.)) | |
query (.parse parser q)] | |
(.search searcher query 10))) | |
(defn dump [results index] | |
(let [ | |
as-triple #(.doc searcher (.doc %)) | |
s #(.get % "s") | |
p #(.get % "p") | |
o #(.get % "o") | |
as-spo #(str (s %) " " (p %) " " (o %))] | |
(str | |
(reduce #(str %1 " . \n" %2 ) | |
(map as-spo | |
(map as-triple (.scoreDocs results)))) " . "))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment