Permalink
Browse files

Add explain-fs.

  • Loading branch information...
1 parent a2ca314 commit 8bdcb2e23da715b7a8a59c30e9a49a41e879705d @drewr committed with Dec 1, 2011
Showing with 29 additions and 8 deletions.
  1. +29 −8 src/esperanto/lucene.clj
View
@@ -1,11 +1,16 @@
(ns esperanto.lucene
+ (:use [clojure.java.io :only [file]])
(:import (java.io StringReader)
+ (org.apache.lucene.analysis SimpleAnalyzer)
(org.apache.lucene.analysis.standard StandardAnalyzer)
(org.apache.lucene.analysis.tokenattributes TermAttribute)
(org.apache.lucene.document Document Field
Field$Store Field$Index)
(org.apache.lucene.index IndexWriter IndexWriter$MaxFieldLength
Term)
+ (org.apache.lucene.queryParser QueryParser)
+ (org.apache.lucene.search IndexSearcher)
+ (org.apache.lucene.store FSDirectory)
(org.apache.lucene.util Version)))
(defn index [writer fields]
@@ -26,13 +31,29 @@
([rdr]
(token-seq (StandardAnalyzer. Version/LUCENE_CURRENT) rdr))
([analyzer rdr]
- (let [stream (.tokenStream analyzer "field" rdr)
- term (.addAttribute stream TermAttribute)
- step (fn step [st v]
- (lazy-seq
- (when (.incrementToken st)
- (cons (.term term) (step st v)))))]
- (step stream []))))
-
+ (let [stream (.tokenStream analyzer "field" rdr)
+ term (.addAttribute stream TermAttribute)
+ step (fn step [st v]
+ (lazy-seq
+ (when (.incrementToken st)
+ (cons (.term term) (step st v)))))]
+ (step stream []))))
+(defn explain [dir query analyzer version]
+ (let [parser (QueryParser. version "text" analyzer)
+ query (.parse parser query)
+ _ (println query)
+ searcher (IndexSearcher. dir)
+ docs (.search searcher query 10)]
+ (doseq [match (.scoreDocs docs)]
+ (let [doc (.doc searcher (.doc match))]
+ (println (map #(let [b (.getBinaryValue %)]
+ (if (pos? (count b)) (String. b)))
+ (.getFields doc)))
+ (-> (.explain searcher query (.doc match)) str println)))
+ (.close searcher)))
+(defn explain-fs [dir query]
+ (let [dir (-> (file dir) FSDirectory/open)]
+ (explain dir query (SimpleAnalyzer.) Version/LUCENE_CURRENT)
+ (.close dir)))

0 comments on commit 8bdcb2e

Please sign in to comment.