The last line in the test program below fails. I'm trying to store a keyword,
not tokenized, and get the same "type" of field back after query. But it
doesn't work, it comes back as "tokenized". Is this a known problem, or am I
missing something?
Thanks.
--
Phil
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
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.IndexableField;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.QueryBuilder;
import org.apache.lucene.util.Version;
import org.junit.Assert;
import org.junit.Test;
public class TestField {
@Test
public void testIt() throws Exception {
Document doc = new Document();
boolean useStringField = false;
IndexableField field;
if (useStringField) {
field = new StringField("foo", "bar", Store.YES);
}
else {
FieldType fieldType = new FieldType();
fieldType.setStored(true);
fieldType.setTokenized(false);
fieldType.setIndexed(true);
field = new Field("foo", "bar", fieldType);
}
doc.add(field);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
Directory directory = new RAMDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
analyzer);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter iwriter = new IndexWriter(directory, iwc);
iwriter.addDocument(doc);
iwriter.close();
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Query q = new QueryBuilder(analyzer).createPhraseQuery("foo", "bar");
TopScoreDocCollector collector = TopScoreDocCollector.create(5, true);
searcher.search(q, collector);
Assert.assertTrue(collector.getTotalHits() == 1);
doc = searcher.doc(collector.topDocs().scoreDocs[0].doc);
Assert.assertTrue(doc != null);
field = doc.getField("foo");
Assert.assertTrue(field != null);
if (useStringField) {
Assert.assertTrue(field instanceof StringField);
}
else {
Assert.assertTrue(field instanceof Field);
}
Assert.assertTrue(!field.fieldType().tokenized());
}
}