Search in sources :

Example 1 with Field

use of org.finra.herd.model.api.xml.Field 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)

Example 2 with Field

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

the class IndexSearchDaoImpl method buildHighlightQuery.

/**
 * Builds a {@link HighlightBuilder} based on (pre/post)tags and fields fetched from the DB config which is added to the main {@link SearchRequestBuilder}
 *
 * @param preTag The specified pre-tag to be used for highlighting
 * @param postTag The specified post-tag to be used for highlighting
 * @param match the set of match fields that are to be searched upon in the index search
 *
 * @return A configured {@link HighlightBuilder} object
 */
private HighlightBuilder buildHighlightQuery(String preTag, String postTag, Set<String> match) {
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    // Field matching is not needed since we are matching on multiple 'type' fields like stemmed and ngrams and enabling highlighting on all those fields
    // will yield duplicates
    highlightBuilder.requireFieldMatch(false);
    // Set the configured value for pre-tags for highlighting
    highlightBuilder.preTags(preTag);
    // Set the configured value for post-tags for highlighting
    highlightBuilder.postTags(postTag);
    // Get highlight fields value from configuration
    String highlightFieldsValue;
    // If the match column is included
    if (match != null && match.contains(MATCH_COLUMN)) {
        highlightFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_COLUMN_MATCH_HIGHLIGHT_FIELDS);
    } else {
        highlightFieldsValue = configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_FIELDS);
    }
    try {
        @SuppressWarnings("unchecked") IndexSearchHighlightFields highlightFieldsConfig = jsonHelper.unmarshallJsonToObject(IndexSearchHighlightFields.class, highlightFieldsValue);
        highlightFieldsConfig.getHighlightFields().forEach(highlightFieldConfig -> {
            // set the field name to the configured value
            HighlightBuilder.Field highlightField = new HighlightBuilder.Field(highlightFieldConfig.getFieldName());
            // set matched_fields to the configured list of fields, this accounts for 'multifields' that analyze the same string in different ways
            if (CollectionUtils.isNotEmpty(highlightFieldConfig.getMatchedFields())) {
                highlightField.matchedFields(highlightFieldConfig.getMatchedFields().toArray(new String[0]));
            }
            // set fragment size to the configured value
            if (highlightFieldConfig.getFragmentSize() != null) {
                highlightField.fragmentSize(highlightFieldConfig.getFragmentSize());
            }
            // set the number of desired fragments to the configured value
            if (highlightFieldConfig.getNumOfFragments() != null) {
                highlightField.numOfFragments(highlightFieldConfig.getNumOfFragments());
            }
            highlightBuilder.field(highlightField);
        });
    } catch (IOException e) {
        LOGGER.warn("Could not parse the configured value for highlight fields: {}", highlightFieldsValue, e);
    }
    return highlightBuilder;
}
Also used : Field(org.finra.herd.model.api.xml.Field) IndexSearchHighlightFields(org.finra.herd.model.dto.IndexSearchHighlightFields) IOException(java.io.IOException) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Aggregations

Field (org.finra.herd.model.api.xml.Field)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 Highlight (org.finra.herd.model.api.xml.Highlight)1 IndexSearchHighlightFields (org.finra.herd.model.dto.IndexSearchHighlightFields)1