Last active
August 29, 2015 14:00
-
-
Save knutwalker/11489359 to your computer and use it in GitHub Desktop.
answer for http://stackoverflow.com/questions/23430775/control-the-disk-location-of-documents-in-lucene
This file contains 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
package org.example; | |
import org.apache.lucene.document.Document; | |
import org.apache.lucene.document.Field; | |
import org.apache.lucene.document.LongField; | |
import org.apache.lucene.index.DirectoryReader; | |
import org.apache.lucene.index.IndexReader; | |
import org.apache.lucene.index.IndexWriter; | |
import org.apache.lucene.index.IndexWriterConfig; | |
import org.apache.lucene.index.SlowCompositeReaderWrapper; | |
import org.apache.lucene.index.sorter.SortingAtomicReader; | |
import org.apache.lucene.index.sorter.SortingMergePolicy; | |
import org.apache.lucene.search.Sort; | |
import org.apache.lucene.search.SortField; | |
import org.apache.lucene.store.Directory; | |
import org.apache.lucene.store.RAMDirectory; | |
import org.apache.lucene.util.Version; | |
import java.io.IOException; | |
public class SO23430775 { | |
/** your id field */ | |
public static String ID = "id"; | |
/** a Sort for your id field */ | |
public static Sort ID_SORT = new Sort(new SortField(ID, SortField.Type.LONG, false)); | |
/** create regular document */ | |
public static Document doc(long id) { | |
Document d = new Document(); | |
d.add(new LongField(ID, id, Field.Store.YES)); | |
return d; | |
} | |
/** add 4 documents in unsorted order. just regular indexing, knows practically nothing about sorting */ | |
public static Directory addDocuments(Directory dir, IndexWriterConfig iwc) throws IOException { | |
IndexWriter iw = new IndexWriter(dir, iwc); | |
iw.addDocument(doc(4)); | |
iw.addDocument(doc(2)); | |
iw.addDocument(doc(3)); | |
iw.addDocument(doc(1)); | |
iw.commit(); | |
// force merge to invoke a possible SortingMergePolicy | |
iw.forceMerge(1, true); | |
iw.close(); | |
return dir; | |
} | |
/** create a straight-forward Directory, unsorted order */ | |
public static Directory createUnsorted() throws IOException { | |
Directory dir = new RAMDirectory(); | |
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, null); | |
return addDocuments(dir, iwc); | |
} | |
/** create a sorting index (online sorting) */ | |
public static Directory createSorting() throws IOException { | |
Directory dir = new RAMDirectory(); | |
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, null); | |
// enable online sorting on a long field 'id' | |
SortingMergePolicy sortingMP = new SortingMergePolicy(iwc.getMergePolicy(), ID_SORT); | |
iwc.setMergePolicy(sortingMP); | |
return addDocuments(dir, iwc); | |
} | |
/** create a sorted index (offline sorting) */ | |
public static Directory createSorted() throws IOException { | |
final Directory unsorted = createUnsorted(); | |
final IndexReader sortingReader = openSortingReader(unsorted); | |
Directory sorted = new RAMDirectory(); | |
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_48, null); | |
IndexWriter iw = new IndexWriter(sorted, iwc); | |
iw.addIndexes(sortingReader); | |
iw.close(); | |
sortingReader.close(); | |
return sorted; | |
} | |
/** open a regular reader in a directory */ | |
public static IndexReader openReader(Directory dir) throws IOException { | |
return DirectoryReader.open(dir); | |
} | |
/** open a sorted (and slow) view of an unsorted directory (live sorting) */ | |
public static IndexReader openSortingReader(Directory dir) throws IOException { | |
final IndexReader originalReader = openReader(dir); | |
return SortingAtomicReader.wrap( | |
SlowCompositeReaderWrapper.wrap(originalReader), ID_SORT); | |
} | |
/** just iterating in IndexReader order, knows nothing about sorting */ | |
public static void iterDocs(IndexReader reader) throws IOException { | |
for (int i = 0; i < reader.maxDoc(); i++) { | |
Document document = reader.document(i); | |
long id = document.getField(ID).numericValue().longValue(); | |
System.out.println("docId = " + i + " " + "ID = " + id); | |
} | |
reader.close(); | |
} | |
public static void main(String[] args) throws IOException { | |
System.out.println("unsorted:"); | |
iterDocs(openReader(createUnsorted())); | |
System.out.println(); | |
System.out.println("online sorting:"); | |
iterDocs(openReader(createSorting())); | |
System.out.println(); | |
System.out.println("live sorting:"); | |
iterDocs(openSortingReader(createUnsorted())); | |
System.out.println(); | |
System.out.println("offline sorting:"); | |
iterDocs(openReader(createSorted())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment