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