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.searchlocal.lucene.IndexBeanList.java

License:Open Source License

/**
 * ?/*w  w  w .  j a  v a2 s .  c o  m*/
 * 
 * @param namespace ?
 * @param doctype 
 * @param beanList 
 */
public static void makeindex(String namespace, String doctype, List beanList) {

    IndexWriterFactory factory = new IndexWriterFactory();

    IndexWriter indexWriter = factory.getWriter(namespace);
    try {
        if (Constant.FileNameClassify.EXCEL.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                ExcelFileBean bean = (ExcelFileBean) itera.next();
                makeExcelindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.WORD.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                WordFileBean bean = (WordFileBean) itera.next();
                makeWordindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.PDF.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                PdfFileBean bean = (PdfFileBean) itera.next();
                makePdfindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.PPT.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                PptFileBean bean = (PptFileBean) itera.next();
                makePptindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.CHM.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                ChmFileBean bean = (ChmFileBean) itera.next();
                makeChmindex(indexWriter, bean);
            }
            IndexWriterFactory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.HTML.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                HtmlFileBean bean = (HtmlFileBean) itera.next();
                makeHtmlindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }
        if (Constant.FileNameClassify.TXT.equals(doctype)) {
            for (Iterator itera = beanList.iterator(); itera.hasNext();) {
                TxtFileBean bean = (TxtFileBean) itera.next();
                makeTxtindex(indexWriter, bean);
            }
            factory.optimize(indexWriter);
        }

        // IndexWriter
        if (indexWriter != null) {
            indexWriter.commit();
            indexWriter.close();
            factory.removeIndexWriter(namespace);
        }

    } catch (InterruptedException e) {
        // TODO ??(I/O)
        e.printStackTrace();
    } catch (IOException e) {
        // TODO ??(I/O)
        e.printStackTrace();
    }
}

From source file:com.senseidb.abacus.api.codec.CodecTest.java

License:Apache License

static Directory buildIndex(Iterable<String> datasrc, Codec codec) throws Exception {
    String idxname = codec == null ? "lucene" : codec.getName();
    Directory dir = FSDirectory.open(new File("/tmp/codectest", idxname));//new RAMDirectory();
    //Directory dir = new RAMDirectory();
    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_44, new StandardAnalyzer(Version.LUCENE_44));
    conf.setUseCompoundFile(false);/*from w w  w. ja  v  a 2  s .c  o m*/
    if (codec != null) {
        conf.setCodec(codec);
    }

    IndexWriter writer = new IndexWriter(dir, conf);

    for (String doc : datasrc) {
        if (doc == null)
            break;
        doc = doc.trim();
        if (doc.length() == 0)
            continue;
        Document d = new Document();
        FieldType ft = new FieldType();
        ft.setIndexed(true);
        ft.setStored(false);
        ft.setIndexOptions(IndexOptions.DOCS_ONLY);
        ft.setOmitNorms(true);
        Field f = new Field(FIELD, doc, ft);
        d.add(f);
        writer.addDocument(d);
    }
    writer.forceMerge(1);
    writer.commit();
    writer.close();
    return dir;
}

From source file:com.senseidb.search.node.inmemory.InMemorySenseiService.java

License:Apache License

@SuppressWarnings("unchecked")
public SenseiResult doQuery(SenseiRequest senseiRequest, List<JSONObject> documents) {
    Directory directory = null;/* w  ww.j  a  va  2  s. c  om*/
    IndexWriter writer = null;
    try {
        directory = new RAMDirectory();
        writer = new IndexWriter(directory,
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        addDocuments(directory, writer, documents);
        ZoieIndexReader<BoboIndexReader> zoieMultiReader = new ZoieMultiReader<BoboIndexReader>(
                IndexReader.open(directory), new SenseiIndexReaderDecorator(facets, runtimeFacets));
        MockIndexReaderFactory mockIndexReaderFactory = new MockIndexReaderFactory<ZoieIndexReader<BoboIndexReader>>(
                Arrays.asList(zoieMultiReader));
        mockSenseiCore.setIndexReaderFactory(mockIndexReaderFactory);
        SenseiResult result = coreSenseiServiceImpl.execute(senseiRequest);
        mockSenseiCore.setIndexReaderFactory(null);
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
            if (directory != null) {
                directory.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.serendio.lingo3g.CreateLuceneIndex.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Args: index-dir");
        System.exit(-1);//from www  .  j  av a2  s .c  o m
    }

    File indexDir = new File(args[0]);
    if (indexDir.exists()) {
        System.out.println("Index directory already exists: " + indexDir.getAbsolutePath());
        System.exit(-2);
    }

    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir.toPath()), config);

    for (Document d : SampleDocumentData.DOCUMENTS_DATA_MINING) {
        final org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
        /*
         * We will create Lucene documents with searchable "fullContent" field and "title", 
         * "url" and "snippet" fields for clustering.
         */
        doc.add(new TextField("fullContent", d.getSummary(), Store.NO));

        doc.add(new TextField("title", d.getTitle(), Store.YES));
        doc.add(new TextField("snippet", d.getSummary(), Store.YES));
        doc.add(new StringField("url", d.getContentUrl(), Store.YES));
        writer.addDocument(doc);
    }

    writer.close();
}

From source file:com.sg.business.vault.index.demo.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" //$NON-NLS-1$
            + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n" //$NON-NLS-1$
            + "This indexes the documents in DOCS_PATH, creating a Lucene index" //$NON-NLS-1$
            + "in INDEX_PATH that can be searched with SearchFiles"; //$NON-NLS-1$
    String indexPath = "index"; //$NON-NLS-1$
    String docsPath = null;//from w w  w.  j  a v  a 2 s .  c om
    boolean create = true;
    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) { //$NON-NLS-1$
            indexPath = args[i + 1];
            i++;
        } else if ("-docs".equals(args[i])) { //$NON-NLS-1$
            docsPath = args[i + 1];
            i++;
        } else if ("-update".equals(args[i])) { //$NON-NLS-1$
            create = false;
        }
    }

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

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

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'..."); //$NON-NLS-1$ //$NON-NLS-2$

        Directory dir = FSDirectory.open(new File(indexPath));
        Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_44);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, 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"); //$NON-NLS-1$

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + //$NON-NLS-1$
                "\n with message: " + e.getMessage()); //$NON-NLS-1$
    }
}

From source file:com.shaie.annots.AnnotationSearchExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
    IndexWriter writer = new IndexWriter(dir, conf);

    // we need to add the annotation as a TokenStream field, therefore cannot use an Analyzer passed in the
    // IndexWriterConfig.
    Tokenizer tokenizer = new WhitespaceTokenizer();
    tokenizer.setReader(new StringReader("quick brown fox ate the blue red chicken"));
    TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer);
    TokenStream colorAnnotationStream = new AnnotatingTokenFilter(
            textStream.newSinkTokenStream(new ColorsSinkFilter()), COLOR_ANNOT_TERM);

    Document doc = new Document();
    doc.add(new TextField("text", textStream));
    doc.add(new TextField("annot", colorAnnotationStream));
    writer.addDocument(doc);/*from   w  w  w. ja  v a  2s  .  c o  m*/

    writer.close();

    DirectoryReader reader = DirectoryReader.open(dir);
    LeafReader ar = reader.leaves().get(0).reader(); // we only have one segment
    printFieldTerms(ar, "text");
    System.out.println();

    final ByteArrayDataInput in = new ByteArrayDataInput();
    PostingsEnum dape = ar.postings(new Term("annot", COLOR_ANNOT_TERM));
    int docID = dape.nextDoc();
    int freq = dape.freq();
    System.out.println("Color annotation spans: doc=" + docID + ", freq=" + freq);
    for (int i = 0; i < freq; i++) {
        dape.nextPosition();
        BytesRef payload = dape.getPayload();
        in.reset(payload.bytes, payload.offset, payload.length);
        System.out.println("  start=" + in.readVInt() + ", length=" + in.readVInt());
    }

    IndexSearcher searcher = new IndexSearcher(reader);

    System.out.println("\nsearching for 'red WITHIN color':");
    Query q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)),
            new SpanInclusivePositionTermQuery(new Term("text", "red")));
    TopDocs td = searcher.search(q, 10);
    System.out.println("  num results: " + td.scoreDocs.length);

    System.out.println("\nsearching for 'ate WITHIN color':");
    q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)),
            new SpanInclusivePositionTermQuery(new Term("text", "ate")));
    td = searcher.search(q, 10);
    System.out.println("  num results: " + td.scoreDocs.length);

    reader.close();
    dir.close();
}

From source file:com.shaie.annots.example.AnnotatorAnyExample.java

License:Apache License

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

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);//from   w w  w . jav  a  2  s. c  om

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);
    System.out.println();

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:"
            + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM));
    System.out.println();

    search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:red"));
    System.out.println();

    searchForRedAnimal(searcher);
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.AnnotatorTeeSinkFilterExample.java

License:Apache License

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

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);/*from w ww .  java 2  s.c  om*/

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:red"));
    System.out.println();

    search(searcher, qp.parse("animal:fox"));
    System.out.println();

    searchForBrownFox(searcher);
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:*"));
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:red"));
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.AnnotatorTokenFilterExample.java

License:Apache License

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

    addDocument(writer, "brown fox and a red dog");
    addDocument(writer, "only red dog");
    addDocument(writer, "no red animals here");
    writer.close();

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);/*from   w w w  . j  ava  2s  .c  o  m*/

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD);

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:red"));
    System.out.println();

    search(searcher, qp.parse("animal:fox"));
    System.out.println();

    searchForBrownFox(searcher);
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:*"));
    System.out.println();

    search(searcher, qp.parse("animal:* AND color:red"));
    System.out.println();

    reader.close();
}

From source file:com.shaie.annots.example.PreAnnotatedTokenFilterExample.java

License:Apache License

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

    addDocument(writer, "quick rosy brown fox and a pale violet red dog", 1, 2, 2, 1, 6, 3, 7, 1, 8, 1);
    addDocument(writer, "only red dog", 1, 1);
    addDocument(writer, "man with red pale face", 2, 1);
    writer.close();

    final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer);
    qp.setAllowLeadingWildcard(true);/* ww  w . j av  a 2 s.com*/

    final DirectoryReader reader = DirectoryReader.open(dir);
    final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment
    IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD);
    IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD);
    System.out.println();

    final IndexSearcher searcher = new IndexSearcher(reader);

    search(searcher, qp.parse("color:" + ANY_ANNOTATION_TERM));
    System.out.println();

    search(searcher, qp.parse("color:pale"));
    System.out.println();

    searchForColoredFox(searcher);
    System.out.println();

    reader.close();
}