Search in sources :

Example 1 with SearchResult

use of com.searchcode.app.dto.SearchResult in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndSvnPath.

public void testEndToEndSvnPath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndSvnTest");
    IndexSvnRepoJob indexSvnRepoJob = new IndexSvnRepoJob();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 2 with SearchResult

use of com.searchcode.app.dto.SearchResult in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndFilePath.

public void testEndToEndFilePath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndFileTest");
    IndexFileRepoJob indexFileRepoJob = new IndexFileRepoJob();
    // Index created files
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete everything
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 3 with SearchResult

use of com.searchcode.app.dto.SearchResult 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 4 with SearchResult

use of com.searchcode.app.dto.SearchResult in project searchcode-server by boyter.

the class TimeSearchRouteService method getTimeSearch.

public SearchResult getTimeSearch(Request request, Response response) {
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    TimeCodeSearcher cs = new TimeCodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    response.header("Content-Encoding", "gzip");
    if (request.queryParams().contains("q") == false || Values.EMPTYSTRING.equals(request.queryParams("q").trim())) {
        return null;
    }
    String query = request.queryParams("q").trim();
    int page = this.getPage(request);
    String[] repos;
    String[] langs;
    String[] owners;
    String[] year;
    String[] yearmonth;
    String[] yearmonthday;
    String[] revisions;
    String[] deleted;
    String reposFilter = Values.EMPTYSTRING;
    String langsFilter = Values.EMPTYSTRING;
    String ownersFilter = Values.EMPTYSTRING;
    String yearFilter = Values.EMPTYSTRING;
    String yearMonthFilter = Values.EMPTYSTRING;
    String yearMonthDayFilter = Values.EMPTYSTRING;
    String revisionsFilter = Values.EMPTYSTRING;
    String deletedFilter = Values.EMPTYSTRING;
    if (request.queryParams().contains("repo")) {
        repos = request.queryParamsValues("repo");
        reposFilter = getRepos(repos, reposFilter);
    }
    if (request.queryParams().contains("lan")) {
        langs = request.queryParamsValues("lan");
        langsFilter = getLanguages(langs, langsFilter);
    }
    if (request.queryParams().contains("own")) {
        owners = request.queryParamsValues("own");
        ownersFilter = getOwners(owners, ownersFilter);
    }
    if (request.queryParams().contains("year")) {
        year = request.queryParamsValues("year");
        yearFilter = this.getYears(year, yearFilter);
    }
    if (request.queryParams().contains("ym")) {
        yearmonth = request.queryParamsValues("ym");
        yearMonthFilter = this.getYearMonths(yearmonth, yearMonthFilter);
    }
    if (request.queryParams().contains("ymd")) {
        yearmonthday = request.queryParamsValues("ymd");
        yearMonthDayFilter = this.getYearMonthDays(yearmonthday, yearMonthDayFilter);
    }
    if (request.queryParams().contains("rev")) {
        revisions = request.queryParamsValues("rev");
        revisionsFilter = this.getRevisions(revisions, revisionsFilter);
    }
    if (request.queryParams().contains("del")) {
        deleted = request.queryParamsValues("del");
        deletedFilter = this.getDeleted(deleted, deletedFilter);
    }
    // split the query escape it and and it together
    String cleanQueryString = scl.formatQueryString(query);
    SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter + yearFilter + yearMonthFilter + yearMonthDayFilter + revisionsFilter + deletedFilter, page);
    searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
    searchResult.setQuery(query);
    this.getAltQueries(scl, query, searchResult);
    // Null out code as it isn't required and there is no point in bloating our ajax requests
    for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
        codeSearchResult.setCode(null);
    }
    return searchResult;
}
Also used : SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) Data(com.searchcode.app.dao.Data) TimeCodeSearcher(com.searchcode.app.service.TimeCodeSearcher) SearchResult(com.searchcode.app.dto.SearchResult)

Example 5 with SearchResult

use of com.searchcode.app.dto.SearchResult 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)

Aggregations

SearchResult (com.searchcode.app.dto.SearchResult)6 CodeResult (com.searchcode.app.dto.CodeResult)5 CodeSearcher (com.searchcode.app.service.CodeSearcher)5 Values (com.searchcode.app.config.Values)4 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)4 IndexGitRepoJob (com.searchcode.app.jobs.repository.IndexGitRepoJob)4 Singleton (com.searchcode.app.service.Singleton)4 Path (java.nio.file.Path)4 TestHelpers (com.searchcode.app.TestHelpers)3 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)3 IndexSvnRepoJob (com.searchcode.app.jobs.repository.IndexSvnRepoJob)3 CodeIndexer (com.searchcode.app.service.CodeIndexer)3 Properties (com.searchcode.app.util.Properties)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 CodeMatcher (com.searchcode.app.service.CodeMatcher)2 SearchcodeLib (com.searchcode.app.util.SearchcodeLib)2