Skip to content

Instantly share code, notes, and snippets.

@marktriggs
Last active April 21, 2017 21:00
Show Gist options
  • Save marktriggs/e61b8e2f0ae3c61461bc83bc4a1a7662 to your computer and use it in GitHub Desktop.
Save marktriggs/e61b8e2f0ae3c61461bc83bc4a1a7662 to your computer and use it in GitHub Desktop.
VuFind browse changes for Solr 6
import org.apache.lucene.store.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import java.io.*;
import java.util.*;
public class Leech
{
protected CompositeReader reader;
protected IndexSearcher searcher;
protected List<LeafReaderContext> leafReaders;
private String field;
TermsEnum tenum = null;
public Leech (String indexPath,
String field) throws Exception
{
// Solr6: Open our composite reader (a top-level DirectoryReader that
// contains one reader per segment in our index).
reader = DirectoryReader.open (FSDirectory.open (new File (indexPath).toPath ()));
// Solr6: Open the searcher that we'll use to verify that terms are
// being used by a non-deleted document.
searcher = new IndexSearcher (reader);
// Solr6: Extract the list of readers for our underlying segments.
// We'll work through these one at a time until we've consumed them all.
leafReaders = new ArrayList<>(reader.getContext().leaves());
this.field = field;
}
public byte[] buildSortKey (String heading)
{
try {
return heading.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public void dropOff () throws IOException
{
reader.close ();
}
private boolean termExists (String t)
{
try {
return (this.searcher.search (new ConstantScoreQuery(new TermQuery (new Term(this.field, t))),
1).totalHits > 0);
} catch (IOException e) {
return false;
}
}
public String next () throws Exception
{
if (tenum == null && leafReaders.isEmpty()) {
// Nothing left to do
return null;
}
if (tenum == null) {
// Solr6: Select our next LeafReader to work from
LeafReader ir = leafReaders.remove(0).reader();
Terms terms = ir.terms(this.field);
if (terms == null) {
// Solr6: Try the next reader
return next();
}
tenum = terms.iterator();
}
if (tenum.next() != null) {
String termText = tenum.term().utf8ToString();
if (termExists(termText)) {
return termText;
} else {
return this.next();
}
} else {
// Solr6: Exhausted this reader
tenum = null;
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment