Last active
April 21, 2017 21:00
-
-
Save marktriggs/e61b8e2f0ae3c61461bc83bc4a1a7662 to your computer and use it in GitHub Desktop.
VuFind browse changes for Solr 6
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
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