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

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

Introduction

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

Prototype

public IndexWriterConfig setOpenMode(OpenMode openMode) 

Source Link

Document

Specifies OpenMode of the index.

Usage

From source file:com.epimorphics.server.indexers.LuceneIndex.java

License:Apache License

protected synchronized IndexWriter getIndexWriter() {
    if (writer == null) {
        try {/*from  w w w.  j a va 2 s .  c om*/
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
            writer = new IndexWriter(indexDir, config);
            config.setOpenMode(OpenMode.CREATE_OR_APPEND);
        } catch (Exception e) {
            throw new EpiException(e);
        }
    }
    return writer;
}

From source file:com.example.search.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "index";
    // String docsPath = "E:\\work\\webExp\\exp\\sina_ansi";
    String docsPath = ".\\doc";
    boolean create = true;
    /*    for(int i=0;i<args.length;i++) {
          if ("-index".equals(args[i])) {
            indexPath = args[i+1];/*from   ww  w . j a v a  2  s.  com*/
            i++;
          } else if ("-docs".equals(args[i])) {
            docsPath = args[i+1];
            i++;
          } else if ("-update".equals(args[i])) {
            create = false;
          }
        }*/

    /* if (docsPath == null) {
       System.err.println("Usage: " + usage);
       System.exit(1);
     }*/

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(new File(indexPath));
        //      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
        Analyzer analyzer = new ICTCLASAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getMinutes() - start.getMinutes() + " total minutes");
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.flycode.CRIBSearch.SearchEngine.Demo.IndexFiles.java

License:Apache License

/**
 * Index all text files under a directory.
 *///from   w  w w .  j a v  a 2s . com
public static void main(String[] args) {
    String usage = "java com.flycode.CRIBSearch.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles class";
    String indexPath = "index";
    String docsPath = null;
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.foundationdb.lucene.SimpleTest.java

License:Open Source License

@Test
public void indexBasic() throws Exception {
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    // recreate the index on each execution
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    config.setCodec(new FDBCodec());
    FDBDirectory dir = createDirectoryForMethod();
    IndexWriter writer = new IndexWriter(dir, config);
    try {/* w  w w. j a v a2 s  . com*/
        writer.addDocument(Arrays.asList(new TextField("title", "The title of my first document", Store.YES),
                new TextField("content", "The content of the first document", Store.NO)));

        writer.addDocument(Arrays.asList(new TextField("title", "The title of the second document", Store.YES),
                new TextField("content", "And this is the content", Store.NO)));
    } finally {
        writer.close();
    }
    assertDocumentsAreThere(dir, 2);
}

From source file:com.fuerve.villageelder.actions.results.SearchResultItemTest.java

License:Apache License

private void buildDummyIndex(final Directory indexDirectory, final Directory taxonomyDirectory)
        throws IOException {
    IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer());
    iwc.setOpenMode(OpenMode.CREATE);
    IndexWriter iw = new IndexWriter(indexDirectory, iwc);
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectory, OpenMode.CREATE);
    List<CategoryPath> categories = new ArrayList<CategoryPath>();
    FacetFields facetFields = new FacetFields(tw);

    Document doc = new Document();
    categories.clear();//from  ww  w  . j a v a2  s.  co m
    doc.add(new StringField("Author", "foo", Store.YES));
    categories.add(new CategoryPath("Author", "foo"));
    doc.add(new LongField("RevisionNumber", 50L, Store.YES));
    doc.add(new StringField("Revision", "50", Store.YES));
    doc.add(new TextField("Message", "stuff", Store.YES));
    iw.addDocument(doc);
    facetFields.addFields(doc, categories);

    doc = new Document();
    facetFields = new FacetFields(tw);
    categories.clear();
    doc.add(new StringField("Author", "bar", Store.YES));
    categories.add(new CategoryPath("Author", "bar"));
    doc.add(new LongField("RevisionNumber", 5000L, Store.YES));
    doc.add(new StringField("Revision", "5000", Store.YES));
    doc.add(new TextField("Message", "stuff", Store.YES));
    iw.addDocument(doc);
    facetFields.addFields(doc, categories);

    tw.commit();
    tw.close();
    iw.commit();
    iw.close();
}

From source file:com.fuerve.villageelder.indexing.IndexManager.java

License:Apache License

/**
 * Gets the writers for the regular and taxonomy indices ready to go.
 * @throws IOException A fatal exception occurred while trying to
 * construct the index writers.//from   ww  w. j a  va2  s  .c  o  m
 */
private void initializeWriters() throws IOException {
    if (luceneVersion == null || analyzer == null) {
        throw new IllegalArgumentException("The Lucene version and the index analyzer were unspecified "
                + "when attempting to create the index writers");
    }
    IndexWriterConfig iwc = new IndexWriterConfig(luceneVersion, analyzer);
    iwc.setOpenMode(openMode);

    indexWriter = new IndexWriter(indexDirectory, iwc);
    taxonomyWriter = new DirectoryTaxonomyWriter(taxonomyDirectory, openMode);
}

From source file:com.fuerve.villageelder.search.SearchQueryParserTest.java

License:Apache License

private IndexReader buildDummyIndex() throws IOException {
    RAMDirectory indexDirectory = new RAMDirectory();

    IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer());
    iwc.setOpenMode(OpenMode.CREATE);
    IndexWriter iw = new IndexWriter(indexDirectory, iwc);

    Document doc = new Document();
    doc.add(new StringField("Author", "foo", Field.Store.YES));
    doc.add(new LongField("RevisionNumber", 50L, Field.Store.YES));
    doc.add(new StringField("Revision", "50", Field.Store.YES));
    doc.add(new TextField("Message", "stuff", Field.Store.YES));
    iw.addDocument(doc);/*  w w w.java 2  s .c o  m*/

    doc = new Document();
    doc.add(new StringField("Author", "bar", Field.Store.YES));
    doc.add(new LongField("RevisionNumber", 5000L, Field.Store.YES));
    doc.add(new StringField("Revision", "5000", Field.Store.YES));
    doc.add(new TextField("Message", "stuff", Field.Store.YES));
    iw.addDocument(doc);
    iw.commit();
    iw.close();

    DirectoryReader result = DirectoryReader.open(indexDirectory);
    return result;
}

From source file:com.fun.sb.demo.lucene.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles"
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
            + "This indexes the documents in DOCS_PATH, creating a Lucene index"
            + "in INDEX_PATH that can be searched with SearchFiles";
    String indexPath = "/Users/baidu/temp/index/";
    String docsPath = "/Users/baidu/temp/";
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            indexPath = args[i + 1];/*from w ww  .ja  v  a  2s  . c  o  m*/
            i++;
        } else if ("-docs".equals(args[i])) {
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) {
            create = false;
        }
    }

    if (docsPath == null) {
        System.err.println("Usage: " + usage);
        System.exit(1);
    }

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * Gets an index writer for the repository. The index will be created if it
 * does not already exist or if forceCreate is specified.
 * /*from ww w  .j a  v  a2 s  . co m*/
 * @param repository
 * @return an IndexWriter
 * @throws IOException
 */
private IndexWriter getIndexWriter(String repository) throws IOException {
    IndexWriter indexWriter = writers.get(repository);
    File repositoryFolder = FileKey.resolve(new File(repositoriesFolder, repository), FS.DETECTED);
    File indexFolder = new File(repositoryFolder, LUCENE_DIR);
    Directory directory = FSDirectory.open(indexFolder);

    if (indexWriter == null) {
        if (!indexFolder.exists()) {
            indexFolder.mkdirs();
        }
        StandardAnalyzer analyzer = new StandardAnalyzer(LUCENE_VERSION);
        IndexWriterConfig config = new IndexWriterConfig(LUCENE_VERSION, analyzer);
        config.setOpenMode(OpenMode.CREATE_OR_APPEND);
        indexWriter = new IndexWriter(directory, config);
        writers.put(repository, indexWriter);
    }
    return indexWriter;
}

From source file:com.gitblit.service.LuceneService.java

License:Apache License

/**
 * Gets an index writer for the repository. The index will be created if it
 * does not already exist or if forceCreate is specified.
 *
 * @param repository//from   ww  w . ja  v  a  2  s.c o  m
 * @return an IndexWriter
 * @throws IOException
 */
private IndexWriter getIndexWriter(String repository) throws IOException {
    IndexWriter indexWriter = writers.get(repository);
    if (indexWriter == null) {
        File repositoryFolder = FileKey.resolve(new File(repositoriesFolder, repository), FS.DETECTED);
        LuceneRepoIndexStore indexStore = new LuceneRepoIndexStore(repositoryFolder, INDEX_VERSION);
        indexStore.create();
        Directory directory = FSDirectory.open(indexStore.getPath());
        StandardAnalyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        config.setOpenMode(OpenMode.CREATE_OR_APPEND);
        indexWriter = new IndexWriter(directory, config);
        writers.put(repository, indexWriter);
    }
    return indexWriter;
}