Search in sources :

Example 11 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory 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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) FieldType(org.apache.lucene.document.FieldType) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 12 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project elasticsearch by elastic.

the class InputStreamIndexInputTests method testSingleReadSingleByteLimit.

public void testSingleReadSingleByteLimit() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }
    output.close();
    IndexInput input = dir.openInput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(input.getFilePointer(), lessThan(input.length()));
        assertThat(is.actualSizeToRead(), equalTo(1L));
        assertThat(is.read(), equalTo(1));
        assertThat(is.read(), equalTo(-1));
    }
    for (int i = 0; i < 3; i++) {
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(input.getFilePointer(), lessThan(input.length()));
        assertThat(is.actualSizeToRead(), equalTo(1L));
        assertThat(is.read(), equalTo(2));
        assertThat(is.read(), equalTo(-1));
    }
    assertThat(input.getFilePointer(), equalTo(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
    assertThat(is.actualSizeToRead(), equalTo(0L));
    assertThat(is.read(), equalTo(-1));
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 13 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project elasticsearch by elastic.

the class InputStreamIndexInputTests method testReadMultiFourBytesLimit.

public void testReadMultiFourBytesLimit() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }
    output.close();
    IndexInput input = dir.openInput("test", IOContext.DEFAULT);
    byte[] read = new byte[4];
    assertThat(input.getFilePointer(), lessThan(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 4);
    assertThat(is.actualSizeToRead(), equalTo(4L));
    assertThat(is.read(read), equalTo(4));
    assertThat(read[0], equalTo((byte) 1));
    assertThat(read[1], equalTo((byte) 1));
    assertThat(read[2], equalTo((byte) 1));
    assertThat(read[3], equalTo((byte) 2));
    assertThat(input.getFilePointer(), lessThan(input.length()));
    is = new InputStreamIndexInput(input, 4);
    assertThat(is.actualSizeToRead(), equalTo(2L));
    assertThat(is.read(read), equalTo(2));
    assertThat(read[0], equalTo((byte) 2));
    assertThat(read[1], equalTo((byte) 2));
    assertThat(input.getFilePointer(), equalTo(input.length()));
    is = new InputStreamIndexInput(input, 4);
    assertThat(is.actualSizeToRead(), equalTo(0L));
    assertThat(is.read(read), equalTo(-1));
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 14 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project elasticsearch by elastic.

the class InputStreamIndexInputTests method testMarkRest.

public void testMarkRest() throws Exception {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }
    output.close();
    IndexInput input = dir.openInput("test", IOContext.DEFAULT);
    InputStreamIndexInput is = new InputStreamIndexInput(input, 4);
    assertThat(is.markSupported(), equalTo(true));
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(1));
    is.mark(0);
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(2));
    is.reset();
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(2));
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 15 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project elasticsearch by elastic.

the class SimpleLuceneTests method testNRTSearchOnClosedWriter.

public void testNRTSearchOnClosedWriter() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    DirectoryReader reader = DirectoryReader.open(indexWriter);
    for (int i = 0; i < 100; i++) {
        Document document = new Document();
        TextField field = new TextField("_id", Integer.toString(i), Field.Store.YES);
        field.setBoost(i);
        document.add(field);
        indexWriter.addDocument(document);
    }
    reader = refreshReader(reader);
    indexWriter.close();
    TermsEnum termDocs = SlowCompositeReaderWrapper.wrap(reader).terms("_id").iterator();
    termDocs.next();
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) 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) TermsEnum(org.apache.lucene.index.TermsEnum)

Aggregations

RAMDirectory (org.apache.lucene.store.RAMDirectory)183 Directory (org.apache.lucene.store.Directory)101 IndexWriter (org.apache.lucene.index.IndexWriter)82 Document (org.apache.lucene.document.Document)75 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)73 IndexSearcher (org.apache.lucene.search.IndexSearcher)43 IndexReader (org.apache.lucene.index.IndexReader)41 Test (org.junit.Test)35 TextField (org.apache.lucene.document.TextField)33 Field (org.apache.lucene.document.Field)29 Term (org.apache.lucene.index.Term)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)21 Before (org.junit.Before)21 IOException (java.io.IOException)19 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 Analyzer (org.apache.lucene.analysis.Analyzer)18 TopDocs (org.apache.lucene.search.TopDocs)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)15 FilterDirectory (org.apache.lucene.store.FilterDirectory)15 FieldType (org.apache.lucene.document.FieldType)13