Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit) throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryLoader.java

License:Apache License

void update(LexEntry entry) throws IOException {
    IndexWriter writer = initIndexWriter();
    Term queryTerm = new Term(LexEntry.ID, entry.getId());
    writer.deleteDocuments(queryTerm);/*w ww.  j  av  a  2 s . c o  m*/
    if (entry.getCurrent() != null) {
        List<Document> docs = createDocument(new HashSet<String>(), entry);
        for (Document document : docs) {
            writer.addDocument(document);
        }
    }
    writer.commit();
    writer.close();
    reader.close();
    reader = DirectoryReader.open(ram);
    searcher = new IndexSearcher(reader);
}

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryLoader.java

License:Apache License

void delete(LexEntry entry) throws IOException {
    IndexWriter writer = initIndexWriter();
    Term queryTerm = new Term(LexEntry.ID, entry.getId());
    writer.deleteDocuments(queryTerm);//from www  .ja  va  2 s  .com
    writer.commit();
    writer.close();
    reader.close();
    reader = DirectoryReader.open(ram);
    searcher = new IndexSearcher(reader);
}

From source file:de.uni_koeln.spinfo.textengineering.tm.classification.lucene.Searcher.java

License:Open Source License

public Searcher(String indexDir) throws IOException {
    /* Das Index-Verzeichnis: */
    Directory directory = new SimpleFSDirectory(new File(indexDir).toPath());
    /*// w  w  w  . jav  a 2  s .c  o  m
     * Der IndexSearcher ist im Wesentlichen ein Wrapper um einen Reader, der fr den Lese-Zugriff auf das
     * Index-Verzeichnis zustndig ist:
     */
    reader = DirectoryReader.open(directory);
    searcher = new IndexSearcher(reader);
}

From source file:de.walware.statet.r.internal.core.rhelp.index.REnvIndexReader.java

License:Open Source License

public REnvIndexReader(final IREnvConfiguration rEnvConfig) throws Exception {
    //      NIOFSDirectory doesn't like Thread#interrupt() used by the information hover manager
    //      final FSDirectory directory= FSDirectory.open(SaveUtil.getIndexDirectory(rEnvConfig));
    final FSDirectory directory = new SimpleFSDirectory(REnvIndexWriter.getIndexDirectory(rEnvConfig), null);
    this.indexReader = DirectoryReader.open(directory);
    this.indexSearcher = new IndexSearcher(this.indexReader);
    this.indexSearcher.setSimilarity(SIMILARITY);
}

From source file:de.walware.statet.r.internal.core.rhelp.index.REnvIndexWriter.java

License:Open Source License

public void beginBatch(final boolean reset) throws AbortIndexException {
    if (this.luceneWriter != null) {
        throw new IllegalStateException();
    }/*from  w  ww  . ja  v a  2 s . c o  m*/

    this.status = new MultiStatus(RCore.PLUGIN_ID, 0, "Indexing: '" + this.rEnvConfig.getName() + "'.", null); //$NON-NLS-1$ //$NON-NLS-2$
    this.status.add(new Status(IStatus.INFO, RCore.PLUGIN_ID,
            "Beginning batch (index directory= '" + this.indexDirectory.getAbsolutePath() + "')."));

    try {
        final RHelpManager rHelpManager = RCorePlugin.getDefault().getRHelpManager();
        this.indexLock = rHelpManager.getIndexLock(this.rEnvConfig.getReference());

        synchronized (this.indexLock) {
            this.reset = reset;
            this.luceneDirectory = new SimpleFSDirectory(this.indexDirectory);
            if (!reset) {
                final REnvHelp oldHelp = rHelpManager.getHelp(this.rEnvConfig.getReference());
                try (final IndexReader dirReader = DirectoryReader.open(this.luceneDirectory)) {
                    this.existingPackages = new HashMap<>(64);
                    TermsEnum termsEnum = null;
                    for (final AtomicReaderContext leave : dirReader.leaves()) {
                        final AtomicReader aReader = leave.reader();
                        final Terms terms = aReader.terms(PACKAGE_FIELD_NAME);
                        if (terms != null) {
                            termsEnum = terms.iterator(termsEnum);
                            BytesRef term;
                            while ((term = termsEnum.next()) != null) {
                                final String name = term.utf8ToString();
                                final IRPkgHelp pkgHelp = (oldHelp != null) ? oldHelp.getRPackage(name) : null;
                                this.existingPackages.put(name, pkgHelp);
                            }
                        }
                    }

                    final IndexWriterConfig config = createWriterConfig();
                    config.setOpenMode(OpenMode.CREATE_OR_APPEND);
                    this.luceneWriter = new IndexWriter(this.luceneDirectory, config);
                } catch (final IOException e) {
                    assert (this.luceneWriter == null);
                    // try again new
                } finally {
                    if (oldHelp != null) {
                        oldHelp.unlock();
                    }
                }
            }
            if (this.luceneWriter == null) {
                this.reset = true;
                this.existingPackages = new HashMap<>(0);

                final IndexWriterConfig config = createWriterConfig();
                config.setOpenMode(OpenMode.CREATE);
                this.luceneWriter = new IndexWriter(this.luceneDirectory, config);
            }
        }

        this.packages = new LinkedHashMap<>();
        this.keywordGroups = new LinkedHashMap<>();
    } catch (final IOException e) {
        throw new AbortIndexException(e);
    } catch (final OutOfMemoryError e) {
        throw new AbortIndexException(e);
    }
}

From source file:demo.facet.SimpleFacetsExample.java

License:Apache License

/** User runs a query and counts facets only without collecting the matching documents.*/
private List<FacetResult> facetsOnly() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    FacetsCollector fc = new FacetsCollector();

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    searcher.search(new MatchAllDocsQuery(), fc);

    // Retrieve results
    List<FacetResult> results = new ArrayList<FacetResult>();

    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);

    results.add(facets.getTopChildren(10, "Author"));
    results.add(facets.getTopChildren(10, "Publish Date"));

    indexReader.close();//w  w w.  ja  v  a  2 s  .  co  m
    taxoReader.close();

    return results;
}

From source file:demo.jaxrs.search.server.Catalog.java

License:Apache License

@GET
@Produces(MediaType.APPLICATION_JSON)/*from  w  ww.  j  a v  a  2 s .  com*/
public JsonArray getBooks() throws IOException {
    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final JsonArrayBuilder builder = Json.createArrayBuilder();

    try {
        final Query query = new MatchAllDocsQuery();

        for (final ScoreDoc scoreDoc : searcher.search(query, 1000).scoreDocs) {
            final DocumentStoredFieldVisitor fieldVisitor = new DocumentStoredFieldVisitor(
                    LuceneDocumentMetadata.SOURCE_FIELD);

            reader.document(scoreDoc.doc, fieldVisitor);
            builder.add(fieldVisitor.getDocument().getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue());
        }

        return builder.build();
    } finally {
        reader.close();
    }
}

From source file:demo.jaxrs.search.server.Catalog.java

License:Apache License

@GET
@Produces(MediaType.APPLICATION_JSON)// w w w . ja va 2  s.  co m
@CrossOriginResourceSharing(allowAllOrigins = true)
@Path("/search")
public Response findBook(@Context SearchContext searchContext, @Context final UriInfo uri) throws IOException {

    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final JsonArrayBuilder builder = Json.createArrayBuilder();

    try {
        visitor.reset();
        visitor.visit(searchContext.getCondition(SearchBean.class));

        final Query query = visitor.getQuery();
        if (query != null) {
            final TopDocs topDocs = searcher.search(query, 1000);
            for (final ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final Document document = reader.document(scoreDoc.doc);
                final String source = document.getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue();

                builder.add(Json.createObjectBuilder().add("source", source).add("score", scoreDoc.score).add(
                        "url", uri.getBaseUriBuilder().path(Catalog.class).path(source).build().toString()));
            }
        }

        return Response.ok(builder.build()).build();
    } finally {
        reader.close();
    }
}

From source file:demo.jaxrs.search.server.Catalog.java

License:Apache License

private boolean exists(final String source) throws IOException {
    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);

    try {/*from   w  w w . j av a  2  s .c o  m*/
        return searcher.search(new TermQuery(new Term(LuceneDocumentMetadata.SOURCE_FIELD, source)),
                1).totalHits > 0;
    } finally {
        reader.close();
    }
}

From source file:demo.jaxrs.search.server.Indexer.java

License:Apache License

public IndexReader getIndexReader() throws IOException {
    return DirectoryReader.open(directory);
}