Example usage for org.apache.lucene.index IndexWriter addDocument

List of usage examples for org.apache.lucene.index IndexWriter addDocument

Introduction

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

Prototype

public long addDocument(Iterable<? extends IndexableField> doc) throws IOException 

Source Link

Document

Adds a document to this index.

Usage

From source file:edu.cuhk.hccl.cmd.AppSearchEngine.java

License:Apache License

/**
 * Create index of RAMDirectory from a data folder with text files
 * @param dataFolder//from  www.j a v  a 2s  .  c om
 * @param analyzer
 * @return
 * @throws IOException
 */
private static Directory createIndex(File dataFolder, StandardAnalyzer analyzer) throws IOException {

    Directory index = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
    IndexWriter writer = new IndexWriter(index, config);

    Collection<File> files = FileUtils.listFiles(dataFolder, null, true);
    for (File file : files) {
        String path = file.getPath();
        String content = FileUtils.readFileToString(file);

        Document doc = new Document();

        doc.add(new StringField(PATH_FIELD, path, Field.Store.YES));
        doc.add(new Field(CONTENT_FIELD, content, TERM_STORED));

        writer.addDocument(doc);

        System.out.println("[INFO] Indexing file: " + path);
    }

    System.out.println("\n[INFO]" + files.size() + " files has been indexed.");

    writer.close();

    return index;
}

From source file:edu.cuhk.hccl.Indexer.java

License:Apache License

/**
 * Create index of RAMDirectory from a data folder with text files
 * //from  w ww . j a  va 2s  .  c  o  m
 * @param dataSet
 * @throws IOException
 */
public static void createIndex(String dataSet) throws IOException {
    IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
    IndexWriter writer = new IndexWriter(index, config);

    Collection<File> files = FileUtils.listFiles(new File(dataSet), null, true);
    for (File file : files) {
        String path = file.getPath();
        String content = FileUtils.readFileToString(file);

        Document doc = new Document();

        doc.add(new StringField(PATH_FIELD, path, Field.Store.YES));
        doc.add(new Field(CONTENT_FIELD, content, TERM_STORED));

        writer.addDocument(doc);

        System.out.println("[INFO] Indexing file: " + path);
    }

    System.out.println("\n[INFO]" + files.size() + " files has been indexed.");

    writer.close();
}