Example usage for org.apache.lucene.store RAMDirectory RAMDirectory

List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory

Introduction

In this page you can find the example usage for org.apache.lucene.store RAMDirectory RAMDirectory.

Prototype

public RAMDirectory() 

Source Link

Document

Constructs an empty Directory .

Usage

From source file:com.google.gerrit.lucene.LuceneGroupIndex.java

License:Apache License

private static Directory dir(Schema<AccountGroup> schema, Config cfg, SitePaths sitePaths) throws IOException {
    if (LuceneIndexModule.isInMemoryTest(cfg)) {
        return new RAMDirectory();
    }/*from w  w w  .  java 2 s  . co  m*/
    Path indexDir = LuceneVersionManager.getDir(sitePaths, GROUPS + "_", schema);
    return FSDirectory.open(indexDir);
}

From source file:com.google.gerrit.server.change.ReviewerSuggestionCache.java

License:Apache License

private IndexSearcher index() throws IOException, OrmException {
    RAMDirectory idx = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer(CharArraySet.EMPTY_SET));
    config.setOpenMode(OpenMode.CREATE);

    try (IndexWriter writer = new IndexWriter(idx, config)) {
        for (Account a : db.get().accounts().all()) {
            if (a.isActive()) {
                addAccount(writer, a);/*from w  ww  . j av  a 2 s.c o  m*/
            }
        }
    }

    return new IndexSearcher(DirectoryReader.open(idx));
}

From source file:com.google.gerrit.server.documentation.QueryDocumentationExecutor.java

License:Apache License

protected Directory readIndexDirectory() throws IOException {
    Directory dir = new RAMDirectory();
    byte[] buffer = new byte[4096];
    InputStream index = getClass().getResourceAsStream(Constants.INDEX_ZIP);
    if (index == null) {
        log.warn("No index available");
        return null;
    }// w  ww  .java  2s. c o m

    try (ZipInputStream zip = new ZipInputStream(index)) {
        ZipEntry entry;
        while ((entry = zip.getNextEntry()) != null) {
            try (IndexOutput out = dir.createOutput(entry.getName(), null)) {
                int count;
                while ((count = zip.read(buffer)) != -1) {
                    out.writeBytes(buffer, count);
                }
            }
        }
    }
    // We must NOT call dir.close() here, as DirectoryReader.open() expects an opened directory.
    return dir;
}

From source file:com.greplin.interval.BaseIntervalQueryTest.java

License:Apache License

@Before
public void setUp() throws IOException {
    RAMDirectory ramDirectory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, new SimpleAnalyzer(Version.LUCENE_35));
    this.indexWriter = new IndexWriter(ramDirectory, config);
}

From source file:com.greplin.lucene.filter.PhraseFilterBenchmark.java

License:Apache License

public static void main(String[] argv) {
    Directory directory = new RAMDirectory();
    try {//from w  w w .  j  ava  2 s  . com
        IndexWriter writer = new IndexWriter(directory,
                new IndexWriterConfig(Version.LUCENE_32, new WhitespaceAnalyzer(Version.LUCENE_32)));
        int done = 0;
        for (int i = 0; i < NUMBER_OF_SEGMENTS; i++) {
            int remaining = NUMBER_OF_SEGMENTS - i;
            int numberOfDocs;
            if (remaining == 1) {
                numberOfDocs = TOTAL_DOCS - done;
            } else {
                numberOfDocs = RANDOM.nextInt(TOTAL_DOCS - done - remaining) + 1;
            }
            done += numberOfDocs;
            System.out.println("Segment #" + i + " has " + numberOfDocs + " docs");

            for (int d = 0; d < numberOfDocs; d++) {
                int wordCount = RANDOM.nextInt(WORDS_PER_DOC_DEVIATION * 2) + AVERAGE_WORDS_PER_DOC
                        - WORDS_PER_DOC_DEVIATION;
                Document doc = new Document();
                doc.add(new Field("f", Joiner.on(' ').join(words(wordCount)), Field.Store.YES,
                        Field.Index.ANALYZED));
                doc.add(new Field("second", RANDOM.nextInt(100) < SECOND_FIELD_MATCH_PERCENTAGE ? "yes" : "no",
                        Field.Store.NO, Field.Index.ANALYZED));
                writer.addDocument(doc);
            }
            writer.commit();
        }
        writer.close();

        IndexReader reader = IndexReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);

        String[][] queries = new String[TOTAL_QUERIES][];
        Term[][] terms = new Term[TOTAL_QUERIES][];

        for (int q = 0; q < TOTAL_QUERIES; q++) {
            queries[q] = words(WORDS_PER_QUERY[RANDOM.nextInt(WORDS_PER_QUERY.length)]);
            terms[q] = new Term[queries[q].length];
            for (int qw = 0; qw < queries[q].length; qw++) {
                terms[q][qw] = new Term(FIELD, queries[q][qw]);
            }
        }

        // Warm up.
        new PhraseFilter(FIELD, queries[0]).getDocIdSet(reader);

        for (int round = 0; round < ROUNDS; round++) {
            System.out.println();
            String name1 = "filter";
            String name2 = "query";

            long ms1 = 0, ms2 = 0;
            for (int step = 0; step < 2; step++) {
                System.gc();
                System.gc();
                System.gc();

                if (step == (round & 1)) {
                    long millis = System.currentTimeMillis();
                    long hits = 0;
                    for (String[] queryWords : queries) {
                        PhraseFilter pf = new PhraseFilter(
                                new FilterIntersectionProvider(TermsFilter.from(new Term("second", "yes"))),
                                FIELD, queryWords);
                        hits += searcher.search(new FilteredQuery(new MatchAllDocsQuery(), pf), 1).totalHits;
                    }
                    ms1 = System.currentTimeMillis() - millis;
                    System.out.println("Finished " + name1 + " in " + ms1 + "ms with " + hits + " hits");
                } else {
                    long millis = System.currentTimeMillis();
                    long hits = 0;
                    for (Term[] queryTerms : terms) {
                        PhraseQuery pq = new PhraseQuery();
                        for (Term term : queryTerms) {
                            pq.add(term);
                        }
                        Query query = BooleanQueryBuilder.builder()
                                .must(new TermQuery(new Term("second", "yes"))).must(pq).build();
                        hits += searcher.search(query, 1).totalHits;
                    }
                    ms2 = System.currentTimeMillis() - millis;
                    System.out.println("Finished " + name2 + " in " + ms2 + "ms with " + hits + " hits");
                }
            }
            System.out.println(name1 + " took " + (int) ((100.0 * ms1) / ms2) + "% as much time as " + name2);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.greplin.lucene.query.PredicateBonusQueryTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    this.directory = new RAMDirectory();
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.LuceneIndexer.java

License:Open Source License

@Override
public void initialize(Properties properties) throws IngestionException {
    String resDirPath = properties.getProperty(ConfigurationConstants.INGESTION_BASE_DIR) + File.separator;
    indexStatPath = resDirPath + properties.getProperty(ConfigurationConstants.INDEX_STAT_PATH);

    if (properties.getProperty(ConfigurationConstants.INDEX_DIR_TYPE).toUpperCase()
            .equals(ConfigurationConstants.IndexDirTypes.RAM.toString()))
        indexDir = new RAMDirectory();
    else if (properties.getProperty(ConfigurationConstants.INDEX_DIR_TYPE).toUpperCase()
            .equals(ConfigurationConstants.IndexDirTypes.FS.toString())) {
        try {/*  www. j  a  v  a 2 s. com*/
            indexDirPath = resDirPath + properties.getProperty(ConfigurationConstants.INDEX_DIR);
            indexDir = FSDirectory.open(new File(indexDirPath).toPath());
            // clear the previous index files in the folder before a new
            // indexing process begins
            clearIndexDirectory(indexDir);
        } catch (IngestionException | IOException e) {
            throw new IngestionException(e);
        }
    } else {
        throw new IngestionException("Unrecognized " + ConfigurationConstants.INDEX_DIR_TYPE + ": "
                + properties.getProperty(ConfigurationConstants.INDEX_DIR_TYPE));
    }
}

From source file:com.ikanow.infinit.e.api.knowledge.SearchHandler.java

License:Open Source License

private synchronized void createAliasSearchCache(AliasLookupTable aliasTable) {
    // Check if we need to update the Lucene store:
    if ((null != _searcherCacheLastCreated) && (null != aliasTable.getLastModified())) {
        if (_searcherCacheLastCreated.getTime() >= aliasTable.getLastModified().getTime()) {
            return;
        }//from   w w  w  .j  a v a  2s. com
    } //TESTED

    RAMDirectory idx = new RAMDirectory();

    try {
        CrossVersionIndexWriter writer = new CrossVersionIndexWriter(idx, Version.LUCENE_30,
                new StandardAnalyzer(Version.LUCENE_30));
        int nAdded = 0;
        indexToSearchCacheIndexes = new EntityFeaturePojo[aliasTable.masters().size()];
        for (EntityFeaturePojo alias : aliasTable.masters()) {

            if ((null != alias.getIndex()) && (null != alias.getDisambiguatedName())
                    && (null != alias.getAlias()) && !alias.getIndex().equalsIgnoreCase("discard")
                    && !alias.getAlias().contains(alias.getIndex())) {
                // (that last check just means there's no point in including the alias if it has itself as a sub-alias) 
                writer.addSingleAnalyzedUnstoredFieldDocument("name", alias.getDisambiguatedName());
                indexToSearchCacheIndexes[nAdded] = alias;
                nAdded++;
                //System.out.println("CACHE ADD: " + alias.getDisambiguatedName() + ": " + nAdded + " - " + alias.getIndex());
            }
        }
        writer.close();

        if (nAdded > 0) {
            if (null != _aliasSearcherCache) {
                try {
                    _aliasSearcherCache.getIndexReader().close();
                } catch (Exception e) {
                }
            }
            _aliasSearcherCache = new CrossVersionIndexSearcher(idx);
            if (null != _aliasSearcherCache) {
                _searcherCacheLastCreated = aliasTable.getLastModified();
            }
        } else {
            _aliasSearcherCache = null;
            _searcherCacheLastCreated = aliasTable.getLastModified();
        }
    } //TESTED
    catch (Exception e) {
        //Probably should never happen once set up correctly
        e.printStackTrace();
    }
}

From source file:com.ikon.analysis.SearchDemo.java

License:Open Source License

public static void main(String args[]) throws Exception {
    for (Analyzer analyzer : analyzers) {
        System.out.println("** Analyzer: " + analyzer.getClass().getName() + " **");
        Directory index = new RAMDirectory();

        for (String str : strings) {
            add(index, analyzer, str);//from w  w  w  . java 2  s .co m
        }

        search(index, analyzer, SEARCH_TERM);
        System.out.println();
    }
}

From source file:com.intuit.tank.search.lucene.LuceneServiceTest.java

License:Open Source License

/**
 * Run the LuceneService(Directory) constructor test.
 *
 * @throws Exception//from  www.  j  av  a  2  s  . c o m
 *
 * @generatedBy CodePro at 12/16/14 3:36 PM
 */
@Test
public void testLuceneService_3() throws Exception {
    Directory directory = new RAMDirectory();

    LuceneService result = new LuceneService(directory);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.IllegalArgumentException: Prefix string too short
    //       at java.io.File.createTempFile(File.java:2001)
    //       at java.io.File.createTempFile(File.java:2070)
    assertNotNull(result);
}