use of com.gitblit.models.SearchResult in project gitblit by gitblit.
the class LuceneExecutorTest method testQuery.
@Test
public void testQuery() throws Exception {
// 2 occurrences on the master branch
Repository repository = GitBlitSuite.getHelloworldRepository();
RepositoryModel model = newRepositoryModel(repository);
repository.close();
List<SearchResult> results = lucene.search("ada", 1, 10, model.name);
assertEquals(2, results.size());
for (SearchResult res : results) {
assertEquals("refs/heads/master", res.branch);
}
// author test
results = lucene.search("author: tinogomes AND type:commit", 1, 10, model.name);
assertEquals(2, results.size());
// blob test
results = lucene.search("type: blob AND \"import std.stdio\"", 1, 10, model.name);
assertEquals(1, results.size());
assertEquals("d.D", results.get(0).path);
// commit test
repository = GitBlitSuite.getJGitRepository();
model = newRepositoryModel(repository);
repository.close();
results = lucene.search("\"initial jgit contribution to eclipse.org\"", 1, 10, model.name);
assertEquals(1, results.size());
assertEquals("Git Development Community", results.get(0).author);
assertEquals("1a6964c8274c50f0253db75f010d78ef0e739343", results.get(0).commitId);
assertEquals("refs/heads/master", results.get(0).branch);
// hash id tests
results = lucene.search("type:commit AND commit:1a6964c8274c50f0253db75f010d78ef0e739343", 1, 10, model.name);
assertEquals(1, results.size());
results = lucene.search("type:commit AND commit:1a6964c8274*", 1, 10, model.name);
assertEquals("Shawn O. Pearce", results.get(0).committer);
assertEquals(1, results.size());
// annotated tag test
results = lucene.search("I663208919f297836a9c16bf458e4a43ffaca4c12", 1, 10, model.name);
assertEquals(1, results.size());
assertEquals("[v1.3.0.201202151440-r]", results.get(0).tags.toString());
}
use of com.gitblit.models.SearchResult in project gitblit by gitblit.
the class LuceneExecutorTest method testMultiSearch.
@Test
public void testMultiSearch() throws Exception {
List<String> list = new ArrayList<String>();
Repository repository = GitBlitSuite.getHelloworldRepository();
list.add(newRepositoryModel(repository).name);
repository.close();
repository = GitBlitSuite.getJGitRepository();
list.add(newRepositoryModel(repository).name);
repository.close();
List<SearchResult> results = lucene.search("test", 1, 10, list);
assertEquals(10, results.size());
}
use of com.gitblit.models.SearchResult in project gitblit by gitblit.
the class LuceneExecutorTest method testIndex.
@Test
public void testIndex() {
// reindex helloworld
Repository repository = GitBlitSuite.getHelloworldRepository();
RepositoryModel model = newRepositoryModel(repository);
lucene.reindex(model, repository);
repository.close();
SearchResult result = lucene.search("type:blob AND path:bit.bit", 1, 1, model.name).get(0);
assertEquals("Mike Donaghy", result.author);
result = lucene.search("type:blob AND path:clipper.prg", 1, 1, model.name).get(0);
assertEquals("tinogomes", result.author);
// reindex JGit
repository = GitBlitSuite.getJGitRepository();
model = newRepositoryModel(repository);
lucene.reindex(model, repository);
repository.close();
}
use of com.gitblit.models.SearchResult in project gitblit by gitblit.
the class LuceneSearchPage method setup.
private void setup(PageParameters params) {
setupPage("", "");
// default values
ArrayList<String> repositories = new ArrayList<String>();
String query = "";
boolean allRepos = false;
int page = 1;
int pageSize = app().settings().getInteger(Keys.web.itemsPerPage, 50);
// display user-accessible selections
UserModel user = GitBlitWebSession.get().getUser();
List<String> availableRepositories = new ArrayList<String>();
for (RepositoryModel model : app().repositories().getRepositoryModels(user)) {
if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {
availableRepositories.add(model.name);
}
}
if (params != null) {
String repository = WicketUtils.getRepositoryName(params);
if (!StringUtils.isEmpty(repository)) {
repositories.add(repository);
}
page = WicketUtils.getPage(params);
if (params.containsKey("repositories")) {
String value = params.getString("repositories", "");
List<String> list = StringUtils.getStringsFromValue(value);
repositories.addAll(list);
}
allRepos = params.getAsBoolean("allrepos", false);
if (allRepos) {
repositories.addAll(availableRepositories);
}
if (params.containsKey("query")) {
query = params.getString("query", "");
} else {
String value = WicketUtils.getSearchString(params);
String type = WicketUtils.getSearchType(params);
com.gitblit.Constants.SearchType searchType = com.gitblit.Constants.SearchType.forName(type);
if (!StringUtils.isEmpty(value)) {
if (searchType == SearchType.COMMIT) {
query = "type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"";
} else {
query = searchType.name().toLowerCase() + ":\"" + value + "\"";
}
}
}
}
boolean luceneEnabled = app().settings().getBoolean(Keys.web.allowLuceneIndexing, true);
if (luceneEnabled) {
if (availableRepositories.size() == 0) {
info(getString("gb.noIndexedRepositoriesWarning"));
}
} else {
error(getString("gb.luceneDisabled"));
}
// enforce user-accessible repository selections
Set<String> uniqueRepositories = new LinkedHashSet<String>();
for (String selectedRepository : repositories) {
if (availableRepositories.contains(selectedRepository)) {
uniqueRepositories.add(selectedRepository);
}
}
ArrayList<String> searchRepositories = new ArrayList<String>(uniqueRepositories);
// search form
final Model<String> queryModel = new Model<String>(query);
final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(searchRepositories);
final Model<Boolean> allreposModel = new Model<Boolean>(allRepos);
SessionlessForm<Void> form = new SessionlessForm<Void>("searchForm", getClass()) {
private static final long serialVersionUID = 1L;
@Override
public void onSubmit() {
String q = queryModel.getObject();
if (StringUtils.isEmpty(q)) {
error(getString("gb.undefinedQueryWarning"));
return;
}
if (repositoriesModel.getObject().size() == 0 && !allreposModel.getObject()) {
error(getString("gb.noSelectedRepositoriesWarning"));
return;
}
PageParameters params = new PageParameters();
params.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
params.put("query", queryModel.getObject());
params.put("allrepos", allreposModel.getObject());
LuceneSearchPage page = new LuceneSearchPage(params);
setResponsePage(page);
}
};
ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", repositoriesModel, availableRepositories, new StringChoiceRenderer());
selections.setMaxRows(8);
form.add(selections.setEnabled(luceneEnabled));
form.add(new TextField<String>("query", queryModel).setEnabled(luceneEnabled));
form.add(new CheckBox("allrepos", allreposModel));
form.add(new ExternalLink("querySyntax", LUCENE_QUERY_SYNTAX_LINK));
add(form.setEnabled(luceneEnabled));
// execute search
final List<SearchResult> results = new ArrayList<SearchResult>();
if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
results.addAll(app().repositories().search(query, page, pageSize, searchRepositories));
}
// results header
if (results.size() == 0) {
if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
add(new Label("resultsHeader", query).setRenderBodyOnly(true));
add(new Label("resultsCount", getString("gb.noHits")).setRenderBodyOnly(true));
} else {
add(new Label("resultsHeader").setVisible(false));
add(new Label("resultsCount").setVisible(false));
}
} else {
add(new Label("resultsHeader", query).setRenderBodyOnly(true));
add(new Label("resultsCount", MessageFormat.format(getString("gb.queryResults"), results.get(0).hitId, results.get(results.size() - 1).hitId, results.get(0).totalHits)).setRenderBodyOnly(true));
}
// search results view
ListDataProvider<SearchResult> resultsDp = new ListDataProvider<SearchResult>(results);
final DataView<SearchResult> resultsView = new DataView<SearchResult>("searchResults", resultsDp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<SearchResult> item) {
final SearchResult sr = item.getModelObject();
switch(sr.type) {
case commit:
{
Label icon = WicketUtils.newIcon("type", "icon-refresh");
WicketUtils.setHtmlTooltip(icon, "commit");
item.add(icon);
item.add(new LinkPanel("summary", null, sr.summary, CommitPage.class, WicketUtils.newObjectParameter(sr.repository, sr.commitId)));
// show tags
Fragment fragment = new Fragment("tags", "tagsPanel", LuceneSearchPage.this);
List<String> tags = sr.tags;
if (tags == null) {
tags = new ArrayList<String>();
}
ListDataProvider<String> tagsDp = new ListDataProvider<String>(tags);
final DataView<String> tagsView = new DataView<String>("tag", tagsDp) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<String> item) {
String tag = item.getModelObject();
Component c = new LinkPanel("tagLink", null, tag, TagPage.class, WicketUtils.newObjectParameter(sr.repository, Constants.R_TAGS + tag));
WicketUtils.setCssClass(c, "tagRef");
item.add(c);
}
};
fragment.add(tagsView);
item.add(fragment);
break;
}
case blob:
{
Label icon = WicketUtils.newIcon("type", "icon-file");
WicketUtils.setHtmlTooltip(icon, "blob");
item.add(icon);
item.add(new LinkPanel("summary", null, sr.path, BlobPage.class, WicketUtils.newPathParameter(sr.repository, sr.branch, sr.path)));
item.add(new Label("tags").setVisible(false));
break;
}
}
item.add(new Label("fragment", sr.fragment).setEscapeModelStrings(false).setVisible(!StringUtils.isEmpty(sr.fragment)));
item.add(new LinkPanel("repository", null, sr.repository, SummaryPage.class, WicketUtils.newRepositoryParameter(sr.repository)));
if (StringUtils.isEmpty(sr.branch)) {
item.add(new Label("branch", "null"));
} else {
item.add(new LinkPanel("branch", "branch", StringUtils.getRelativePath(Constants.R_HEADS, sr.branch), LogPage.class, WicketUtils.newObjectParameter(sr.repository, sr.branch)));
}
item.add(new Label("author", sr.author));
item.add(WicketUtils.createDatestampLabel("date", sr.date, getTimeZone(), getTimeUtils()));
}
};
add(resultsView.setVisible(results.size() > 0));
PageParameters pagerParams = new PageParameters();
pagerParams.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
pagerParams.put("query", queryModel.getObject());
boolean showPager = false;
int totalPages = 0;
if (results.size() > 0) {
totalPages = (results.get(0).totalHits / pageSize) + (results.get(0).totalHits % pageSize > 0 ? 1 : 0);
showPager = results.get(0).totalHits > pageSize;
}
add(new PagerPanel("topPager", page, totalPages, LuceneSearchPage.class, pagerParams).setVisible(showPager));
add(new PagerPanel("bottomPager", page, totalPages, LuceneSearchPage.class, pagerParams).setVisible(showPager));
}
use of com.gitblit.models.SearchResult in project gitblit by gitblit.
the class LuceneService method createSearchResult.
private SearchResult createSearchResult(Document doc, float score, int hitId, int totalHits) throws ParseException {
SearchResult result = new SearchResult();
result.hitId = hitId;
result.totalHits = totalHits;
result.score = score;
result.date = DateTools.stringToDate(doc.get(FIELD_DATE));
result.summary = doc.get(FIELD_SUMMARY);
result.author = doc.get(FIELD_AUTHOR);
result.committer = doc.get(FIELD_COMMITTER);
result.type = SearchObjectType.fromName(doc.get(FIELD_OBJECT_TYPE));
result.branch = doc.get(FIELD_BRANCH);
result.commitId = doc.get(FIELD_COMMIT);
result.path = doc.get(FIELD_PATH);
if (doc.get(FIELD_TAG) != null) {
result.tags = StringUtils.getStringsFromValue(doc.get(FIELD_TAG));
}
return result;
}
Aggregations