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

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

Introduction

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

Prototype

public IndexWriterConfig(Analyzer analyzer) 

Source Link

Document

Creates a new config that with the provided Analyzer .

Usage

From source file:com.shaie.suggest.ContextSuggestDemo.java

License:Apache License

private void buildSearchIndex() throws IOException {
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    try (IndexWriter writer = new IndexWriter(indexDir, conf)) {
        Document doc = new Document();
        doc.add(new StringField("username", USER1_CONTEXT.utf8ToString(), Store.YES));
        doc.add(new TextField("content", USER1_TEXT.utf8ToString(), Store.YES));
        writer.addDocument(doc);/*from  w  w w  .  ja va2 s .  c o  m*/

        doc = new Document();
        doc.add(new StringField("username", USER2_CONTEXT.utf8ToString(), Store.YES));
        doc.add(new TextField("content", USER2_TEXT.utf8ToString(), Store.YES));
        writer.addDocument(doc);
    }
}

From source file:com.shaie.UTF8Indexing.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Directory dir = new RAMDirectory();
    final StandardAnalyzer analyzer = new StandardAnalyzer();
    final IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    final IndexWriter writer = new IndexWriter(dir, conf);

    final Document doc = new Document();
    doc.add(new TextField("f", "Russia\u2013United States relations", Store.YES));
    writer.addDocument(doc);//from   w  ww  . ja  va  2 s.c  om
    writer.close();

    final DirectoryReader reader = DirectoryReader.open(dir);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final QueryParser qp = new QueryParser("f", analyzer);
    search(searcher, qp, "Russia United States relations");
    search(searcher, qp, "\"Russia United states relations\"");
    search(searcher, qp, "\"Russia-United states relations\"");
    search(searcher, qp, "\"Russia\u2013United states relations\"");
    reader.close();

    dir.close();
}

From source file:com.spike.text.lucene.util.LuceneTestBookIndexingUtil.java

License:Apache License

public static void main(String[] args) throws IOException {
    String dataDir = LuceneAppConstants.BOOK_DATA_DIR;
    String indexDir = LuceneAppConstants.BOOK_INDEX_DIR;

    List<File> results = new ArrayList<File>();
    findFiles(results, new File(dataDir));
    System.out.println(results.size() + " books to index");
    Directory directory = FSDirectory.open(Paths.get(indexDir));

    IndexWriterConfig config = new IndexWriterConfig(new MyStandardAnalyzer());
    config.setCommitOnClose(true);/*from   ww w.  j  a  v a2 s  .  co m*/
    IndexWriter indexWriter = new IndexWriter(directory, config);

    for (File file : results) {
        Document document = getDocument(dataDir, file);
        indexWriter.addDocument(document);
    }

    indexWriter.close();
    directory.close();
}

From source file:com.stratio.cassandra.lucene.index.DocumentIteratorTest.java

License:Apache License

@Test
public void testConstructorWithPageEqualsZero() throws IOException {
    IndexWriterConfig iwConfig = new IndexWriterConfig(new AnalyzerMock());
    SearcherManager searcherManager = new SearcherManager(new IndexWriterMock(new DirectoryMock(), iwConfig),
            true, null);/*from   w  ww  .  ja v  a2s . c  o  m*/
    DocumentIterator docIterator = new DocumentIterator(searcherManager, new Sort(), null, new QueryMock(),
            new Sort(), 0, new HashSet<>());

    assertEquals("document Iterator page is invalid", 1, docIterator.page);
}

From source file:com.stratio.cassandra.lucene.index.DocumentIteratorTest.java

License:Apache License

@Test
public void testConstructorWithPageEqualsOne() throws IOException {
    IndexWriterConfig iwConfig = new IndexWriterConfig(new AnalyzerMock());
    SearcherManager searcherManager = new SearcherManager(new IndexWriterMock(new DirectoryMock(), iwConfig),
            true, null);/*from  ww w  .j  a  v a 2  s. c  o m*/
    DocumentIterator docIterator = new DocumentIterator(searcherManager, new Sort(), null, new QueryMock(),
            new Sort(), 1, new HashSet<>());

    assertEquals("document Iterator page is invalid", 2, docIterator.page);
}

From source file:com.stratio.cassandra.lucene.index.DocumentIteratorTest.java

License:Apache License

@Test
public void testConstructorWithPageEqualsMaxValue() throws IOException {
    IndexWriterConfig iwConfig = new IndexWriterConfig(new AnalyzerMock());
    SearcherManager searcherManager = new SearcherManager(new IndexWriterMock(new DirectoryMock(), iwConfig),
            true, null);/*from  ww  w  .jav a2s  .co m*/
    DocumentIterator docIterator = new DocumentIterator(searcherManager, new Sort(), null, new QueryMock(),
            new Sort(), DocumentIterator.MAX_PAGE_SIZE, new HashSet<>());

    assertEquals("document Iterator page is invalid", DocumentIterator.MAX_PAGE_SIZE + 1, docIterator.page);
}

From source file:com.stratio.cassandra.lucene.index.DocumentIteratorTest.java

License:Apache License

@Test
public void testConstructorWithPageOverMaxValue() throws IOException {
    IndexWriterConfig iwConfig = new IndexWriterConfig(new AnalyzerMock());
    SearcherManager searcherManager = new SearcherManager(new IndexWriterMock(new DirectoryMock(), iwConfig),
            true, null);//from   w  w  w  .  j  ava 2s. c  o  m
    DocumentIterator docIterator = new DocumentIterator(searcherManager, new Sort(), null, new QueryMock(),
            new Sort(), 10000000, new HashSet<>());

    assertEquals("document Iterator page is invalid", DocumentIterator.MAX_PAGE_SIZE + 1, docIterator.page);
}

From source file:com.stratio.cassandra.lucene.index.FSIndex.java

License:Apache License

/**
 * Builds a new {@link FSIndex}.//  w w w  .ja v a  2s. c o m
 *
 * @param name the index name
 * @param mbeanName the JMX MBean object name
 * @param path the directory path
 * @param analyzer the index writer analyzer
 * @param refresh the index reader refresh frequency in seconds
 * @param ramBufferMB the index writer RAM buffer size in MB
 * @param maxMergeMB the directory max merge size in MB
 * @param maxCachedMB the directory max cache size in MB
 * @param refreshTask action to be done during refresh
 */
public FSIndex(String name, String mbeanName, Path path, Analyzer analyzer, double refresh, int ramBufferMB,
        int maxMergeMB, int maxCachedMB, Runnable refreshTask) {
    try {
        this.path = path;
        this.name = name;

        // Open or create directory
        FSDirectory fsDirectory = FSDirectory.open(path);
        directory = new NRTCachingDirectory(fsDirectory, maxMergeMB, maxCachedMB);

        // Setup index writer
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        indexWriterConfig.setRAMBufferSizeMB(ramBufferMB);
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        indexWriterConfig.setUseCompoundFile(true);
        indexWriterConfig.setMergePolicy(new TieredMergePolicy());
        indexWriter = new IndexWriter(directory, indexWriterConfig);

        // Setup NRT search
        SearcherFactory searcherFactory = new SearcherFactory() {
            @Override
            public IndexSearcher newSearcher(IndexReader reader, IndexReader previousReader) {
                if (refreshTask != null) {
                    refreshTask.run();
                }
                IndexSearcher searcher = new IndexSearcher(reader);
                searcher.setSimilarity(new NoIDFSimilarity());
                return searcher;
            }
        };
        TrackingIndexWriter trackingWriter = new TrackingIndexWriter(indexWriter);
        searcherManager = new SearcherManager(indexWriter, true, searcherFactory);
        searcherReopener = new ControlledRealTimeReopenThread<>(trackingWriter, searcherManager, refresh,
                refresh);
        searcherReopener.start();

        // Register JMX MBean
        mbean = new ObjectName(mbeanName);
        ManagementFactory.getPlatformMBeanServer().registerMBean(this, this.mbean);

    } catch (Exception e) {
        throw new IndexException(logger, e, "Error while creating index %s", name);
    }
}

From source file:com.stratio.cassandra.lucene.index.RAMIndex.java

License:Apache License

/**
 * Builds a new {@link RAMIndex}./*ww  w .  java 2s .co m*/
 *
 * @param analyzer the index writer analyzer
 */
public RAMIndex(Analyzer analyzer) {
    try {
        directory = new RAMDirectory();
        indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer));
    } catch (Exception e) {
        throw new IndexException(logger, e, "Error while creating index");
    }
}

From source file:com.stratio.cassandra.lucene.service.LuceneIndex.java

License:Apache License

/**
 * Builds a new {@code RowDirectory} using the specified directory path and analyzer.
 *
 * @param keyspace        The keyspace name.
 * @param table           The table name.
 * @param name            The index name.
 * @param path            The path of the directory in where the Lucene files will be stored.
 * @param ramBufferMB     The index writer buffer size in MB.
 * @param maxMergeMB      NRTCachingDirectory max merge size in MB.
 * @param maxCachedMB     NRTCachingDirectory max cached MB.
 * @param analyzer        The default {@link Analyzer}.
 * @param refreshSeconds  The index readers refresh time in seconds. Writings are not visible until this time.
 * @param refreshCallback A runnable to be run on index refresh.
 * @throws IOException If Lucene throws IO errors.
 *//*  ww  w  .j av a  2 s. c o  m*/
public LuceneIndex(String keyspace, String table, String name, Path path, Integer ramBufferMB,
        Integer maxMergeMB, Integer maxCachedMB, Analyzer analyzer, Double refreshSeconds,
        Runnable refreshCallback) throws IOException {
    this.path = path;
    this.refreshCallback = refreshCallback;
    this.logName = String.format("Lucene index %s.%s.%s", keyspace, table, name);

    // Open or create directory
    FSDirectory fsDirectory = FSDirectory.open(path);
    directory = new NRTCachingDirectory(fsDirectory, maxMergeMB, maxCachedMB);

    // Setup index writer
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    config.setRAMBufferSizeMB(ramBufferMB);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    config.setUseCompoundFile(true);
    config.setMergePolicy(new TieredMergePolicy());
    indexWriter = new IndexWriter(directory, config);

    // Setup NRT search
    SearcherFactory searcherFactory = new SearcherFactory() {
        public IndexSearcher newSearcher(IndexReader reader) throws IOException {
            LuceneIndex.this.refreshCallBack();
            IndexSearcher searcher = new IndexSearcher(reader);
            searcher.setSimilarity(new NoIDFSimilarity());
            return searcher;
        }
    };
    TrackingIndexWriter trackingIndexWriter = new TrackingIndexWriter(indexWriter);
    searcherManager = new SearcherManager(indexWriter, true, searcherFactory);
    searcherReopener = new ControlledRealTimeReopenThread<>(trackingIndexWriter, searcherManager,
            refreshSeconds, refreshSeconds);
    searcherReopener.start(); // Start the refresher thread

    // Register JMX MBean
    try {
        objectName = new ObjectName(
                String.format("com.stratio.cassandra.lucene:type=LuceneIndexes,keyspace=%s,table=%s,index=%s",
                        keyspace, table, name));
        ManagementFactory.getPlatformMBeanServer().registerMBean(this, objectName);
    } catch (MBeanException | OperationsException e) {
        Log.error(e, "Error while registering MBean");
    }
}