Search in sources :

Example 31 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method getIndexSearcher.

/**
     * Get IndexSearcher for given project.
     * Each IndexSearcher is born from a SearcherManager object. There is
     * one SearcherManager for every project.
     * This schema makes it possible to reuse IndexSearcher/IndexReader objects
     * so the heavy lifting (esp. system calls) performed in FSDirectory
     * and DirectoryReader happens only once for a project.
     * The caller has to make sure that the IndexSearcher is returned back
     * to the SearcherManager. This is done with returnIndexSearcher().
     * The return of the IndexSearcher should happen only after the search
     * result data are read fully.
     *
     * @param proj project
     * @return SearcherManager for given project
     */
public SuperIndexSearcher getIndexSearcher(String proj) throws IOException {
    SearcherManager mgr = searcherManagerMap.get(proj);
    SuperIndexSearcher searcher = null;
    if (mgr == null) {
        File indexDir = new File(getDataRootPath(), IndexDatabase.INDEX_DIR);
        try {
            Directory dir = FSDirectory.open(new File(indexDir, proj).toPath());
            mgr = new SearcherManager(dir, new ThreadpoolSearcherFactory());
            searcherManagerMap.put(proj, mgr);
            searcher = (SuperIndexSearcher) mgr.acquire();
            searcher.setSearcherManager(mgr);
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "cannot construct IndexSearcher for project " + proj, ex);
        }
    } else {
        searcher = (SuperIndexSearcher) mgr.acquire();
        searcher.setSearcherManager(mgr);
    }
    return searcher;
}
Also used : SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException) File(java.io.File) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 32 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project OpenGrok by OpenGrok.

the class SearchHelper method getSuggestions.

/**
     * If a search did not return a hit, one may use this method to obtain
     * suggestions for a new search.
     *
     * <p>
     * Parameters which should be populated/set at this time: <ul>
     * <li>{@link #projects}</li> <li>{@link #dataRoot}</li>
     * <li>{@link #builder}</li> </ul>
     *
     * @return a possible empty list of suggestions.
     */
public List<Suggestion> getSuggestions() {
    if (projects == null) {
        return new ArrayList<>(0);
    }
    String[] name;
    if (projects.isEmpty()) {
        name = new String[] { "/" };
    } else if (projects.size() == 1) {
        name = new String[] { projects.first() };
    } else {
        name = new String[projects.size()];
        int ii = 0;
        for (String proj : projects) {
            name[ii++] = proj;
        }
    }
    List<Suggestion> res = new ArrayList<>();
    List<String> dummy = new ArrayList<>();
    FSDirectory dir;
    IndexReader ir = null;
    Term t;
    for (String proj : name) {
        Suggestion s = new Suggestion(proj);
        try {
            if (!closeOnDestroy) {
                SuperIndexSearcher searcher = RuntimeEnvironment.getInstance().getIndexSearcher(proj);
                searcherList.add(searcher);
                ir = searcher.getIndexReader();
            } else {
                dir = FSDirectory.open(new File(indexDir, proj).toPath());
                ir = DirectoryReader.open(dir);
            }
            if (builder.getFreetext() != null && !builder.getFreetext().isEmpty()) {
                t = new Term(QueryBuilder.FULL, builder.getFreetext());
                getSuggestion(t, ir, dummy);
                s.freetext = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            if (builder.getRefs() != null && !builder.getRefs().isEmpty()) {
                t = new Term(QueryBuilder.REFS, builder.getRefs());
                getSuggestion(t, ir, dummy);
                s.refs = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            if (builder.getDefs() != null && !builder.getDefs().isEmpty()) {
                t = new Term(QueryBuilder.DEFS, builder.getDefs());
                getSuggestion(t, ir, dummy);
                s.defs = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            //TODO suggest also for path and history?
            if ((s.freetext != null && s.freetext.length > 0) || (s.defs != null && s.defs.length > 0) || (s.refs != null && s.refs.length > 0)) {
                res.add(s);
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Got exception while getting " + "spelling suggestions: ", e);
        } finally {
            if (ir != null && closeOnDestroy) {
                try {
                    ir.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.WARNING, "Got exception while " + "getting spelling suggestions: ", ex);
                }
            }
        }
    }
    return res;
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) ArrayList(java.util.ArrayList) IndexReader(org.apache.lucene.index.IndexReader) FSDirectory(org.apache.lucene.store.FSDirectory) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) File(java.io.File)

Example 33 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project zm-mailbox by Zimbra.

the class LuceneIndexRepair method rename.

private void rename(String from, String to) {
    File dir;
    if (directory instanceof LuceneDirectory) {
        dir = ((LuceneDirectory) directory).getDirectory();
    } else if (directory instanceof FSDirectory) {
        dir = ((FSDirectory) directory).getDirectory();
    } else {
        return;
    }
    File renameFrom = new File(dir, from);
    File renameTo = new File(dir, to);
    if (renameTo.exists()) {
        renameTo.delete();
    }
    renameFrom.renameTo(renameTo);
}
Also used : FSDirectory(org.apache.lucene.store.FSDirectory) File(java.io.File)

Example 34 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project geode by apache.

the class DumpDirectoryFilesIntegrationTest method shouldDumpReadableLuceneIndexFile.

@Test
public void shouldDumpReadableLuceneIndexFile() throws Exception {
    luceneService.createIndexFactory().setFields("title", "description").create(INDEX_NAME, REGION_NAME);
    Region region = createRegion(REGION_NAME, RegionShortcut.PARTITION);
    region.put(0, new TestObject("title 1", "hello world"));
    region.put(1 * 113, new TestObject("title 2", "this will not match"));
    region.put(2 * 113, new TestObject("title 3", "hello world"));
    region.put(3 * 113, new TestObject("hello world", "hello world"));
    InternalLuceneIndex index = (InternalLuceneIndex) luceneService.getIndex(INDEX_NAME, REGION_NAME);
    luceneService.waitUntilFlushed(INDEX_NAME, REGION_NAME, 60000, TimeUnit.MILLISECONDS);
    index.dumpFiles(diskDirRule.get().getAbsolutePath());
    // Find the directory for the first bucket
    File bucket0 = diskDirRule.get().listFiles(file -> file.getName().endsWith("_0"))[0];
    // Test that we can read the lucene index from the dump
    final FSDirectory directory = FSDirectory.open(bucket0.toPath());
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    final TopDocs results = searcher.search(new MatchAllDocsQuery(), 1000);
    assertEquals(4, results.totalHits);
}
Also used : InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) TopDocs(org.apache.lucene.search.TopDocs) DirectoryReader(org.apache.lucene.index.DirectoryReader) RegionShortcut(org.apache.geode.cache.RegionShortcut) Test(org.junit.Test) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Category(org.junit.experimental.categories.Category) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) LuceneTestUtilities(org.apache.geode.cache.lucene.test.LuceneTestUtilities) Region(org.apache.geode.cache.Region) LuceneIntegrationTest(org.apache.geode.cache.lucene.LuceneIntegrationTest) DiskDirRule(org.apache.geode.test.junit.rules.DiskDirRule) TestObject(org.apache.geode.cache.lucene.test.TestObject) Assert(org.junit.Assert) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest) FSDirectory(org.apache.lucene.store.FSDirectory) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) IndexReader(org.apache.lucene.index.IndexReader) Region(org.apache.geode.cache.Region) TestObject(org.apache.geode.cache.lucene.test.TestObject) FSDirectory(org.apache.lucene.store.FSDirectory) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) File(java.io.File) Test(org.junit.Test) LuceneIntegrationTest(org.apache.geode.cache.lucene.LuceneIntegrationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 35 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project jackrabbit-oak by apache.

the class FSDirectoryFactoryTest method reuseExistingDir.

@Test
public void reuseExistingDir() throws Exception {
    IndexDefinition defn = IndexDefinition.newBuilder(root, idx.getNodeState(), "/fooIndex").build();
    FSDirectoryFactory factory = new FSDirectoryFactory(temporaryFolder.getRoot());
    Directory dir = factory.newInstance(defn, idx, ":data", false);
    File fsDir1 = ((FSDirectory) dir).getDirectory();
    dir.close();
    Directory dir2 = factory.newInstance(defn, idx, ":data", false);
    File fsDir2 = ((FSDirectory) dir2).getDirectory();
    dir2.close();
    assertEquals(fsDir1, fsDir2);
    assertEquals(1, temporaryFolder.getRoot().list(DirectoryFileFilter.DIRECTORY).length);
}
Also used : IndexDefinition(org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition) FSDirectory(org.apache.lucene.store.FSDirectory) File(java.io.File) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) Test(org.junit.Test)

Aggregations

FSDirectory (org.apache.lucene.store.FSDirectory)43 File (java.io.File)18 Directory (org.apache.lucene.store.Directory)12 IOException (java.io.IOException)10 Path (java.nio.file.Path)10 IndexSearcher (org.apache.lucene.search.IndexSearcher)9 FileNotFoundException (java.io.FileNotFoundException)5 FileSystem (java.nio.file.FileSystem)5 Document (org.apache.lucene.document.Document)5 IndexReader (org.apache.lucene.index.IndexReader)5 MMapDirectory (org.apache.lucene.store.MMapDirectory)5 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)5 FilterDirectory (org.apache.lucene.store.FilterDirectory)4 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)4 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 Term (org.apache.lucene.index.Term)3 WindowsFS (org.apache.lucene.mockfile.WindowsFS)3 TermQuery (org.apache.lucene.search.TermQuery)3