Search in sources :

Example 86 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project lucene-solr by apache.

the class TestTryDelete method createIndex.

private static Directory createIndex() throws IOException {
    Directory directory = new RAMDirectory();
    IndexWriter writer = getWriter(directory);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("foo", String.valueOf(i), Store.YES));
        writer.addDocument(doc);
    }
    writer.commit();
    writer.close();
    return directory;
}
Also used : StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 87 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project lucene-solr by apache.

the class TestSloppyPhraseQuery method checkPhraseQuery.

private float checkPhraseQuery(Document doc, PhraseQuery query, int slop, int expectedNumResults) throws Exception {
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    Term[] terms = query.getTerms();
    int[] positions = query.getPositions();
    for (int i = 0; i < terms.length; ++i) {
        builder.add(terms[i], positions[i]);
    }
    builder.setSlop(slop);
    query = builder.build();
    MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random(), new RAMDirectory());
    RandomIndexWriter writer = new RandomIndexWriter(random(), ramDir, new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
    writer.addDocument(doc);
    IndexReader reader = writer.getReader();
    IndexSearcher searcher = newSearcher(reader);
    MaxFreqCollector c = new MaxFreqCollector();
    searcher.search(query, c);
    assertEquals("slop: " + slop + "  query: " + query + "  doc: " + doc + "  Wrong number of hits", expectedNumResults, c.totalHits);
    //QueryUtils.check(query,searcher);
    writer.close();
    reader.close();
    ramDir.close();
    // with these different tokens/distributions/lengths.. otherwise this test is very fragile.
    return c.max;
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Term(org.apache.lucene.index.Term) RAMDirectory(org.apache.lucene.store.RAMDirectory) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 88 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project lucene-solr by apache.

the class SweetSpotSimilarityTest method computeNorm.

private static float computeNorm(Similarity sim, String field, int length) throws IOException {
    String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" "));
    Directory dir = new RAMDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));
    w.addDocument(Collections.singleton(newTextField(field, value, Store.NO)));
    DirectoryReader reader = DirectoryReader.open(w);
    w.close();
    IndexSearcher searcher = new IndexSearcher(reader);
    searcher.setSimilarity(sim);
    Explanation expl = searcher.explain(new TermQuery(new Term(field, "a")), 0);
    reader.close();
    dir.close();
    Explanation norm = findExplanation(expl, "fieldNorm");
    assertNotNull(norm);
    return norm.getValue();
}
Also used : IntStream(java.util.stream.IntStream) Explanation(org.apache.lucene.search.Explanation) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) RAMDirectory(org.apache.lucene.store.RAMDirectory) IOException(java.io.IOException) PerFieldSimilarityWrapper(org.apache.lucene.search.similarities.PerFieldSimilarityWrapper) Collectors(java.util.stream.Collectors) IndexWriter(org.apache.lucene.index.IndexWriter) TermQuery(org.apache.lucene.search.TermQuery) Similarity(org.apache.lucene.search.similarities.Similarity) Directory(org.apache.lucene.store.Directory) Store(org.apache.lucene.document.Field.Store) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) Collections(java.util.Collections) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 89 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project lucene-solr by apache.

the class BaseCompressingDocValuesFormatTestCase method testUniqueValuesCompression.

public void testUniqueValuesCompression() throws IOException {
    final Directory dir = new RAMDirectory();
    final IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    final IndexWriter iwriter = new IndexWriter(dir, iwc);
    final int uniqueValueCount = TestUtil.nextInt(random(), 1, 256);
    final List<Long> values = new ArrayList<>();
    final Document doc = new Document();
    final NumericDocValuesField dvf = new NumericDocValuesField("dv", 0);
    doc.add(dvf);
    for (int i = 0; i < 300; ++i) {
        final long value;
        if (values.size() < uniqueValueCount) {
            value = random().nextLong();
            values.add(value);
        } else {
            value = RandomPicks.randomFrom(random(), values);
        }
        dvf.setLongValue(value);
        iwriter.addDocument(doc);
    }
    iwriter.forceMerge(1);
    final long size1 = dirSize(dir);
    for (int i = 0; i < 20; ++i) {
        dvf.setLongValue(RandomPicks.randomFrom(random(), values));
        iwriter.addDocument(doc);
    }
    iwriter.forceMerge(1);
    final long size2 = dirSize(dir);
    // make sure the new longs did not cost 8 bytes each
    assertTrue(size2 < size1 + 8 * 20);
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory)

Example 90 with RAMDirectory

use of org.apache.lucene.store.RAMDirectory in project lucene-solr by apache.

the class SpatialExample method init.

protected void init() {
    //Typical geospatial context
    //  These can also be constructed from SpatialContextFactory
    this.ctx = SpatialContext.GEO;
    //results in sub-meter precision for geohash
    int maxLevels = 11;
    //TODO demo lookup by detail distance
    //  This can also be constructed from SpatialPrefixTreeFactory
    SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
    this.strategy = new RecursivePrefixTreeStrategy(grid, "myGeoField");
    this.directory = new RAMDirectory();
}
Also used : RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) Point(org.locationtech.spatial4j.shape.Point) RAMDirectory(org.apache.lucene.store.RAMDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

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