Search in sources :

Example 1 with Highlight

use of org.finra.herd.model.api.xml.Highlight in project herd by FINRAOS.

the class IndexSearchDaoImpl method buildIndexSearchResults.

/**
 * Extracts and builds a list of {@link IndexSearchResult}s from a given {@link SearchResult}
 *
 * @param fields the specified fields to be included in the response
 * @param tagActiveIndex the name of the active tag index
 * @param bdefActiveIndex the name of the active business object definition index
 * @param searchResult the raw search result returned by the elasticsearch client
 * @param isHighlightingEnabled boolean which specifies if highlighting is requested or not
 *
 * @return A {@link List} of {@link IndexSearchResult} which represent the search response
 */
private List<IndexSearchResult> buildIndexSearchResults(Set<String> fields, String tagActiveIndex, String bdefActiveIndex, SearchResult searchResult, Boolean isHighlightingEnabled) {
    final Integer tagShortDescMaxLength = configurationHelper.getProperty(ConfigurationValue.TAG_SHORT_DESCRIPTION_LENGTH, Integer.class);
    final Integer businessObjectDefinitionShortDescMaxLength = configurationHelper.getProperty(ConfigurationValue.BUSINESS_OBJECT_DEFINITION_SHORT_DESCRIPTION_LENGTH, Integer.class);
    List<IndexSearchResult> indexSearchResults = new ArrayList<>();
    final List<SearchResult.Hit<Map, Void>> searchHitList = searchResult.getHits(Map.class);
    // For each indexSearch hit
    for (final SearchResult.Hit<Map, Void> hit : searchHitList) {
        // Get the source map from the indexSearch hit
        @SuppressWarnings("unchecked") final Map<String, Object> sourceMap = hit.source;
        // Get the index from which this result is from
        final String index = hit.index;
        // Create a new document to populate with the indexSearch results
        final IndexSearchResult indexSearchResult = new IndexSearchResult();
        // Populate the results
        indexSearchResult.setSearchIndexKey(new SearchIndexKey(index));
        if (fields.contains(DISPLAY_NAME_FIELD)) {
            indexSearchResult.setDisplayName((String) sourceMap.get(DISPLAY_NAME_SOURCE));
        }
        // Populate tag index specific key
        if (index.equals(tagActiveIndex)) {
            if (fields.contains(SHORT_DESCRIPTION_FIELD)) {
                indexSearchResult.setShortDescription(HerdStringUtils.getShortDescription((String) sourceMap.get(DESCRIPTION_SOURCE), tagShortDescMaxLength));
            }
            final TagKey tagKey = new TagKey();
            tagKey.setTagCode((String) sourceMap.get(TAG_CODE_SOURCE));
            tagKey.setTagTypeCode((String) ((Map) sourceMap.get(TAG_TYPE)).get(CODE));
            indexSearchResult.setIndexSearchResultType(SearchIndexTypeEntity.SearchIndexTypes.TAG.name());
            indexSearchResult.setIndexSearchResultKey(new IndexSearchResultKey(tagKey, null));
        } else // Populate business object definition key
        if (index.equals(bdefActiveIndex)) {
            if (fields.contains(SHORT_DESCRIPTION_FIELD)) {
                indexSearchResult.setShortDescription(HerdStringUtils.getShortDescription((String) sourceMap.get(DESCRIPTION_SOURCE), businessObjectDefinitionShortDescMaxLength));
            }
            final BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey();
            businessObjectDefinitionKey.setNamespace((String) ((Map) sourceMap.get(NAMESPACE)).get(CODE));
            businessObjectDefinitionKey.setBusinessObjectDefinitionName((String) sourceMap.get(NAME_SOURCE));
            indexSearchResult.setIndexSearchResultType(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
            indexSearchResult.setIndexSearchResultKey(new IndexSearchResultKey(null, businessObjectDefinitionKey));
        } else {
            throw new IllegalStateException(String.format("Search result index name \"%s\" does not match any of the active search indexes. tagActiveIndex=%s bdefActiveIndex=%s", index, tagActiveIndex, bdefActiveIndex));
        }
        if (BooleanUtils.isTrue(isHighlightingEnabled)) {
            // Fetch configured 'tag' values for highlighting
            String preTag = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_PRETAGS);
            String postTag = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_POSTTAGS);
            // Extract highlighted content from the search hit and clean html tags except the pre/post-tags as configured
            Highlight highlightedContent = extractHighlightedContent(hit, preTag, postTag);
            // Set highlighted content in the response element
            indexSearchResult.setHighlight(highlightedContent);
        }
        indexSearchResults.add(indexSearchResult);
    }
    return indexSearchResults;
}
Also used : Highlight(org.finra.herd.model.api.xml.Highlight) BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) ArrayList(java.util.ArrayList) IndexSearchResult(org.finra.herd.model.api.xml.IndexSearchResult) SearchResult(io.searchbox.core.SearchResult) IndexSearchResultKey(org.finra.herd.model.api.xml.IndexSearchResultKey) SearchHit(org.elasticsearch.search.SearchHit) SearchIndexKey(org.finra.herd.model.api.xml.SearchIndexKey) TagKey(org.finra.herd.model.api.xml.TagKey) IndexSearchResult(org.finra.herd.model.api.xml.IndexSearchResult) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with Highlight

use of org.finra.herd.model.api.xml.Highlight in project herd by FINRAOS.

the class IndexSearchDaoImpl method extractHighlightedContent.

/**
 * Extracts highlighted content from a given {@link SearchHit}
 *
 * @param searchHit a given {@link SearchHit} from the elasticsearch results
 * @param preTag the specified pre-tag for highlighting
 * @param postTag the specified post-tag for highlighting
 *
 * @return {@link Highlight} a cleaned highlighted content
 */
private Highlight extractHighlightedContent(SearchResult.Hit<Map, Void> searchHit, String preTag, String postTag) {
    Highlight highlightedContent = new Highlight();
    List<Field> highlightFields = new ArrayList<>();
    // make sure there is highlighted content in the search hit
    if (MapUtils.isNotEmpty(searchHit.highlight)) {
        Set<String> keySet = searchHit.highlight.keySet();
        for (String key : keySet) {
            Field field = new Field();
            // Extract the field-name
            field.setFieldName(key);
            List<String> cleanFragments = new ArrayList<>();
            // Extract fragments which have the highlighted content
            List<String> fragments = searchHit.highlight.get(key);
            for (String fragment : fragments) {
                cleanFragments.add(HerdStringUtils.stripHtml(fragment, preTag, postTag));
            }
            field.setFragments(cleanFragments);
            highlightFields.add(field);
        }
    }
    highlightedContent.setFields(highlightFields);
    return highlightedContent;
}
Also used : Field(org.finra.herd.model.api.xml.Field) Highlight(org.finra.herd.model.api.xml.Highlight) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 Highlight (org.finra.herd.model.api.xml.Highlight)2 SearchResult (io.searchbox.core.SearchResult)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SearchHit (org.elasticsearch.search.SearchHit)1 BusinessObjectDefinitionKey (org.finra.herd.model.api.xml.BusinessObjectDefinitionKey)1 Field (org.finra.herd.model.api.xml.Field)1 IndexSearchResult (org.finra.herd.model.api.xml.IndexSearchResult)1 IndexSearchResultKey (org.finra.herd.model.api.xml.IndexSearchResultKey)1 SearchIndexKey (org.finra.herd.model.api.xml.SearchIndexKey)1 TagKey (org.finra.herd.model.api.xml.TagKey)1