use of org.elasticsearch.search.highlight.HighlightField in project stash-codesearch-plugin by palantir.
the class SearchServlet method searchHitToDataMap.
// Returns map view of search hits for soy templates
// Not sure this is actually safe at all. Jerry thought so.
@SuppressWarnings("unchecked")
private ImmutableMap<String, Object> searchHitToDataMap(SearchHit hit, // null iff no permission validation required
Map<String, Repository> repoMap, int maxPreviewLines, int maxMatchLines, ImmutableSet<String> noHighlight) {
ImmutableMap.Builder<String, Object> hitData = new ImmutableMap.Builder<String, Object>();
Map<String, Object> hitSource = hit.getSource();
String type = hit.getType();
hitData.put("type", type);
String project = getStringFromMap(hitSource, "project");
hitData.put("project", project);
String repository = getStringFromMap(hitSource, "repository");
hitData.put("repository", repository);
// Validate permissions & build hit data map
String repoId = project + "^" + repository;
Repository repoObject;
if (repoMap == null) {
// current user is system administrator
repoObject = repositoryServiceManager.getRepositoryService().getBySlug(project, repository);
} else {
// must validate against allowed repositories for non-administrators
repoObject = repoMap.get(repoId);
}
if (repoObject != null && repoObject.getProject().getKey().equals(project) && repoObject.getSlug().equals(repository)) {
// Generate refs array
ImmutableSortedSet<String> refSet;
try {
refSet = ImmutableSortedSet.copyOf((Iterable<String>) hitSource.get("refs"));
} catch (Exception e) {
log.warn("Invalid refs collection detected for element in {}/{}", project, repository, e);
return null;
}
if (refSet.isEmpty()) {
log.warn("Detected empty refs collection for element in {}/{}", project, repository);
return null;
}
hitData.put("refs", refSet);
// Human-readable labels
hitData.put("projectname", repoObject.getProject().getName()).put("repositoryname", repoObject.getName());
if (type.equals("commit")) {
hitData.put("hash", getStringFromMap(hitSource, "hash")).put("subject", getStringFromMap(hitSource, "subject")).put("body", getStringFromMap(hitSource, "body")).put("commitDate", getDateStringFromMap(hitSource, "commitdate")).put("authorName", getStringFromMap(hitSource, "authorname")).put("authorEmail", getStringFromMap(hitSource, "authoremail"));
} else if (type.equals("file")) {
HighlightField highlightField = hit.getHighlightFields().get("contents");
String path = getStringFromMap(hitSource, "path");
String primaryRef = "refs/heads/master";
if (!refSet.contains(primaryRef)) {
primaryRef = refSet.iterator().next();
}
String contents = getStringFromMap(hitSource, "contents");
SourceSearch searchedContents = SourceSearch.search(contents, highlightField, 1, maxPreviewLines, maxMatchLines);
String extension = getStringFromMap(hitSource, "extension");
hitData.put("path", path).put("blob", getStringFromMap(hitSource, "blob")).put("primaryRef", primaryRef).put("sourceLines", searchedContents.getJoinedLines()).put("sourceLineNums", searchedContents.getJoinedLineNums()).put("isPreview", searchedContents.isPreview()).put("shownLines", searchedContents.getLines().length).put("excessLines", searchedContents.getExcess()).put("extension", extension).put("noHighlight", noHighlight.contains(extension));
}
} else {
return null;
}
return hitData.build();
}
use of org.elasticsearch.search.highlight.HighlightField in project graylog2-server by Graylog2.
the class ResultMessage method setHighlightRanges.
public void setHighlightRanges(Map<String, HighlightField> highlightFields) {
if (!highlightFields.isEmpty()) {
highlightRanges = ArrayListMultimap.create();
for (Map.Entry<String, HighlightField> hlEntry : highlightFields.entrySet()) {
final HighlightField highlight = hlEntry.getValue();
final String s = highlight.fragments()[0].toString();
int pos = 0;
int cutChars = 0;
while (true) {
int startIdx = s.indexOf("<em>", pos);
if (startIdx == -1) {
break;
}
final int endIdx = s.indexOf("</em>", startIdx);
final Range<Integer> highlightPosition = Range.closedOpen(startIdx - cutChars, endIdx - cutChars - 4);
pos = endIdx;
cutChars += 9;
highlightRanges.put(hlEntry.getKey(), highlightPosition);
}
}
LOG.debug("Highlight positions for message {}: {}", message.getId(), highlightRanges);
}
}
Aggregations