Search in sources :

Example 1 with FSDirectory

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

the class IndexDatabase method getIndexReader.

/**
     * Get an indexReader for the Index database where a given file
     *
     * @param path the file to get the database for
     * @return The index database where the file should be located or null if it
     * cannot be located.
     */
public static IndexReader getIndexReader(String path) {
    IndexReader ret = null;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    File indexDir = new File(env.getDataRootFile(), INDEX_DIR);
    if (env.hasProjects()) {
        Project p = Project.getProject(path);
        if (p == null) {
            return null;
        }
        indexDir = new File(indexDir, p.getPath());
    }
    try {
        FSDirectory fdir = FSDirectory.open(indexDir.toPath(), NoLockFactory.INSTANCE);
        if (indexDir.exists() && DirectoryReader.indexExists(fdir)) {
            ret = DirectoryReader.open(fdir);
        }
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Failed to open index: {0}", indexDir.getAbsolutePath());
        LOGGER.log(Level.FINE, "Stack Trace: ", ex);
    }
    return ret;
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) IndexReader(org.apache.lucene.index.IndexReader) FSDirectory(org.apache.lucene.store.FSDirectory) File(java.io.File) HistoryException(org.opensolaris.opengrok.history.HistoryException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException)

Example 2 with FSDirectory

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

the class SearchHelper method prepareExec.

/**
     * Create the searcher to use wrt. to currently set parameters and the given
     * projects. Does not produce any {@link #redirect} link. It also does
     * nothing if {@link #redirect} or {@link #errorMsg} have a
     * none-{@code null} value.
     * <p>
     * Parameters which should be populated/set at this time: <ul>
     * <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
     * <li>{@link #order} (falls back to relevance if unset)</li>
     * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul>
     * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
     * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
     * </ul>
     *
     * @param projects project to use query. If empty, a no-project setup
     * is assumed (i.e. DATA_ROOT/index will be used instead of possible
     * multiple DATA_ROOT/$project/index).
     * @return this instance
     */
public SearchHelper prepareExec(SortedSet<String> projects) {
    if (redirect != null || errorMsg != null) {
        return this;
    }
    // the Query created by the QueryBuilder
    try {
        indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
        query = builder.build();
        if (projects == null) {
            errorMsg = "No project selected!";
            return this;
        }
        this.projects = projects;
        if (projects.isEmpty()) {
            // no project setup
            FSDirectory dir = FSDirectory.open(indexDir.toPath());
            searcher = new IndexSearcher(DirectoryReader.open(dir));
            closeOnDestroy = true;
        } else {
            // We use MultiReader even for single project. This should
            // not matter given that MultiReader is just a cheap wrapper
            // around set of IndexReader objects.
            closeOnDestroy = false;
            MultiReader multireader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
            if (multireader != null) {
                searcher = new IndexSearcher(multireader);
            } else {
                errorMsg = "Failed to initialize search. Check the index.";
            }
        }
        // Most probably they are not reused. SearcherLifetimeManager might help here.
        switch(order) {
            case LASTMODIFIED:
                sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true));
                break;
            case BY_PATH:
                sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING));
                break;
            default:
                sort = Sort.RELEVANCE;
                break;
        }
        checker = new DirectSpellChecker();
    } catch (ParseException e) {
        errorMsg = PARSE_ERROR_MSG + e.getMessage();
    } catch (FileNotFoundException e) {
        //          errorMsg = "Index database(s) not found: " + e.getMessage();
        errorMsg = "Index database(s) not found.";
    } catch (IOException e) {
        errorMsg = e.getMessage();
    }
    return this;
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) FileNotFoundException(java.io.FileNotFoundException) Sort(org.apache.lucene.search.Sort) FSDirectory(org.apache.lucene.store.FSDirectory) SortField(org.apache.lucene.search.SortField) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException) File(java.io.File) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker)

Example 3 with FSDirectory

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

the class SentenceSourceIndexer method main.

public static void main(String... args) throws Exception {
    if (args.length != 5) {
        System.out.println("Usage: " + SentenceSourceIndexer.class.getSimpleName() + " <dataFile...> <indexDir> <languageCode> <maxSentences> <indexPosTags>");
        System.out.println("\t<dataFiles> comma-separated list of a Wikipedia XML dump (*.xml) and/or Tatoeba files (tatoeba-*)");
        System.out.println("\t<indexDir> directory where Lucene index will be written to, existing index content will be removed");
        System.out.println("\t<languageCode> short code like en for English, de for German etc");
        System.out.println("\t<maxSentences> maximum number of sentences to be indexed, use 0 for no limit");
        System.out.println("\t<indexPosTags> 1 to also index POS tags (i.e. analyze text by LT), 0 to index only the plain text");
        System.exit(1);
    }
    List<String> dumpFilesNames = Arrays.asList(args[0].split(","));
    File indexDir = new File(args[1]);
    String languageCode = args[2];
    int maxSentences = Integer.parseInt(args[3]);
    Language language = Languages.getLanguageForShortCode(languageCode);
    if (maxSentences == 0) {
        System.out.println("Going to index contents from " + dumpFilesNames);
    } else {
        System.out.println("Going to index up to " + maxSentences + " sentences from " + dumpFilesNames);
    }
    System.out.println("Output index dir: " + indexDir);
    long start = System.currentTimeMillis();
    Analyzer analyzer;
    String indexPos = args[4];
    if (indexPos.equals("1")) {
        // this will use LanguageToolAnalyzer
        analyzer = null;
    } else if (indexPos.equals("0")) {
        analyzer = new StandardAnalyzer(new CharArraySet(Collections.emptyList(), false));
    } else {
        throw new IllegalArgumentException("Unknown value '" + indexPos + "' for indexPosTags parameter, use 0 or 1");
    }
    try (FSDirectory fsDirectory = FSDirectory.open(indexDir.toPath());
        SentenceSourceIndexer indexer = new SentenceSourceIndexer(fsDirectory, language, maxSentences, analyzer)) {
        try {
            indexer.run(dumpFilesNames, language);
        } catch (DocumentLimitReachedException e) {
            System.out.println("Sentence limit (" + e.getLimit() + ") reached, stopping indexing");
        } finally {
            indexer.writeMetaDocuments();
        }
        if (analyzer != null) {
            analyzer.close();
        }
    }
    long end = System.currentTimeMillis();
    float minutes = (end - start) / (float) (1000 * 60);
    System.out.printf("Indexing took %.2f minutes\n", minutes);
}
Also used : CharArraySet(org.apache.lucene.analysis.util.CharArraySet) FSDirectory(org.apache.lucene.store.FSDirectory) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Language(org.languagetool.Language) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) File(java.io.File)

Example 4 with FSDirectory

use of org.apache.lucene.store.FSDirectory in project jforum2 by rafaelsteil.

the class LuceneSettings method createIndexDirectory.

public void createIndexDirectory(String directoryPath) throws IOException {
    FSDirectory fsDir = FSDirectory.getDirectory(directoryPath);
    IndexWriter writer = new IndexWriter(fsDir, this.analyzer, true);
    writer.close();
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 5 with FSDirectory

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

the class SimilarWordFinder method createIndex.

private void createIndex(List<String> words, File indexDir) throws IOException {
    FSDirectory dir = FSDirectory.open(indexDir.toPath());
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
    System.out.println("Creating index...");
    int docs = 0;
    try (IndexWriter writer = new IndexWriter(dir, indexWriterConfig)) {
        for (String word : words) {
            Document doc = new Document();
            doc.add(new TextField("word", word, Field.Store.YES));
            writer.addDocument(doc);
            docs++;
        }
    }
    System.out.println("Index created: " + docs + " docs");
}
Also used : StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextField(org.apache.lucene.document.TextField) FSDirectory(org.apache.lucene.store.FSDirectory) Document(org.apache.lucene.document.Document)

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