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:dacapo.lucene.LuceneIndex.java

License:Apache License

/**
 * Index either a file or a directory tree.
 * //  w  w w. j  a  v a 2s . c o  m
 * @param writer
 * @param file
 * @throws IOException
 */
void indexDocs(IndexWriter writer, File file) throws IOException {

    /* Strip the absolute part of the path name from file name output */
    int scratchP = scratch.getPath().length() + 1;

    /* do not try to index files that cannot be read */
    if (file.canRead()) {
        if (file.isDirectory()) {
            String[] files = file.list();
            // an IO error could occur
            if (files != null) {
                sortArray(files);
                for (int i = 0; i < files.length; i++) {
                    indexDocs(writer, new File(file, files[i]));
                }
            }
        } else {
            System.out.println("adding " + file.getPath().substring(scratchP));
            try {
                writer.addDocument(FileDocument.Document(file));
            }
            // at least on windows, some temporary files raise this exception with an "access denied" message
            // checking if the file can be read doesn't help
            catch (FileNotFoundException fnfe) {
                ;
            }
        }
    }
}

From source file:ddf.catalog.pubsub.criteria.contextual.ContextualEvaluator.java

License:Open Source License

/**
 * Create a field with the specified field name and value, and add it to a Lucene Document to be
 * added to the specified IndexWriter./*  w  w  w .  j  a  v  a 2  s.c om*/
 *
 * @param indexWriter
 * @param fieldName
 * @param value
 *
 * @throws IOException
 */
private static void addDoc(IndexWriter indexWriter, String fieldName, String value) throws IOException {
    Document doc = new Document();
    doc.add(new Field(fieldName, value, Field.Store.YES, Field.Index.ANALYZED,
            Field.TermVector.WITH_POSITIONS_OFFSETS));
    indexWriter.addDocument(doc);
}

From source file:de.berlios.jhelpdesk.utils.LuceneIndexer.java

License:Open Source License

public synchronized void addToIndex(Article article) {
    IndexWriter indexWriter = null;
    try {/*w w w. j  av a 2 s . c om*/
        indexWriter = getIndexWriter();
        indexWriter.addDocument(articleToDocument(article));
        indexWriter.commit();
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    } finally {
        closeWriter(indexWriter);
    }
}

From source file:de.blizzy.documentr.search.AllDocIdsCollectorTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument());
    writer.addDocument(createDocument());
    writer.addDocument(createDocument());
    writer.commit();//from w w w. ja v  a2  s. co  m
    writer.close(true);

    reader = DirectoryReader.open(directory);
}

From source file:de.blizzy.documentr.search.GetSearchHitTaskTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument("project", "branch", "home", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            new String[] { "tag1", "tag2" }, //$NON-NLS-1$ //$NON-NLS-2$
            "title", "some text")); //$NON-NLS-1$ //$NON-NLS-2$
    writer.commit();/*from  ww  w  .j a v  a2s .c  o  m*/
    writer.close(true);

    reader = DirectoryReader.open(directory);

    Query query = new TermQuery(new Term("text", "some")); //$NON-NLS-1$ //$NON-NLS-2$
    task = new GetSearchHitTask(query, reader, 0, analyzer);
}

From source file:de.blizzy.documentr.search.InaccessibleDocIdsCollectorTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.commit();/*from   www.ja  va2 s .c o m*/
    writer.close(true);

    reader = DirectoryReader.open(directory);

    collector = new InaccessibleDocIdsCollector(Permission.VIEW, authentication, permissionEvaluator);
}

From source file:de.blizzy.documentr.search.PagePermissionFilterTest.java

License:Open Source License

@Before
public void setUp() throws IOException {
    directory = new RAMDirectory();

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer);
    writerConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    writer.addDocument(createDocument("project", "branch1", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.addDocument(createDocument("project", "branch2", "home")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    writer.commit();/*from www  .j a  va 2s . co  m*/
    writer.close(true);

    reader = DirectoryReader.open(directory);

    BitSet docs = new BitSet();
    docs.set(1);
    Bits docIds = new DocIdBitSet(docs);
    filter = new PagePermissionFilter(docIds);
}

From source file:de.citec.lucene.CreateIndex.java

private static void indexDoc(final IndexWriter writer, String input) {

    try {//  ww w.  j  a v a2s.  co  m
        input = input.replace("\n", "");
        String[] tmp = input.split("######");
        String persons = tmp[0];
        String id = tmp[1];
        String name = tmp[2];
        name = name.replace("-RRB-_TRUNC", "");
        name = name.replace("-LRB-_TRUNC", "");
        name = name.replace("._$.", "");
        name = name.replace(" ._$.", "");
        name = name.replace("_$.", "");
        name = name.replace("/_$[", "");
        name = name.replace("-_$[", "");
        name = name.replace("_$[", "");
        name = name.replace(" 's", "s");
        name = name.replace("' ", "");
        name = name.replace("'", " ");
        name = name.replace("  ", " ");
        if (name.contains(" ")) {
            String[] tmp_string = name.split(" ");
            name = "";
            for (String s : tmp_string) {
                if (s.contains("_")) {
                    name += " " + s.split("_")[0];
                } else
                    name += " " + s;
            }
        }
        name = name.trim();
        if (tmp.length < 4) {
            //System.out.println(input);
            counter += 1;
        } else {
            //System.out.println(name);
            String text = tmp[3];
            Document doc = new Document();
            Field field_persons = new StringField("persons", persons, Field.Store.NO);
            Field field_id = new StringField("id", id, Field.Store.YES);
            Field field_name = new TextField("name", name, Field.Store.YES);
            Field field_text = new TextField("text", text.toLowerCase(), Field.Store.NO);
            doc.add(field_persons);
            doc.add(field_id);
            doc.add(field_name);
            doc.add(field_text);
            writer.addDocument(doc);
        }

    } catch (Exception e) {
        e.printStackTrace();
        //System.out.println("Problem with:"+input);
    }

}

From source file:de.citec.sc.sentence.preprocessing.lucene.CreateIndex.java

private static void indexDoc(final IndexWriter writer, String s, Language language) {
    try {/* w  w  w .j  a  v a2s .  co  m*/
        String plain_sentence = getPlainSentence(s, language);
        Document doc = new Document();
        Field sentence = new TextField("plain", plain_sentence.toLowerCase(), Field.Store.NO);
        Field parsed_sentence = new TextField("parsed", s, Field.Store.YES);
        doc.add(sentence);
        doc.add(parsed_sentence);
        writer.addDocument(doc);

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Problem with:" + s);
    }

}

From source file:de.cosmocode.lucene.LuceneQueryTest.java

License:Apache License

/**
 * Creates a new Lucene Index with some predefined field and arguments.
 * @throws IOException if creating the index failed
 *//*from ww  w. j a  v  a  2s  .  com*/
@BeforeClass
public static void createLuceneIndex() throws IOException {
    final IndexWriter writer = IndexHelper.createIndexWriter();

    // iterate through all fields
    for (final String field : IndexHelper.ALL_FIELDS) {
        // name for default field: "arg", every other field: (fieldName)_arg
        final String fieldName = (field.equals(IndexHelper.DEFAULT_FIELD)) ? "arg" : field + "_arg";

        // iterate through all ARGS in IndexHelper and add documents with the current field
        int argsCounter = 0;
        for (final Object arg : IndexHelper.ARGS) {
            argsCounter++;

            // create document with only two fields: name and field => arg.
            writer.addDocument(
                    IndexHelper.createDocument("name", fieldName + argsCounter, field, arg.toString()));

            // add all args up to current argument combined for conjunct searches
            final Multimap<String, Object> multiArgs = ArrayListMultimap.create();
            multiArgs.put("name", fieldName + "1-" + argsCounter);
            for (final Object innerArg : Arrays.copyOf(IndexHelper.ARGS, argsCounter)) {
                multiArgs.put(field, innerArg);
            }
            writer.addDocument(IndexHelper.createDocument(multiArgs));
        }
    }

    writer.close();
}