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:dcu.com.ie.patent.queryreduction.PatentMagdyQueryReduction.java

License:Apache License

public PatentMagdyQueryReduction(String indexDir, String model, int Nbr_Docs, int Nbr_Terms, int source)
        throws IOException, Exception {
    Directory dir = FSDirectory.open(new File(indexDir));
    searcher = new IndexSearcher(DirectoryReader.open(dir));
    this.model = model;
    this.Nbr_Docs = Nbr_Docs;
    this.Nbr_Terms = Nbr_Terms;
    this.source = source;
    if (source <= 0 || source == 4 || source == 6 || source > 7) {
        throw new Exception("Invalid source of expansion!");
    }/*from  w w  w .  j ava  2  s .  co  m*/
}

From source file:de.anycook.db.lucene.FulltextIndex.java

License:Open Source License

private boolean checkIfRecipeExists(String recipeName) throws IOException {
    try (IndexReader reader = DirectoryReader.open(index)) {
        IndexSearcher searcher = new IndexSearcher(reader);
        MultiFieldQueryParser.parse(new String[] { recipeName }, new String[] { "title" }, analyzer);
        Query query = new QueryParser("title", analyzer).parse(recipeName);
        TopDocs topdocs = searcher.search(query, 1);
        if (topdocs.totalHits > 0) {
            return true;
        }/*  w ww . j  a  v  a  2  s  .  com*/
    } catch (CorruptIndexException | ParseException e) {
        throw new IOException(e);
    }

    return false;
}

From source file:de.anycook.db.lucene.FulltextIndex.java

License:Open Source License

public Set<String> search(String q) throws IOException {
    Set<String> recipes = new LinkedHashSet<>();
    String fields[] = new String[] { "description", "steps" };
    logger.debug(String.format("searching for %s", q));

    try (IndexReader reader = DirectoryReader.open(index)) {
        int hitsPerPage = 1000;
        IndexSearcher searcher = new IndexSearcher(reader);
        Query query = new MultiFieldQueryParser(fields, analyzer).parse(q);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, null);
        searcher.search(query, collector);

        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        for (ScoreDoc hit : hits) {
            Document d = searcher.doc(hit.doc);
            recipes.add(d.get("title"));
        }//from  w w  w  .j  a va 2 s .c  om

    } catch (CorruptIndexException | ParseException e) {
        logger.error(e);
    }

    logger.debug(String.format("found %d results", recipes.size()));
    return recipes;
}

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   www .ja v  a 2 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  w w  w  .  j a  v a  2s . co 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();/* w ww .jav a  2 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  w w  w.  java2s  . c  o 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.SearchIndex.java

public SearchIndex(String pathToIndex, Language input_language) throws IOException {
    if (input_language.equals(DE))
        this.analyzer = new GermanAnalyzer();
    if (input_language.equals(EN))
        this.analyzer = new EnglishAnalyzer();
    this.language = input_language;
    this.reader = DirectoryReader.open(FSDirectory.open(Paths.get(pathToIndex)));
    this.searcher = new IndexSearcher(reader);

}

From source file:de.csw.linkgenerator.plugin.lucene.IndexUpdater.java

License:Open Source License

/**
 * Opens the index reader and searcher used for finding and deleting old versions of indexed
 * documents.//from   ww  w. j  a v a2  s.c  o m
 */
private synchronized void openSearcher() {
    try {
        this.reader = DirectoryReader.open(this.indexDir);
        this.searcher = new IndexSearcher(this.reader);
    } catch (IOException e) {
        LOG.error("error opening index searcher", e);
    }
}

From source file:de.csw.linkgenerator.plugin.lucene.LucenePlugin.java

License:Open Source License

/**
 * Creates an array of Searchers for a number of lucene indexes.
 * // w w  w .j ava 2 s  .  c o  m
 * @param indexDirs Comma separated list of Lucene index directories to create searchers for.
 * @return Array of searchers
 */
public IndexSearcher[] createSearchers(String indexDirs) throws Exception {
    String[] dirPaths = StringUtils.split(indexDirs, ",");
    NIOFSDirectory[] dirs = new NIOFSDirectory[dirPaths.length];
    for (int i = 0; i < dirPaths.length; i++) {
        dirs[i] = new NIOFSDirectory(new File(dirPaths[i]));
    }
    List<IndexSearcher> searchersList = new ArrayList<IndexSearcher>();
    for (int i = 0; i < dirs.length; i++) {
        try {
            if (!DirectoryReader.indexExists(dirs[i])) {
                // If there's no index there, create an empty one; otherwise the reader
                // constructor will throw an exception and fail to initialize
                IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_40, analyzer);
                new IndexWriter(dirs[i], conf).close();
            }
            DirectoryReader reader = DirectoryReader.open(dirs[i]);
            searchersList.add(new IndexSearcher(reader));
        } catch (IOException e) {
            LOG.error("cannot open index " + dirs[i], e);
        }
    }

    return searchersList.toArray(new IndexSearcher[searchersList.size()]);
}