use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.
the class TypeFieldTypeTests method testTermsQuery.
public void testTermsQuery() throws Exception {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
IndexReader reader = openReaderWithNewType("my_type", w);
TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType();
ft.setName(TypeFieldMapper.NAME);
Query query = ft.termQuery("my_type", null);
assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
// Make sure that Lucene actually simplifies the query when there is a single type
Query userQuery = new PhraseQuery("body", "quick", "fox");
Query filteredQuery = new BooleanQuery.Builder().add(userQuery, Occur.MUST).add(query, Occur.FILTER).build();
Query rewritten = new IndexSearcher(reader).rewrite(filteredQuery);
assertEquals(userQuery, rewritten);
// ... and does not rewrite it if there is more than one type
reader.close();
reader = openReaderWithNewType("my_type2", w);
Query expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.NAME, "my_type")), Occur.SHOULD).build());
assertEquals(expected, query.rewrite(reader));
BytesRef[] types = new BytesRef[] { new BytesRef("my_type"), new BytesRef("my_type2"), new BytesRef("my_type3") };
// the query should match all documents
query = new TypeFieldMapper.TypesQuery(types);
assertEquals(new MatchAllDocsQuery(), query.rewrite(reader));
reader.close();
reader = openReaderWithNewType("unknown_type", w);
// the query cannot rewrite to a match all docs sinc unknown_type is not queried.
query = new TypeFieldMapper.TypesQuery(types);
expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[0])), Occur.SHOULD).add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, types[1])), Occur.SHOULD).build());
rewritten = query.rewrite(reader);
assertEquals(expected, rewritten);
// make sure that redundant types does not rewrite to MatchAllDocsQuery
query = new TypeFieldMapper.TypesQuery(new BytesRef("my_type"), new BytesRef("my_type"), new BytesRef("my_type"));
expected = new ConstantScoreQuery(new BooleanQuery.Builder().add(new TermQuery(new Term(TypeFieldMapper.CONTENT_TYPE, "my_type")), Occur.SHOULD).build());
rewritten = query.rewrite(reader);
assertEquals(expected, rewritten);
IOUtils.close(reader, w, dir);
}
use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.
the class RangeQueryRewriteTests method testRewriteMissingField.
public void testRewriteMissingField() throws Exception {
IndexService indexService = createIndex("test");
IndexReader reader = new MultiReader();
QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), null, null, indexService.mapperService(), null, null, xContentRegistry(), null, reader, null);
RangeQueryBuilder range = new RangeQueryBuilder("foo");
assertEquals(Relation.DISJOINT, range.getRelation(context));
}
use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.
the class IndexShardTests method testAcquireIndexCommit.
public void testAcquireIndexCommit() throws IOException {
final IndexShard shard = newStartedShard();
int numDocs = randomInt(20);
for (int i = 0; i < numDocs; i++) {
indexDoc(shard, "type", "id_" + i);
}
final boolean flushFirst = randomBoolean();
IndexCommit commit = shard.acquireIndexCommit(flushFirst);
int moreDocs = randomInt(20);
for (int i = 0; i < moreDocs; i++) {
indexDoc(shard, "type", "id_" + numDocs + i);
}
flushShard(shard);
// check that we can still read the commit that we captured
try (IndexReader reader = DirectoryReader.open(commit)) {
assertThat(reader.numDocs(), equalTo(flushFirst ? numDocs : 0));
}
shard.releaseIndexCommit(commit);
flushShard(shard, true);
// check it's clean up
assertThat(DirectoryReader.listCommits(shard.store().directory()), hasSize(1));
closeShards(shard);
}
use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.
the class MultiPhrasePrefixQueryTests method testSimple.
public void testSimple() throws Exception {
IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
Document doc = new Document();
doc.add(new Field("field", "aaa bbb ccc ddd", TextField.TYPE_NOT_STORED));
writer.addDocument(doc);
IndexReader reader = DirectoryReader.open(writer);
IndexSearcher searcher = new IndexSearcher(reader);
MultiPhrasePrefixQuery query = new MultiPhrasePrefixQuery();
query.add(new Term("field", "aa"));
assertThat(searcher.count(query), equalTo(1));
query = new MultiPhrasePrefixQuery();
query.add(new Term("field", "aaa"));
query.add(new Term("field", "bb"));
assertThat(searcher.count(query), equalTo(1));
query = new MultiPhrasePrefixQuery();
query.setSlop(1);
query.add(new Term("field", "aaa"));
query.add(new Term("field", "cc"));
assertThat(searcher.count(query), equalTo(1));
query = new MultiPhrasePrefixQuery();
query.setSlop(1);
query.add(new Term("field", "xxx"));
assertThat(searcher.count(query), equalTo(0));
}
use of org.apache.lucene.index.IndexReader in project elasticsearch by elastic.
the class MoreLikeThisQueryTests method testSimple.
public void testSimple() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
indexWriter.commit();
Document document = new Document();
document.add(new TextField("_id", "1", Field.Store.YES));
document.add(new TextField("text", "lucene", Field.Store.YES));
indexWriter.addDocument(document);
document = new Document();
document.add(new TextField("_id", "2", Field.Store.YES));
document.add(new TextField("text", "lucene release", Field.Store.YES));
indexWriter.addDocument(document);
IndexReader reader = DirectoryReader.open(indexWriter);
IndexSearcher searcher = new IndexSearcher(reader);
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery("lucene", new String[] { "text" }, Lucene.STANDARD_ANALYZER);
mltQuery.setLikeText("lucene");
mltQuery.setMinTermFrequency(1);
mltQuery.setMinDocFreq(1);
long count = searcher.count(mltQuery);
assertThat(count, equalTo(2L));
reader.close();
indexWriter.close();
}
Aggregations