Search in sources :

Example 6 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class CodeRouteService method literalSearch.

// This is very alpha and just for testing
// TODO improve or remove this
public Map<String, Object> literalSearch(Request request, Response response) {
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    Map<String, Object> map = new HashMap<>();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q")) {
        String query = request.queryParams("q").trim();
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " ");
        SearchResult searchResult = cs.search(query, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), altquery, false));
        map.put("searchValue", query);
        map.put("searchResult", searchResult);
        map.put("reposQueryString", "");
        map.put("langsQueryString", "");
        map.put("altQuery", "");
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        return map;
    }
    map.put("photoId", 1);
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return map;
}
Also used : Repo(com.searchcode.app.dao.Repo) CodeMatcher(com.searchcode.app.service.CodeMatcher) Data(com.searchcode.app.dao.Data) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 7 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class SearchRouteService method codeSearch.

public SearchResult codeSearch(Request request, Response response) {
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(Singleton.getData());
    SearchcodeLib scl = Singleton.getSearchcodeLib(Singleton.getData());
    if (request.queryParams().contains("q") && !request.queryParams("q").trim().equals(Values.EMPTYSTRING)) {
        String query = request.queryParams("q").trim();
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String[] repos;
        String[] langs;
        String[] owners;
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = Values.EMPTYSTRING;
        if (request.queryParams().contains("repo")) {
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                List<String> reposList = Arrays.asList(repos).stream().map((s) -> "reponame:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
            }
        }
        if (request.queryParams().contains("lan")) {
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                List<String> langsList = Arrays.asList(langs).stream().map((s) -> "languagename:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
            }
        }
        if (request.queryParams().contains("own")) {
            owners = request.queryParamsValues("own");
            if (owners.length != 0) {
                List<String> ownersList = Arrays.asList(owners).stream().map((s) -> "codeowner:" + QueryParser.escape(s.replace(" ", "_"))).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
            }
        }
        // split the query escape it and and it together
        String cleanQueryString = scl.formatQueryString(query);
        SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
        searchResult.setQuery(query);
        for (String altQuery : scl.generateAltQueries(query)) {
            searchResult.addAltQuery(altQuery);
        }
        // Null out code as it isnt required and there is no point in bloating our ajax requests
        for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
            codeSearchResult.setCode(null);
        }
        return searchResult;
    }
    return null;
}
Also used : Arrays(java.util.Arrays) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) List(java.util.List) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Request(spark.Request) Response(spark.Response) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 8 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class CodeRouteService method root.

public ModelAndView root(Request request, Response response) {
    Map<String, Object> map = new HashMap<>();
    Repo repo = Singleton.getRepo();
    Gson gson = new Gson();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q") && !request.queryParams("q").trim().equals("")) {
        String query = request.queryParams("q").trim();
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        List<String> reposList = new ArrayList<>();
        List<String> langsList = new ArrayList<>();
        List<String> ownsList = new ArrayList<>();
        if (request.queryParams().contains("repo")) {
            String[] repos;
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                reposList = Arrays.asList(repos);
            }
        }
        if (request.queryParams().contains("lan")) {
            String[] langs;
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                langsList = Arrays.asList(langs);
            }
        }
        if (request.queryParams().contains("own")) {
            String[] owns;
            owns = request.queryParamsValues("own");
            if (owns.length != 0) {
                ownsList = Arrays.asList(owns);
            }
        }
        map.put("searchValue", query);
        map.put("searchResultJson", gson.toJson(new CodePreload(query, page, langsList, reposList, ownsList)));
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new ModelAndView(map, "search_ajax.ftl");
    }
    CodeSearcher cs = new CodeSearcher();
    map.put("photoId", CommonRouteService.getPhotoId());
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return new ModelAndView(map, "index.ftl");
}
Also used : ModelAndView(spark.ModelAndView) Gson(com.google.gson.Gson) CodeSearcher(com.searchcode.app.service.CodeSearcher) Repo(com.searchcode.app.dao.Repo)

Example 9 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndGitDelta.

public void testEndToEndGitDelta() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    IndexGitRepoJob indexGitRepoJob = new IndexGitRepoJob();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndGitTest");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "init", ".");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "add", ".");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "commit", "-m", "\"First commit\"");
    // Clone from the above into a new directory
    File tempPath = TestHelpers.clearAndCreateTempPath("EndToEndGitCloneTest");
    this.runCommand(tempPath.toString(), this.GITPATH, "clone", directoryWithFiles.toString(), "EndToEndGitTest");
    // Index
    indexGitRepoJob.indexDocsByPath(Paths.get(tempPath.toString()), "EndToEndGitTest", "", tempPath.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    // Update the source
    TestHelpers.createFile(directoryWithFiles, "EndToEndTestFile4.cpp", "EndToEndTestFile EndToEndTestFile4");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "add", ".");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "commit", "-m", "\"Add new\"");
    // Index and lets dance
    RepositoryChanged repositoryChanged = indexGitRepoJob.updateExistingRepository("EndToEndGitTest", "repoRemoteLocation", "", "", tempPath.toString(), "", false);
    String repoGitLocation = tempPath.toString() + "/" + "EndToEndGitTest";
    Path docDir = Paths.get(repoGitLocation);
    indexGitRepoJob.indexDocsByDelta(docDir, "EndToEndGitTest", tempPath.toString(), "", repositoryChanged);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(4);
    // Update the source
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "rm", "EndToEndTestFile4.cpp");
    this.runCommand(directoryWithFiles.toString(), this.GITPATH, "commit", "-m", "\"Baleted\"");
    // Index and lets dance
    repositoryChanged = indexGitRepoJob.updateExistingRepository("EndToEndGitTest", "repoRemoteLocation", "", "", tempPath.toString(), "", false);
    repoGitLocation = tempPath.toString() + "/" + "EndToEndGitTest";
    docDir = Paths.get(repoGitLocation);
    indexGitRepoJob.indexDocsByDelta(docDir, "EndToEndGitTest", tempPath.toString(), "", repositoryChanged);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    Singleton.getCodeIndexer().deleteByReponame("EndToEndGitTest");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : Path(java.nio.file.Path) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob)

Example 10 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class IndexBaseAndGitRepoJobTest method testMissingPathFilesNoLocations.

public void testMissingPathFilesNoLocations() {
    IndexGitRepoJob gitRepoJob = new IndexGitRepoJob();
    CodeSearcher codeSearcherMock = Mockito.mock(CodeSearcher.class);
    when(codeSearcherMock.getRepoDocuments("testRepoName", 0)).thenReturn(new ArrayList<>());
    gitRepoJob.cleanMissingPathFiles(codeSearcherMock, "testRepoName", new HashMap<String, String>());
    verify(codeSearcherMock, times(1)).getRepoDocuments("testRepoName", 0);
}
Also used : CodeSearcher(com.searchcode.app.service.CodeSearcher) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob)

Aggregations

CodeSearcher (com.searchcode.app.service.CodeSearcher)11 IndexGitRepoJob (com.searchcode.app.jobs.repository.IndexGitRepoJob)6 Values (com.searchcode.app.config.Values)5 SearchResult (com.searchcode.app.dto.SearchResult)5 Singleton (com.searchcode.app.service.Singleton)5 CodeResult (com.searchcode.app.dto.CodeResult)4 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)4 Properties (com.searchcode.app.util.Properties)4 Path (java.nio.file.Path)4 TestHelpers (com.searchcode.app.TestHelpers)3 Repo (com.searchcode.app.dao.Repo)3 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)3 IndexSvnRepoJob (com.searchcode.app.jobs.repository.IndexSvnRepoJob)3 CodeIndexer (com.searchcode.app.service.CodeIndexer)3 CodeMatcher (com.searchcode.app.service.CodeMatcher)3 java.io (java.io)3 Paths (java.nio.file.Paths)3 TestCase (junit.framework.TestCase)3 FileUtils (org.apache.commons.io.FileUtils)3 AssertionsForInterfaceTypes.assertThat (org.assertj.core.api.AssertionsForInterfaceTypes.assertThat)3