use of org.apache.lucene.document.FieldType in project elasticsearch by elastic.
the class SimpleAllTests method testNoTokens.
public void testNoTokens() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.KEYWORD_ANALYZER));
FieldType allFt = getAllFieldType();
Document doc = new Document();
doc.add(new Field("_id", "1", StoredField.TYPE));
doc.add(new AllField("_all", "", 2.0f, allFt));
indexWriter.addDocument(doc);
IndexReader reader = DirectoryReader.open(indexWriter);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(new MatchAllDocsQuery(), 10);
assertThat(docs.totalHits, equalTo(1));
assertThat(docs.scoreDocs[0].doc, equalTo(0));
}
use of org.apache.lucene.document.FieldType in project elasticsearch by elastic.
the class SimpleAllTests method testSimpleAllNoBoost.
public void testSimpleAllNoBoost() throws Exception {
FieldType allFt = getAllFieldType();
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
Document doc = new Document();
doc.add(new Field("_id", "1", StoredField.TYPE));
doc.add(new AllField("_all", "something", 1.0f, allFt));
doc.add(new AllField("_all", "else", 1.0f, allFt));
indexWriter.addDocument(doc);
doc = new Document();
doc.add(new Field("_id", "2", StoredField.TYPE));
doc.add(new AllField("_all", "else", 1.0f, allFt));
doc.add(new AllField("_all", "something", 1.0f, allFt));
indexWriter.addDocument(doc);
IndexReader reader = DirectoryReader.open(indexWriter);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new AllTermQuery(new Term("_all", "else"));
TopDocs docs = searcher.search(query, 10);
assertThat(docs.totalHits, equalTo(2));
assertThat(docs.scoreDocs[0].doc, equalTo(0));
assertExplanationScore(searcher, query, docs.scoreDocs[0]);
assertThat(docs.scoreDocs[1].doc, equalTo(1));
assertExplanationScore(searcher, query, docs.scoreDocs[1]);
query = new AllTermQuery(new Term("_all", "something"));
docs = searcher.search(query, 10);
assertThat(docs.totalHits, equalTo(2));
assertThat(docs.scoreDocs[0].doc, equalTo(0));
assertExplanationScore(searcher, query, docs.scoreDocs[0]);
assertThat(docs.scoreDocs[1].doc, equalTo(1));
assertExplanationScore(searcher, query, docs.scoreDocs[1]);
indexWriter.close();
}
use of org.apache.lucene.document.FieldType in project languagetool by languagetool-org.
the class Indexer method add.
private void add(String sentence, String source, String title, int docCount) throws IOException {
Document doc = new Document();
FieldType type = new FieldType();
type.setStored(true);
type.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
type.setTokenized(true);
doc.add(new Field(FIELD_NAME, sentence, type));
doc.add(new Field(FIELD_NAME_LOWERCASE, sentence, type));
if (docCount != -1) {
FieldType countType = new FieldType();
countType.setStored(true);
countType.setIndexOptions(IndexOptions.NONE);
doc.add(new Field("docCount", String.valueOf(docCount), countType));
}
if (title != null) {
FieldType titleType = new FieldType();
titleType.setStored(true);
titleType.setIndexOptions(IndexOptions.NONE);
titleType.setTokenized(false);
doc.add(new Field(TITLE_FIELD_NAME, title, titleType));
}
if (source != null) {
FieldType sourceType = new FieldType();
sourceType.setStored(true);
sourceType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
sourceType.setTokenized(false);
doc.add(new Field(SOURCE_FIELD_NAME, source, sourceType));
}
writer.addDocument(doc);
}
use of org.apache.lucene.document.FieldType in project camel by apache.
the class LuceneIndexer method createFieldType.
private static FieldType createFieldType(boolean tokenized) {
FieldType answer = new FieldType();
//answer.setIndexed(true);
answer.setStored(true);
answer.setTokenized(tokenized);
// freeze the answer so that it becomes immutable
answer.freeze();
return answer;
}
use of org.apache.lucene.document.FieldType in project lucene-solr by apache.
the class TestPointVectorStrategy method testFieldOptions.
@Test
public void testFieldOptions() throws IOException, ParseException {
// It's not stored; test it isn't.
this.strategy = PointVectorStrategy.newInstance(ctx, getClass().getSimpleName());
adoc("99", "POINT(-5.0 8.2)");
commit();
SearchResults results = executeQuery(new MatchAllDocsQuery(), 1);
Document document = results.results.get(0).document;
assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X));
assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y));
deleteAll();
// Now we mark it stored. We also disable pointvalues...
FieldType fieldType = new FieldType(PointVectorStrategy.DEFAULT_FIELDTYPE);
fieldType.setStored(true);
//disable point values
fieldType.setDimensions(0, 0);
this.strategy = new PointVectorStrategy(ctx, getClass().getSimpleName(), fieldType);
adoc("99", "POINT(-5.0 8.2)");
commit();
results = executeQuery(new MatchAllDocsQuery(), 1);
document = results.results.get(0).document;
assertEquals("stored", -5.0, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X).numericValue());
assertEquals("stored", 8.2, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y).numericValue());
// Test a query fails without point values
expectThrows(UnsupportedOperationException.class, () -> {
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeRectangle(-10.0, 10.0, -5.0, 5.0));
this.strategy.makeQuery(args);
});
}
Aggregations