Search in sources :

Example 16 with IndexReader

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 17 with IndexReader

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));
}
Also used : IndexService(org.elasticsearch.index.IndexService) MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader)

Example 18 with IndexReader

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);
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) IndexCommit(org.apache.lucene.index.IndexCommit)

Example 19 with IndexReader

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));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 20 with IndexReader

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)962 Document (org.apache.lucene.document.Document)610 Directory (org.apache.lucene.store.Directory)603 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)549 IndexSearcher (org.apache.lucene.search.IndexSearcher)410 Term (org.apache.lucene.index.Term)332 TopDocs (org.apache.lucene.search.TopDocs)204 TermQuery (org.apache.lucene.search.TermQuery)160 Query (org.apache.lucene.search.Query)158 IndexWriter (org.apache.lucene.index.IndexWriter)150 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)142 Field (org.apache.lucene.document.Field)135 BytesRef (org.apache.lucene.util.BytesRef)134 IOException (java.io.IOException)133 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 ArrayList (java.util.ArrayList)108 TextField (org.apache.lucene.document.TextField)81 Test (org.junit.Test)81