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

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

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes all open resources and releases the write lock.

Usage

From source file:com.gnizr.core.search.SearchIndexManager.java

License:Mozilla Public License

private void createEmptyIndexDirectory() {
    IndexWriter writer = null;
    try {//from   w  w  w . ja  v  a  2 s .c  o  m
        writer = new IndexWriter(indexDirectory, DocumentCreator.createDocumentAnalyzer(), true);
    } catch (Exception e) {
        logger.error("Unable to reset the search index. Path " + indexDirectory.toString(), e);
    } finally {
        if (writer != null) {
            try {
                writer.optimize();
                writer.close();
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
}

From source file:com.greplin.lucene.filter.PhraseFilterBenchmark.java

License:Apache License

public static void main(String[] argv) {
    Directory directory = new RAMDirectory();
    try {//from w w  w  .  j  a  v a 2s  .  c om
        IndexWriter writer = new IndexWriter(directory,
                new IndexWriterConfig(Version.LUCENE_32, new WhitespaceAnalyzer(Version.LUCENE_32)));
        int done = 0;
        for (int i = 0; i < NUMBER_OF_SEGMENTS; i++) {
            int remaining = NUMBER_OF_SEGMENTS - i;
            int numberOfDocs;
            if (remaining == 1) {
                numberOfDocs = TOTAL_DOCS - done;
            } else {
                numberOfDocs = RANDOM.nextInt(TOTAL_DOCS - done - remaining) + 1;
            }
            done += numberOfDocs;
            System.out.println("Segment #" + i + " has " + numberOfDocs + " docs");

            for (int d = 0; d < numberOfDocs; d++) {
                int wordCount = RANDOM.nextInt(WORDS_PER_DOC_DEVIATION * 2) + AVERAGE_WORDS_PER_DOC
                        - WORDS_PER_DOC_DEVIATION;
                Document doc = new Document();
                doc.add(new Field("f", Joiner.on(' ').join(words(wordCount)), Field.Store.YES,
                        Field.Index.ANALYZED));
                doc.add(new Field("second", RANDOM.nextInt(100) < SECOND_FIELD_MATCH_PERCENTAGE ? "yes" : "no",
                        Field.Store.NO, Field.Index.ANALYZED));
                writer.addDocument(doc);
            }
            writer.commit();
        }
        writer.close();

        IndexReader reader = IndexReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);

        String[][] queries = new String[TOTAL_QUERIES][];
        Term[][] terms = new Term[TOTAL_QUERIES][];

        for (int q = 0; q < TOTAL_QUERIES; q++) {
            queries[q] = words(WORDS_PER_QUERY[RANDOM.nextInt(WORDS_PER_QUERY.length)]);
            terms[q] = new Term[queries[q].length];
            for (int qw = 0; qw < queries[q].length; qw++) {
                terms[q][qw] = new Term(FIELD, queries[q][qw]);
            }
        }

        // Warm up.
        new PhraseFilter(FIELD, queries[0]).getDocIdSet(reader);

        for (int round = 0; round < ROUNDS; round++) {
            System.out.println();
            String name1 = "filter";
            String name2 = "query";

            long ms1 = 0, ms2 = 0;
            for (int step = 0; step < 2; step++) {
                System.gc();
                System.gc();
                System.gc();

                if (step == (round & 1)) {
                    long millis = System.currentTimeMillis();
                    long hits = 0;
                    for (String[] queryWords : queries) {
                        PhraseFilter pf = new PhraseFilter(
                                new FilterIntersectionProvider(TermsFilter.from(new Term("second", "yes"))),
                                FIELD, queryWords);
                        hits += searcher.search(new FilteredQuery(new MatchAllDocsQuery(), pf), 1).totalHits;
                    }
                    ms1 = System.currentTimeMillis() - millis;
                    System.out.println("Finished " + name1 + " in " + ms1 + "ms with " + hits + " hits");
                } else {
                    long millis = System.currentTimeMillis();
                    long hits = 0;
                    for (Term[] queryTerms : terms) {
                        PhraseQuery pq = new PhraseQuery();
                        for (Term term : queryTerms) {
                            pq.add(term);
                        }
                        Query query = BooleanQueryBuilder.builder()
                                .must(new TermQuery(new Term("second", "yes"))).must(pq).build();
                        hits += searcher.search(query, 1).totalHits;
                    }
                    ms2 = System.currentTimeMillis() - millis;
                    System.out.println("Finished " + name2 + " in " + ms2 + "ms with " + hits + " hits");
                }
            }
            System.out.println(name1 + " took " + (int) ((100.0 * ms1) / ms2) + "% as much time as " + name2);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.greplin.lucene.query.PredicateBonusQueryTest.java

License:Apache License

@Test
public void testBasics() throws Exception {
    IndexWriter writer = new IndexWriter(this.directory,
            new IndexWriterConfig(Version.LUCENE_35, new WhitespaceAnalyzer(Version.LUCENE_35)));
    writer.addDocument(new DocumentBuilder().add("value", "5").build());
    writer.close();

    IndexReader reader = IndexReader.open(this.directory);
    IndexSearcher searcher = new IndexSearcher(reader);

    Query query = new ConstantScoreQuery(new TermQuery(new Term("value", "5")));
    Assert.assertEquals(1.0, searcher.search(query, 1).getMaxScore(), 0.00001);

    Query noBonus = new PredicateBonusQuery(query, Predicates.NONE, 10.0f);
    Assert.assertEquals(1.0, searcher.search(noBonus, 1).getMaxScore(), 0.00001);

    Query bonus = new PredicateBonusQuery(query, Predicates.ALL, 100.0f);
    Assert.assertEquals(101.0, searcher.search(bonus, 1).getMaxScore(), 0.00001);

    Query noMatch = new TermQuery(new Term("value", "not5"));
    Assert.assertEquals(Double.NaN, searcher.search(noMatch, 1).getMaxScore(), 0.00001);

    Query noMatchNoBonus = new PredicateBonusQuery(noMatch, Predicates.NONE, 10.0f);
    Assert.assertEquals(Double.NaN, searcher.search(noMatchNoBonus, 1).getMaxScore(), 0.00001);

    Query noMatchIgnoresBonus = new PredicateBonusQuery(noMatch, Predicates.ALL, 100.0f);
    Assert.assertEquals(Double.NaN, searcher.search(noMatchIgnoresBonus, 1).getMaxScore(), 0.00001);
}

From source file:com.ibm.jaql.lang.expr.index.BuildLuceneFn.java

License:Apache License

@Override
public JsonValue eval(Context context) throws Exception {
    JsonRecord fd = (JsonRecord) exprs[1].eval(context);
    if (fd == null) {
        return null;
    }/*from   ww  w.j a  v  a  2s  .  c  o  m*/
    JsonString loc = (JsonString) fd.get(new JsonString("location"));
    if (loc == null) {
        return null;
    }
    Function keyFn = (Function) exprs[2].eval(context);
    if (keyFn == null) {
        return null;
    }
    Function valFn = (Function) exprs[3].eval(context);
    JsonIterator iter = exprs[0].iter(context);
    JsonValue[] fnArgs = new JsonValue[1];
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriter writer = new IndexWriter(loc.toString(), analyzer, true);
    ByteArrayOutputStream buf = null;
    DataOutputStream out = null;
    if (valFn != null) {
        buf = new ByteArrayOutputStream();
        out = new DataOutputStream(buf);
    }

    for (JsonValue value : iter) {
        fnArgs[0] = value;
        keyFn.setArguments(fnArgs);
        JsonIterator keyIter = keyFn.iter(context);
        Document doc = null;
        for (JsonValue key : keyIter) {
            JsonString jkey = (JsonString) key;
            if (doc == null) {
                doc = new Document();
            }
            doc.add(new Field("key", jkey.toString(), Store.NO, Index.UN_TOKENIZED)); // TODO: typed keys, store binary value
        }

        if (doc != null) {
            if (valFn != null) {
                valFn.setArguments(fnArgs);
                JsonIterator valIter = valFn.iter(context);
                for (JsonValue val : valIter) {
                    JsonRecord jrec = (JsonRecord) val;
                    for (Entry<JsonString, JsonValue> e : jrec) {
                        JsonString name = e.getKey();
                        JsonValue fval = e.getValue();
                        buf.reset();
                        serializer.write(out, fval);
                        out.flush();
                        byte[] bytes = buf.toByteArray();
                        doc.add(new Field(name.toString(), bytes, Store.COMPRESS));
                    }
                }
            }
            writer.addDocument(doc);
        }
    }

    writer.optimize();
    writer.close();
    return fd;
}

From source file:com.icdd.lucence.IndexFiles.java

License:Apache License

public void index(boolean mode) {
    boolean create = mode;
    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        logger.warn("Document directory '" + docDir + "'does not exist or  is not readable, "
                + "please check the path");
        System.exit(1);//from   w w  w  .  j a  va  2 s  .  co m
    }
    Date start = new Date();
    try {
        logger.warn("Indexing to directory '" + indexPath + "'...");
        Directory dir = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }
        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);
        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.ikon.analysis.SearchDemo.java

License:Open Source License

/**
 * Add documents//from   ww  w .j a  v  a 2s. com
 */
private static void add(Directory index, Analyzer analyzer, String str) throws IOException, ParseException {
    IndexWriterConfig config = new IndexWriterConfig(Config.LUCENE_VERSION, analyzer);
    IndexWriter w = new IndexWriter(index, config);
    Document doc = new Document();
    doc.add(new Field(DOC_FIELD, str, Field.Store.YES, Field.Index.ANALYZED));
    w.addDocument(doc);
    w.close();
}

From source file:com.impetus.kundera.index.LucandraIndexer.java

License:Apache License

@Override
public final void index(EntityMetadata metadata, Object object) {

    if (!metadata.isIndexable()) {
        return;/*  w w w . j  ava 2  s  . c  o  m*/
    }

    log.debug("Indexing @Entity[" + metadata.getEntityClazz().getName() + "] " + object);

    String indexName = metadata.getIndexName();

    Document document = new Document();
    Field luceneField;

    // index row
    try {
        String id = PropertyAccessorHelper.getId(object, metadata);
        luceneField = new Field(ENTITY_ID_FIELD, id, // adding class
                // namespace
                Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
        document.add(luceneField);

        // index namespace for unique deletion
        luceneField = new Field(KUNDERA_ID_FIELD, getKunderaId(metadata, id), // adding
                // class
                // namespace
                Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
        document.add(luceneField);

        // index entity class
        luceneField = new Field(ENTITY_CLASS_FIELD, metadata.getEntityClazz().getCanonicalName().toLowerCase(),
                Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
        document.add(luceneField);

        // index index name
        luceneField = new Field(ENTITY_INDEXNAME_FIELD, metadata.getIndexName(), Field.Store.YES,
                Field.Index.NOT_ANALYZED_NO_NORMS);
        document.add(luceneField);

    } catch (PropertyAccessException e) {
        throw new IllegalArgumentException("Id could not be read.");
    }

    // now index all indexable properties
    for (PropertyIndex index : metadata.getIndexProperties()) {

        java.lang.reflect.Field property = index.getProperty();
        String propertyName = index.getName();

        try {
            String value = PropertyAccessorHelper.getString(object, property).toString();
            luceneField = new Field(getCannonicalPropertyName(indexName, propertyName), value, Field.Store.NO,
                    Field.Index.ANALYZED);
            document.add(luceneField);
        } catch (PropertyAccessException e) {
            // TODO: do something with the exceptions
            // e.printStackTrace();
        }
    }

    // flush the indexes
    try {
        log.debug("Flushing to Lucandra: " + document);
        if (!metadata.getDBType().equals(DBType.CASSANDRA)) {
            IndexWriter w = getDefaultIndexWriter();
            w.addDocument(document, analyzer);
            w.optimize();
            w.commit();
            w.close();

        } else {
            getIndexWriter().addDocument(document, analyzer);
        }
    } catch (CorruptIndexException e) {
        throw new IndexingException(e.getMessage());
    } catch (IOException e) {
        throw new IndexingException(e.getMessage());
    }
}

From source file:com.intuit.tank.search.lucene.LuceneService.java

License:Open Source License

/**
 * Closes the writer after optimizing the writer. One must call the closeWriter after one is done performing
 * indexing on documents.//from  ww  w  . j  a  va 2 s. c o  m
 * 
 * @param writer
 */
private void closeWriter(IndexWriter writer) {
    try {
        writer.optimize();
        writer.close();
    } catch (Exception e1) {
        e1.printStackTrace();
        // throw new RuntimeException(e1);
    }
}

From source file:com.isa.basic.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 = null;//from   ww  w. j  a v a  2s. c  om
    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);
    }

    //    docsPath = Thread.currentThread().getContextClassLoader().getResource(docsPath).;
    //    indexPath = Thread.currentThread().getContextClassLoader().getResource(indexPath).toString();
    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.ivannotes.searchbee.SearchBee.java

License:Apache License

public final void doIndex(DataFetcher<T> df) throws CorruptIndexException, IOException {
    df.reset();/*ww w  . j ava2  s.c o  m*/
    IndexWriter idxWriter = getIndexWriter();
    int contiuousException = 0;
    try {
        while (df.hasMore()) {
            try {
                List<T> data = df.fetchData();
                for (T bean : data) {
                    Document doc = buildDocument(bean);
                    idxWriter.addDocument(doc);
                }

                idxWriter.commit();
                contiuousException = 0;
            } catch (Exception e) {
                contiuousException++;
                logger.error("build index error", e);
                if (contiuousException > 100) {
                    logger.error("build index exceed max continuous exception count(100), exit build.");
                    break;
                }
            }
        }
    } finally {
        if (null != idxWriter) {
            idxWriter.close();
        }
    }

}