Search in sources :

Example 1 with HighlightField

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();
}
Also used : Repository(com.atlassian.stash.repository.Repository) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FilteredQueryBuilder(org.elasticsearch.index.query.FilteredQueryBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HighlightField(org.elasticsearch.search.highlight.HighlightField) QueryBuilders.queryString(org.elasticsearch.index.query.QueryBuilders.queryString) ImmutableMap(com.google.common.collect.ImmutableMap) ServletException(javax.servlet.ServletException) AuthorisationException(com.atlassian.stash.exception.AuthorisationException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) IOException(java.io.IOException)

Example 2 with HighlightField

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);
    }
}
Also used : HighlightField(org.elasticsearch.search.highlight.HighlightField) Map(java.util.Map)

Aggregations

HighlightField (org.elasticsearch.search.highlight.HighlightField)2 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)1 Repository (com.atlassian.stash.repository.Repository)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)1 FilteredQueryBuilder (org.elasticsearch.index.query.FilteredQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryBuilders.queryString (org.elasticsearch.index.query.QueryBuilders.queryString)1 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)1