public virtual void TestGetFieldNames()
{
RAMDirectory d = new MockRAMDirectory();
// set up writer
IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
AddDocumentWithFields(writer);
writer.Close();
// set up reader
IndexReader reader = IndexReader.Open(d, true);
System.Collections.Generic.ICollection<string> fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "keyword"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "text"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unindexed"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unstored"));
reader.Close();
// add more documents
writer = new IndexWriter(d, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), false, IndexWriter.MaxFieldLength.LIMITED);
// want to get some more segments here
for (int i = 0; i < 5 * writer.MergeFactor; i++)
{
AddDocumentWithFields(writer);
}
// new fields are in some different segments (we hope)
for (int i = 0; i < 5 * writer.MergeFactor; i++)
{
AddDocumentWithDifferentFields(writer);
}
// new termvector fields
for (int i = 0; i < 5 * writer.MergeFactor; i++)
{
AddDocumentWithTermVectorFields(writer);
}
writer.Close();
// verify fields again
reader = IndexReader.Open(d, true);
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
Assert.AreEqual(13, fieldNames.Count); // the following fields
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "keyword"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "text"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unindexed"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unstored"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "keyword2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "text2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unindexed2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unstored2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvnot"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "termvector"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvposition"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvoffset"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvpositionoffset"));
// verify that only indexed fields were returned
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.INDEXED);
Assert.AreEqual(11, fieldNames.Count); // 6 original + the 5 termvector fields
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "keyword"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "text"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unstored"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "keyword2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "text2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unstored2"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvnot"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "termvector"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvposition"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvoffset"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvpositionoffset"));
// verify that only unindexed fields were returned
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.UNINDEXED);
Assert.AreEqual(2, fieldNames.Count); // the following fields
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unindexed"));
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "unindexed2"));
// verify index term vector fields
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.TERMVECTOR);
Assert.AreEqual(1, fieldNames.Count); // 1 field has term vector only
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "termvector"));
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.TERMVECTOR_WITH_POSITION);
Assert.AreEqual(1, fieldNames.Count); // 4 fields are indexed with term vectors
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvposition"));
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.TERMVECTOR_WITH_OFFSET);
Assert.AreEqual(1, fieldNames.Count); // 4 fields are indexed with term vectors
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvoffset"));
fieldNames = reader.GetFieldNames(IndexReader.FieldOption.TERMVECTOR_WITH_POSITION_OFFSET);
Assert.AreEqual(1, fieldNames.Count); // 4 fields are indexed with term vectors
Assert.IsTrue(CollectionsHelper.Contains(fieldNames, "tvpositionoffset"));
reader.Close();
d.Close();
}