TestDocumentpublic class TestDocument extends TestCase Tests {@link Document} class. |
Methods Summary |
---|
private void | doAssert(org.apache.lucene.document.Document doc, boolean fromIndex)
String[] keywordFieldValues = doc.getValues("keyword");
String[] textFieldValues = doc.getValues("text");
String[] unindexedFieldValues = doc.getValues("unindexed");
String[] unstoredFieldValues = doc.getValues("unstored");
assertTrue(keywordFieldValues.length == 2);
assertTrue(textFieldValues.length == 2);
assertTrue(unindexedFieldValues.length == 2);
// this test cannot work for documents retrieved from the index
// since unstored fields will obviously not be returned
if (! fromIndex)
{
assertTrue(unstoredFieldValues.length == 2);
}
assertTrue(keywordFieldValues[0].equals("test1"));
assertTrue(keywordFieldValues[1].equals("test2"));
assertTrue(textFieldValues[0].equals("test1"));
assertTrue(textFieldValues[1].equals("test2"));
assertTrue(unindexedFieldValues[0].equals("test1"));
assertTrue(unindexedFieldValues[1].equals("test2"));
// this test cannot work for documents retrieved from the index
// since unstored fields will obviously not be returned
if (! fromIndex)
{
assertTrue(unstoredFieldValues[0].equals("test1"));
assertTrue(unstoredFieldValues[1].equals("test2"));
}
| private org.apache.lucene.document.Document | makeDocumentWithFields()
Document doc = new Document();
doc.add(Field.Keyword( "keyword", "test1"));
doc.add(Field.Keyword( "keyword", "test2"));
doc.add(Field.Text( "text", "test1"));
doc.add(Field.Text( "text", "test2"));
doc.add(Field.UnIndexed("unindexed", "test1"));
doc.add(Field.UnIndexed("unindexed", "test2"));
doc.add(Field.UnStored( "unstored", "test1"));
doc.add(Field.UnStored( "unstored", "test2"));
return doc;
| public void | testGetValuesForIndexedDocument()Tests {@link Document#getValues()} method for a Document retrieved from
an index.
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
writer.addDocument(makeDocumentWithFields());
writer.close();
Searcher searcher = new IndexSearcher(dir);
// search for something that does exists
Query query = new TermQuery(new Term("keyword", "test1"));
// ensure that queries return expected results without DateFilter first
Hits hits = searcher.search(query);
assertEquals(1, hits.length());
try
{
doAssert(hits.doc(0), true);
}
catch (Exception e)
{
e.printStackTrace(System.err);
System.err.print("\n");
}
finally
{
searcher.close();
}
| public void | testGetValuesForNewDocument()Tests {@link Document#getValues()} method for a brand new Document
that has not been indexed yet.
doAssert(makeDocumentWithFields(), false);
| public void | testRemoveForNewDocument()Tests {@link Document#remove()} method for a brand new Document
that has not been indexed yet.
Document doc = makeDocumentWithFields();
assertEquals(8, doc.fields.size());
doc.removeFields("keyword");
assertEquals(6, doc.fields.size());
doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
doc.removeFields("keyword"); // removing a field more than once
assertEquals(6, doc.fields.size());
doc.removeField("text");
assertEquals(5, doc.fields.size());
doc.removeField("text");
assertEquals(4, doc.fields.size());
doc.removeField("text");
assertEquals(4, doc.fields.size());
doc.removeField("doesnotexists"); // removing non-existing fields is siltenlty ignored
assertEquals(4, doc.fields.size());
doc.removeFields("unindexed");
assertEquals(2, doc.fields.size());
doc.removeFields("unstored");
assertEquals(0, doc.fields.size());
doc.removeFields("doesnotexists"); // removing non-existing fields is siltenlty ignored
assertEquals(0, doc.fields.size());
|
|