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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations