Created
November 18, 2011 14:38
-
-
Save lbjay/1376620 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static void regexQuery(String pattern) throws Exception { | |
Directory dir = FSDirectory.open(new File(SOLR_HOME + "/fulltext-build/data/index/")); | |
IndexReader reader = IndexReader.open(dir, true); | |
IndexSearcher searcher = new IndexSearcher(reader); | |
Term t = new Term("body", pattern); | |
Query q = new RegexQuery(t); | |
System.out.println("query object: " + q); | |
System.out.println("stats: " + reader.numDocs()); | |
TopDocs docs = searcher.search(q, 10); | |
FastVectorHighlighter fvh = new FastVectorHighlighter(false, true); | |
System.out.println("total: " + docs.totalHits); | |
for (ScoreDoc match : docs.scoreDocs) { | |
Document doc = searcher.doc(match.doc); | |
// get the matched text snippet somehow | |
// this doesn't work because getBestFragment wants a FieldQuery, not a RegexQuery | |
// String frag = fvh.getBestFragment(q, reader, match.doc, 100); | |
System.out.println(doc.get("bibcode")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment