Example usage for org.apache.lucene.index IndexWriterConfig setCodec

List of usage examples for org.apache.lucene.index IndexWriterConfig setCodec

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriterConfig setCodec.

Prototype

public IndexWriterConfig setCodec(Codec codec) 

Source Link

Document

Set the Codec .

Usage

From source file:org.kie.kieora.backend.lucene.setups.DirectoryLuceneSetup.java

License:Apache License

public DirectoryLuceneSetup(final Directory directory, final boolean freshIndex) {
    try {// www.j  av  a2s .co  m
        this.freshIndex = freshIndex;
        this.directory = checkNotNull("directory", directory);
        this.analyzer = new StandardAnalyzer(LUCENE_40);
        final IndexWriterConfig config = new IndexWriterConfig(LUCENE_40, getAnalyzer());

        final Codec codec = new Lucene40Codec() {
            @Override
            public PostingsFormat getPostingsFormatForField(String field) {
                if (field.equals("id")) {
                    return PostingsFormat.forName("Memory");
                } else {
                    return PostingsFormat.forName("Lucene40");
                }
            }
        };
        config.setCodec(codec);

        this.writer = new IndexWriter(directory, config);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.kie.uberfire.metadata.backend.lucene.index.directory.DirectoryFactory.java

License:Apache License

private IndexWriterConfig newConfig(final Analyzer analyzer) {
    final IndexWriterConfig config = new IndexWriterConfig(LUCENE_40, analyzer);
    final Codec codec = new Lucene40Codec() {
        @Override// w  ww .ja va  2s . c  om
        public PostingsFormat getPostingsFormatForField(String field) {
            if (field.equals("id")) {
                return PostingsFormat.forName("Memory");
            } else {
                return PostingsFormat.forName("Lucene40");
            }
        }
    };
    config.setCodec(codec);

    return config;
}

From source file:org.meresco.lucene.numerate.UriEnumerate.java

License:Open Source License

/**
 *
 * @param path/*from  w w  w .j av a 2 s . c  om*/
 * @param max_cache_size
 * @param withTransactionLog allows for crash recovery, but slows down UriNumerate considerably because of file system flush.
 * @throws IOException
 */
public UriEnumerate(String path, int max_cache_size, boolean withTransactionLog) throws IOException {
    IndexWriterConfig config = new IndexWriterConfig(null);
    ConcurrentMergeScheduler ms = (ConcurrentMergeScheduler) config.getMergeScheduler();
    ms.setDefaultMaxMergesAndThreads(/* spins= */false);
    LogDocMergePolicy mp = new LogDocMergePolicy();
    mp.setMergeFactor(2);
    mp.setMinMergeDocs(max_cache_size);
    config.setMergePolicy(mp);
    config.setCodec(new Lucene60Codec() {
        @Override
        public PostingsFormat getPostingsFormatForField(String field) {
            return new BloomFilteringPostingsFormat(super.getPostingsFormatForField(field));
        }
    });
    config.setUseCompoundFile(false);
    this.writer = new IndexWriter(FSDirectory.open(FileSystems.getDefault().getPath(path)), config);
    this.next_ord = writer.numDocs() + 1;
    this.searcher = new SimpleSearcher(this.writer);
    this.cache = new Cache(max_cache_size, () -> this.commit());
    this.transactionLog = new TransactionLog(withTransactionLog ? path + "/transactionLog" : null);
    this.transactionLog.maybeRecover();
}

From source file:org.modeshape.jcr.index.lucene.LuceneConfig.java

License:Apache License

private IndexWriterConfig newIndexWriterConfig() {
    IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);
    writerConfig.setCommitOnClose(true);
    writerConfig.setCodec(codec);
    return writerConfig;
}

From source file:org.neo4j.kernel.api.impl.index.IndexWriterConfigs.java

License:Open Source License

public static IndexWriterConfig standard() {
    IndexWriterConfig writerConfig = new IndexWriterConfig(LuceneDataSource.KEYWORD_ANALYZER);

    writerConfig.setMaxBufferedDocs(MAX_BUFFERED_DOCS);
    writerConfig.setIndexDeletionPolicy(new MultipleBackupDeletionPolicy());
    writerConfig.setUseCompoundFile(true);
    writerConfig.setCodec(new Lucene54Codec() {
        @Override/*www  .jav a  2 s .c  om*/
        public PostingsFormat getPostingsFormatForField(String field) {
            PostingsFormat postingFormat = super.getPostingsFormatForField(field);
            return CODEC_BLOCK_TREE_ORDS_POSTING_FORMAT ? blockTreeOrdsPostingsFormat : postingFormat;
        }
    });

    LogByteSizeMergePolicy mergePolicy = new LogByteSizeMergePolicy();
    mergePolicy.setNoCFSRatio(MERGE_POLICY_NO_CFS_RATIO);
    mergePolicy.setMinMergeMB(MERGE_POLICY_MIN_MERGE_MB);
    mergePolicy.setMergeFactor(MERGE_POLICY_MERGE_FACTOR);
    writerConfig.setMergePolicy(mergePolicy);

    return writerConfig;
}

From source file:org.netbeans.mvn.index.perftest.Main.java

private static IndexWriter createIndexWriter(File file) throws IOException {
    final FSDirectory out = FSDirectory.open(file);
    final IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_CURRENT,
            new StandardAnalyzer(Version.LUCENE_CURRENT));
    cfg.setCodec(new NoCompressCodec(new Lucene46Codec()));
    return new IndexWriter(out, cfg);
}

From source file:org.opengrok.indexer.index.IndexDatabase.java

License:Open Source License

/**
 * Update the content of this index database
 *
 * @throws IOException if an error occurs
 *///from  ww w  .  ja v  a 2s. co m
public void update() throws IOException {
    synchronized (lock) {
        if (running) {
            throw new IOException("Indexer already running!");
        }
        running = true;
        interrupted = false;
    }

    RuntimeEnvironment env = RuntimeEnvironment.getInstance();

    reader = null;
    writer = null;
    settings = null;
    uidIter = null;
    postsIter = null;
    acceptedNonlocalSymlinks.clear();

    IOException finishingException = null;
    try {
        Analyzer analyzer = AnalyzerGuru.getAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwc.setRAMBufferSizeMB(env.getRamBufferSize());
        /**
         * Most data in OpenGrok is indexed but not stored, so use the best
         * compression on the minority of data that is stored, since it
         * should not have a detrimental impact on overall throughput.
         */
        iwc.setCodec(new Lucene70Codec(Lucene50StoredFieldsFormat.Mode.BEST_COMPRESSION));
        writer = new IndexWriter(indexDirectory, iwc);
        writer.commit(); // to make sure index exists on the disk
        completer = new PendingFileCompleter();

        if (directories.isEmpty()) {
            if (project == null) {
                directories.add("");
            } else {
                directories.add(project.getPath());
            }
        }

        for (String dir : directories) {
            File sourceRoot;
            if ("".equals(dir)) {
                sourceRoot = env.getSourceRootFile();
            } else {
                sourceRoot = new File(env.getSourceRootFile(), dir);
            }

            if (env.isHistoryEnabled()) {
                try {
                    HistoryGuru.getInstance().ensureHistoryCacheExists(sourceRoot);
                } catch (HistoryException ex) {
                    String exmsg = String.format("Failed to ensureHistoryCacheExists() for %s", sourceRoot);
                    LOGGER.log(Level.SEVERE, exmsg, ex);
                    continue;
                }
            }

            dir = Util.fixPathIfWindows(dir);

            String startuid = Util.path2uid(dir, "");
            reader = DirectoryReader.open(indexDirectory); // open existing index
            settings = readAnalysisSettings();
            if (settings == null) {
                settings = new IndexAnalysisSettings();
            }
            Terms terms = null;
            int numDocs = reader.numDocs();
            if (numDocs > 0) {
                Fields uFields = MultiFields.getFields(reader);//reader.getTermVectors(0);
                terms = uFields.terms(QueryBuilder.U);
            }

            try {
                if (terms != null) {
                    uidIter = terms.iterator();
                    TermsEnum.SeekStatus stat = uidIter.seekCeil(new BytesRef(startuid)); //init uid
                    if (stat == TermsEnum.SeekStatus.END) {
                        uidIter = null;
                        LOGGER.log(Level.WARNING, "Couldn''t find a start term for {0}, empty u field?",
                                startuid);
                    }
                }

                // The actual indexing happens in indexParallel().

                IndexDownArgs args = new IndexDownArgs();
                Statistics elapsed = new Statistics();
                LOGGER.log(Level.INFO, "Starting traversal of directory {0}", dir);
                indexDown(sourceRoot, dir, args);
                showFileCount(dir, args, elapsed);

                args.cur_count = 0;
                elapsed = new Statistics();
                LOGGER.log(Level.INFO, "Starting indexing of directory {0}", dir);
                indexParallel(dir, args);
                elapsed.report(LOGGER, String.format("Done indexing of directory %s", dir));

                // Remove data for the trailing terms that indexDown()
                // did not traverse. These correspond to files that have been
                // removed and have higher ordering than any present files.
                while (uidIter != null && uidIter.term() != null
                        && uidIter.term().utf8ToString().startsWith(startuid)) {

                    removeFile(true);
                    BytesRef next = uidIter.next();
                    if (next == null) {
                        uidIter = null;
                    }
                }

                markProjectIndexed(project);
            } finally {
                reader.close();
            }
        }

        try {
            finishWriting();
        } catch (IOException e) {
            finishingException = e;
        }
    } catch (RuntimeException ex) {
        LOGGER.log(Level.SEVERE, "Failed with unexpected RuntimeException", ex);
        throw ex;
    } finally {
        completer = null;
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            if (finishingException == null) {
                finishingException = e;
            }
            LOGGER.log(Level.WARNING, "An error occurred while closing writer", e);
        } finally {
            writer = null;
            synchronized (lock) {
                running = false;
            }
        }
    }

    if (finishingException != null) {
        throw finishingException;
    }

    if (!isInterrupted() && isDirty()) {
        if (env.isOptimizeDatabase()) {
            optimize();
        }
        env.setIndexTimestamp();
    }
}

From source file:org.sindice.siren.demo.SimpleIndexer.java

License:Apache License

private IndexWriter initializeIndexWriter() throws IOException {
    final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, this.initializeAnalyzer());

    // Register the SIREn codec
    config.setCodec(new Siren10Codec());

    return new IndexWriter(dir, config);
}

From source file:org.uberfire.ext.metadata.backend.lucene.index.directory.DirectoryFactory.java

License:Apache License

private IndexWriterConfig newConfig(final Analyzer analyzer) {
    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    final Codec codec = new Lucene53Codec() {
        @Override//  w ww . j  av  a2s  . co  m
        public PostingsFormat getPostingsFormatForField(String field) {
            if (field.equals("id")) {
                return PostingsFormat.forName("Memory");
            } else {
                return PostingsFormat.forName("Lucene50");
            }
        }
    };
    config.setCodec(codec);

    return config;
}

From source file:org.uberfire.metadata.backend.lucene.setups.DirectoryLuceneSetup.java

License:Apache License

public DirectoryLuceneSetup(final Directory directory, final boolean freshIndex) {
    try {//  www . ja  v a 2  s.  c  o m
        this.freshIndex = freshIndex;
        this.directory = checkNotNull("directory", directory);

        this.analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(LUCENE_40),
                new HashMap<String, Analyzer>() {
                    {
                        put(CUSTOM_FIELD_FILENAME, new FilenameAnalyzer(LUCENE_40));
                    }
                });

        final IndexWriterConfig config = new IndexWriterConfig(LUCENE_40, getAnalyzer());

        final Codec codec = new Lucene40Codec() {
            @Override
            public PostingsFormat getPostingsFormatForField(String field) {
                if (field.equals("id")) {
                    return PostingsFormat.forName("Memory");
                } else {
                    return PostingsFormat.forName("Lucene40");
                }
            }
        };
        config.setCodec(codec);

        this.writer = new IndexWriter(directory, config);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}