Skip to content

Instantly share code, notes, and snippets.

@lbjay
Created November 18, 2011 14:38
Show Gist options
  • Save lbjay/1376620 to your computer and use it in GitHub Desktop.
Save lbjay/1376620 to your computer and use it in GitHub Desktop.
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