forked from DmitryKey/luke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue DmitryKey#175: Offered improvement to reconstruction of unstore…
…d fields with no position information
- Loading branch information
Chris Bamford
committed
Nov 25, 2019
1 parent
13f7243
commit 75c0d1a
Showing
2 changed files
with
138 additions
and
28 deletions.
There are no files selected for viewing
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
102 changes: 102 additions & 0 deletions
102
src/test/java/org.apache.lucene.index/IndexTester2.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.apache.lucene.index; | ||
|
||
import junit.framework.TestCase; | ||
import org.apache.lucene.analysis.standard.UAX29URLEmailAnalyzer; | ||
import org.apache.lucene.document.*; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.store.NIOFSDirectory; | ||
import org.apache.lucene.util.Version; | ||
import org.getopt.luke.DocReconstructor; | ||
import org.getopt.luke.IndexInfo; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Created by cbamford on 19/11/2019. | ||
* Tests that unstored fields with no position info are reconstructed correctly. | ||
* For completeness it also checks the 3 other field types. | ||
*/ | ||
public class IndexTester2 extends TestCase { | ||
|
||
private String indexPath = "src/test/indices/lukeindex2"; | ||
private IndexWriterConfig indexCfg; | ||
private Directory directory; | ||
private DocReconstructor recon; | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
directory = NIOFSDirectory.open(new File(indexPath)); | ||
populate(); | ||
} | ||
|
||
@Override | ||
protected void tearDown() throws Exception { | ||
super.tearDown(); | ||
if (directory != null) directory.close(); | ||
} | ||
|
||
public void testDummy() { | ||
assertTrue(true == true); | ||
} | ||
|
||
public void testVerifyReconstructionOfMultipleFieldTypesAcrossMultipleDocs() throws Exception { | ||
|
||
// Check doc 1 | ||
DocReconstructor.Reconstructed reconstructed = recon.reconstruct(0); | ||
assertEquals("value1", (reconstructed.getStoredFields().get("stored"))[0].stringValue()); | ||
assertEquals("value1", reconstructed.getReconstructedFields().get("stored+tvs").get(0)); | ||
assertEquals("value1", reconstructed.getReconstructedFields().get("unstored-posns").get(0)); | ||
assertEquals("value1", reconstructed.getReconstructedFields().get("unstored+posns").get(0)); | ||
|
||
// Check doc 2 | ||
reconstructed = recon.reconstruct(1); | ||
assertEquals("value2", (reconstructed.getStoredFields().get("stored"))[0].stringValue()); | ||
assertEquals("value2", reconstructed.getReconstructedFields().get("stored+tvs").get(0)); | ||
assertEquals("value2", reconstructed.getReconstructedFields().get("unstored-posns").get(0)); | ||
assertEquals("value2", reconstructed.getReconstructedFields().get("unstored+posns").get(0)); | ||
} | ||
|
||
private void populate() throws Exception { | ||
// create an index | ||
indexCfg = new IndexWriterConfig(Version.LUCENE_4_10_3, new UAX29URLEmailAnalyzer()); | ||
indexCfg.setOpenMode(IndexWriterConfig.OpenMode.CREATE); | ||
|
||
IndexWriter writer = new IndexWriter(directory, indexCfg); | ||
FieldType tvFtype = createUnstoredWithTermVectorsFieldType(); | ||
|
||
Document doc = new Document(); | ||
doc.add(new TextField("stored", "value1", Field.Store.YES)); | ||
doc.add(new Field("stored+tvs", "value1", tvFtype)); | ||
doc.add(new TextField("unstored+posns", "value1", Field.Store.NO)); | ||
doc.add(new StringField("unstored-posns", "value1", Field.Store.NO)); | ||
writer.addDocument(doc); | ||
|
||
doc = new Document(); | ||
doc.add(new TextField("stored", "value2", Field.Store.YES)); | ||
doc.add(new Field("stored+tvs", "value2", tvFtype)); | ||
doc.add(new TextField("unstored+posns", "value2", Field.Store.NO)); | ||
doc.add(new StringField("unstored-posns", "value2", Field.Store.NO)); | ||
writer.addDocument(doc); | ||
|
||
writer.close(); | ||
|
||
IndexReader ir = DirectoryReader.open(directory); | ||
IndexInfo idxInfo = new IndexInfo(ir, indexPath); | ||
String[] idxFields = idxInfo.getFieldNames().toArray(new String[0]); | ||
|
||
recon = new DocReconstructor(ir, idxFields, idxInfo.getNumTerms()); | ||
} | ||
|
||
private FieldType createUnstoredWithTermVectorsFieldType() { | ||
FieldType fType = new FieldType(); | ||
fType.setStored(false); | ||
fType.setIndexed(true); | ||
fType.setTokenized(true); | ||
fType.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); | ||
fType.setStoreTermVectors(true); | ||
fType.setStoreTermVectorOffsets(true); | ||
fType.setStoreTermVectorPositions(true); | ||
return fType; | ||
} | ||
} |