Search in sources :

Example 26 with Project

use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.

the class SearchHelper method prepareExec.

/**
 * Create the searcher to use w.r.t. 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>
 * </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 names. If empty, a no-project setup
 * is assumed (i.e. DATA_ROOT/index will be used instead of possible
 * multiple DATA_ROOT/$project/index). If the set contains projects
 * not known in the configuration or projects not yet indexed,
 * an error will be returned in {@link #errorMsg}.
 * @return this instance
 */
public SearchHelper prepareExec(SortedSet<String> projects) {
    if (redirect != null || errorMsg != null) {
        return this;
    }
    settingsHelper = null;
    // 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());
            reader = DirectoryReader.open(dir);
            searcher = new IndexSearcher(reader);
            closeOnDestroy = true;
        } else {
            // Check list of project names first to make sure all of them
            // are valid and indexed.
            closeOnDestroy = false;
            Set<String> invalidProjects = projects.stream().filter(proj -> (Project.getByName(proj) == null)).collect(Collectors.toSet());
            if (!invalidProjects.isEmpty()) {
                errorMsg = "Project list contains invalid projects: " + String.join(", ", invalidProjects);
                return this;
            }
            Set<Project> notIndexedProjects = projects.stream().map(Project::getByName).filter(proj -> !proj.isIndexed()).collect(Collectors.toSet());
            if (!notIndexedProjects.isEmpty()) {
                errorMsg = "Some of the projects to be searched are not indexed yet: " + String.join(", ", notIndexedProjects.stream().map(Project::getName).collect(Collectors.toSet()));
                return this;
            }
            // We use MultiReader even for single project. This should
            // not matter given that MultiReader is just a cheap wrapper
            // around set of IndexReader objects.
            reader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
            if (reader != null) {
                searcher = new IndexSearcher(reader);
            } else {
                errorMsg = "Failed to initialize search. Check the index";
                if (!projects.isEmpty()) {
                    errorMsg += " for projects: " + String.join(", ", projects);
                }
                return this;
            }
        }
        // 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 not found. Check the index";
        if (!projects.isEmpty()) {
            errorMsg += " for projects: " + String.join(", ", projects);
        }
        errorMsg += "; " + e.getMessage();
    } catch (IOException e) {
        errorMsg = e.getMessage();
    }
    return this;
}
Also used : SuperIndexSearcher(org.opengrok.indexer.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) HistoryContext(org.opengrok.indexer.search.context.HistoryContext) SortedSet(java.util.SortedSet) ScoreDoc(org.apache.lucene.search.ScoreDoc) Context(org.opengrok.indexer.search.context.Context) SettingsHelper(org.opengrok.indexer.search.SettingsHelper) MatchesUtils(org.apache.lucene.search.MatchesUtils) IndexableField(org.apache.lucene.index.IndexableField) Summarizer(org.opengrok.indexer.search.Summarizer) Term(org.apache.lucene.index.Term) Project(org.opengrok.indexer.configuration.Project) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) Document(org.apache.lucene.document.Document) Map(java.util.Map) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) Path(java.nio.file.Path) SuggestWord(org.apache.lucene.search.spell.SuggestWord) MatchesIterator(org.apache.lucene.search.MatchesIterator) Definitions(org.opengrok.indexer.analysis.Definitions) Sort(org.apache.lucene.search.Sort) DirectoryReader(org.apache.lucene.index.DirectoryReader) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker) Set(java.util.Set) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) IndexDatabase(org.opengrok.indexer.index.IndexDatabase) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) SuperIndexSearcher(org.opengrok.indexer.configuration.SuperIndexSearcher) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) IndexedSymlink(org.opengrok.indexer.index.IndexedSymlink) LoggerFactory(org.opengrok.indexer.logger.LoggerFactory) Pattern(java.util.regex.Pattern) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) ReaderUtil(org.apache.lucene.index.ReaderUtil) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggestMode(org.apache.lucene.search.spell.SuggestMode) Weight(org.apache.lucene.search.Weight) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) CompatibleAnalyser(org.opengrok.indexer.analysis.CompatibleAnalyser) FSDirectory(org.apache.lucene.store.FSDirectory) TopDocs(org.apache.lucene.search.TopDocs) AnalyzerGuru(org.opengrok.indexer.analysis.AnalyzerGuru) IOUtils(org.opengrok.indexer.util.IOUtils) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) IOException(java.io.IOException) File(java.io.File) ScoreMode(org.apache.lucene.search.ScoreMode) TermQuery(org.apache.lucene.search.TermQuery) Paths(java.nio.file.Paths) Matches(org.apache.lucene.search.Matches) FileNotFoundException(java.io.FileNotFoundException) FSDirectory(org.apache.lucene.store.FSDirectory) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException) Project(org.opengrok.indexer.configuration.Project) Sort(org.apache.lucene.search.Sort) ParseException(org.apache.lucene.queryparser.classic.ParseException) File(java.io.File) DirectSpellChecker(org.apache.lucene.search.spell.DirectSpellChecker)

Example 27 with Project

use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.

the class SuggesterServiceImpl method rebuild.

/**
 * {@inheritDoc}
 */
@Override
public void rebuild(final String project) {
    Project p = env.getProjects().get(project);
    if (p == null) {
        logger.log(Level.WARNING, "Cannot rebuild suggester because project for name {0} was not found", project);
        return;
    }
    if (!p.isIndexed()) {
        logger.log(Level.WARNING, "Cannot rebuild project {0} because it is not indexed yet", project);
        return;
    }
    lock.readLock().lock();
    try {
        if (suggester == null) {
            logger.log(Level.FINE, "Cannot rebuild {0} because suggester is not initialized", project);
            return;
        }
        suggester.rebuild(Collections.singleton(getNamedIndexDir(p)));
    } finally {
        lock.readLock().unlock();
    }
}
Also used : Project(org.opengrok.indexer.configuration.Project)

Example 28 with Project

use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.

the class ProjectHelperTestBase method createProject.

protected static Project createProject(int index, int number, boolean grouped, boolean allowed, boolean repository, List<RepositoryInfo> rps, Map<String, Project> prjs, Map<Project, List<RepositoryInfo>> map) {
    Project p = new Project((allowed ? "allowed_" : "") + (grouped ? "grouped_" : "ungrouped_") + (repository ? "repository" : "project") + "_" + index + "_" + number);
    prjs.put(p.getName(), p);
    p.setIndexed(true);
    return p;
}
Also used : Project(org.opengrok.indexer.configuration.Project)

Example 29 with Project

use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.

the class ProjectHelperExtendedTest method createProject.

protected static Project createProject(String name) {
    Project p = new Project(name);
    p.setIndexed(true);
    return p;
}
Also used : Project(org.opengrok.indexer.configuration.Project)

Example 30 with Project

use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.

the class ProjectHelperTest method testGetProjectsAllowedGroup.

/**
 * Test of getProjects method, of class ProjectHelper.
 */
@Test
public void testGetProjectsAllowedGroup() {
    for (Group g : RuntimeEnvironment.getInstance().getGroups()) {
        if (g.getName().startsWith("allowed_group_0")) {
            Set<Project> result = helper.getProjects(g);
            assertEquals(2, result.size());
            for (Project p : result) {
                assertTrue(p.getName().startsWith("allowed_"));
            }
        }
    }
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) Test(org.junit.jupiter.api.Test)

Aggregations

Project (org.opengrok.indexer.configuration.Project)88 Test (org.junit.jupiter.api.Test)42 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)27 File (java.io.File)22 Group (org.opengrok.indexer.configuration.Group)20 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)17 ArrayList (java.util.ArrayList)16 TreeSet (java.util.TreeSet)11 IOException (java.io.IOException)10 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)10 List (java.util.List)8 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)7 Path (jakarta.ws.rs.Path)7 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)7 Path (java.nio.file.Path)6 Map (java.util.Map)6 Paths (java.nio.file.Paths)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 MercurialRepositoryTest (org.opengrok.indexer.history.MercurialRepositoryTest)5