use of org.apache.lucene.index.RandomIndexWriter in project lucene-solr by apache.
the class TestSimilarity2 method testOmitTF.
/** make sure all sims work if TF is omitted */
public void testOmitTF() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
Document doc = new Document();
FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
ft.setIndexOptions(IndexOptions.DOCS);
ft.freeze();
Field f = newField("foo", "bar", ft);
doc.add(f);
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher is = newSearcher(ir);
for (Similarity sim : sims) {
is.setSimilarity(sim);
BooleanQuery.Builder query = new BooleanQuery.Builder();
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
assertEquals(1, is.search(query.build(), 10).totalHits);
}
ir.close();
dir.close();
}
use of org.apache.lucene.index.RandomIndexWriter in project lucene-solr by apache.
the class TestSimilarity2 method testNoNorms.
/** make sure we can retrieve when norms are disabled */
public void testNoNorms() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
Document doc = new Document();
FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
ft.setOmitNorms(true);
ft.freeze();
doc.add(newField("foo", "bar", ft));
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher is = newSearcher(ir);
for (Similarity sim : sims) {
is.setSimilarity(sim);
BooleanQuery.Builder query = new BooleanQuery.Builder();
query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
assertEquals(1, is.search(query.build(), 10).totalHits);
}
ir.close();
dir.close();
}
use of org.apache.lucene.index.RandomIndexWriter in project lucene-solr by apache.
the class TestNearSpansOrdered method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(newTextField(FIELD, docFields[i], Field.Store.NO));
writer.addDocument(doc);
}
writer.forceMerge(1);
reader = writer.getReader();
writer.close();
searcher = newSearcher(getOnlyLeafReader(reader));
}
use of org.apache.lucene.index.RandomIndexWriter in project lucene-solr by apache.
the class TestSpanContainQuery method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(newTextField(field, docFields[i], Field.Store.YES));
writer.addDocument(doc);
}
writer.forceMerge(1);
reader = writer.getReader();
writer.close();
searcher = newSearcher(getOnlyLeafReader(reader));
}
use of org.apache.lucene.index.RandomIndexWriter in project lucene-solr by apache.
the class TestSpanNearQuery method testNoPositions.
public void testNoPositions() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new StringField("foo", "bar", Field.Store.NO));
iw.addDocument(doc);
IndexReader ir = iw.getReader();
iw.close();
IndexSearcher is = new IndexSearcher(ir);
SpanTermQuery query = new SpanTermQuery(new Term("foo", "bar"));
SpanTermQuery query2 = new SpanTermQuery(new Term("foo", "baz"));
IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
is.search(new SpanNearQuery(new SpanQuery[] { query, query2 }, 10, true), 5);
});
assertTrue(expected.getMessage().contains("was indexed without position data"));
ir.close();
dir.close();
}
Aggregations