use of org.finra.herd.model.api.xml.IndexSearchResultKey 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;
}
use of org.finra.herd.model.api.xml.IndexSearchResultKey in project herd by FINRAOS.
the class IndexSearchServiceTest method testIndexSearchWithResultTypeFilter.
@Test
public void testIndexSearchWithResultTypeFilter() {
// Create an index search key
final IndexSearchKey indexSearchKey = new IndexSearchKey();
// Create a tag key
final IndexSearchResultTypeKey resultTypeKey = new IndexSearchResultTypeKey(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
indexSearchKey.setIndexSearchResultTypeKey(resultTypeKey);
// Create an index search keys list and add the previously defined key to it
final List<IndexSearchKey> indexSearchKeys = Collections.singletonList(indexSearchKey);
// Create an index search filter with the keys previously defined
final IndexSearchFilter indexSearchFilter = new IndexSearchFilter(EXCLUSION_SEARCH_FILTER, indexSearchKeys);
List<IndexSearchFilter> indexSearchFilters = Collections.singletonList(indexSearchFilter);
// Create index search request
final IndexSearchRequest indexSearchRequest = new IndexSearchRequest(SEARCH_TERM, indexSearchFilters, null, false);
// Create a set of fields.
final Set<String> fields = Sets.newHashSet(FIELD_DISPLAY_NAME, FIELD_SHORT_DESCRIPTION);
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyBusinessObjectDefinition = new IndexSearchResultKey(null, new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME));
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyTag = new IndexSearchResultKey(new TagKey(TAG_TYPE, TAG_CODE), null);
// Create a new index search results
final IndexSearchResult indexSearchResultBusinessObjectDefinition = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name(), new SearchIndexKey(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), indexSearchResultKeyBusinessObjectDefinition, BDEF_DISPLAY_NAME, BDEF_SHORT_DESCRIPTION, null);
final IndexSearchResult indexSearchResultTag = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.TAG.name(), new SearchIndexKey(TAG_SEARCH_INDEX_NAME), indexSearchResultKeyTag, TAG_DISPLAY_NAME, TAG_DESCRIPTION, null);
// Create a list to contain the index search results
final List<IndexSearchResult> indexSearchResults = new ArrayList<>();
indexSearchResults.add(indexSearchResultBusinessObjectDefinition);
indexSearchResults.add(indexSearchResultTag);
// Construct an index search response
final IndexSearchResponse indexSearchResponse = new IndexSearchResponse(TOTAL_INDEX_SEARCH_RESULTS, indexSearchResults, null);
// Mock the call to the index search service
when(alternateKeyHelper.validateStringParameter("An", "index search result type", SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name())).thenReturn(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name())).thenReturn(SEARCH_INDEX_NAME);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name())).thenReturn(SEARCH_INDEX_NAME_2);
when(indexSearchDao.indexSearch(indexSearchRequest, fields, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2)).thenReturn(indexSearchResponse);
// Call the method under test.
IndexSearchResponse result = indexSearchService.indexSearch(indexSearchRequest, fields, NO_MATCH);
// Verify the external calls.
verify(alternateKeyHelper).validateStringParameter("An", "index search result type", SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexTypeDaoHelper).getSearchIndexTypeEntity(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name());
verify(indexSearchDao).indexSearch(indexSearchRequest, fields, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2);
verifyNoMoreInteractionsHelper();
// Validate the result.
assertEquals(indexSearchResponse, result);
}
use of org.finra.herd.model.api.xml.IndexSearchResultKey in project herd by FINRAOS.
the class IndexSearchServiceTest method testIndexSearchWithMatch.
@Test
public void testIndexSearchWithMatch() {
// Create a tag key.
TagKey tagKey = new TagKey(TAG_TYPE_CODE, TAG_CODE);
// Create an index search request.
final IndexSearchRequest indexSearchRequest = new IndexSearchRequest(SEARCH_TERM, ImmutableList.of(new IndexSearchFilter(EXCLUSION_SEARCH_FILTER, ImmutableList.of(new IndexSearchKey(tagKey, NO_INDEX_SEARCH_RESULT_TYPE_KEY))), new IndexSearchFilter(EXCLUSION_SEARCH_FILTER, ImmutableList.of(new IndexSearchKey(NO_TAG_KEY, new IndexSearchResultTypeKey(INDEX_SEARCH_RESULT_TYPE))))), Collections.singletonList(ElasticsearchHelper.TAG_FACET), ENABLE_HIT_HIGHLIGHTING);
// Create a set of fields.
final Set<String> fields = Sets.newHashSet(FIELD_DISPLAY_NAME, FIELD_SHORT_DESCRIPTION);
// Create a set of match fields.
final Set<String> match = Sets.newHashSet(MATCH_COLUMN);
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyBusinessObjectDefinition = new IndexSearchResultKey(null, new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME));
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyTag = new IndexSearchResultKey(new TagKey(TAG_TYPE, TAG_CODE), null);
// Create a new index search results
final IndexSearchResult indexSearchResultBusinessObjectDefinition = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name(), new SearchIndexKey(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), indexSearchResultKeyBusinessObjectDefinition, BDEF_DISPLAY_NAME, BDEF_SHORT_DESCRIPTION, null);
final IndexSearchResult indexSearchResultTag = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.TAG.name(), new SearchIndexKey(TAG_SEARCH_INDEX_NAME), indexSearchResultKeyTag, TAG_DISPLAY_NAME, TAG_DESCRIPTION, null);
// Create a list to contain the index search results
final List<IndexSearchResult> indexSearchResults = new ArrayList<>();
indexSearchResults.add(indexSearchResultBusinessObjectDefinition);
indexSearchResults.add(indexSearchResultTag);
// Construct an index search response
final IndexSearchResponse indexSearchResponse = new IndexSearchResponse(TOTAL_INDEX_SEARCH_RESULTS, indexSearchResults, null);
// Construct a search index entity
SearchIndexTypeEntity searchIndexTypeEntity = new SearchIndexTypeEntity();
searchIndexTypeEntity.setCode(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
// Create a tag type entity.
TagTypeEntity tagTypeEntity = new TagTypeEntity();
tagTypeEntity.setCode(tagKey.getTagTypeCode());
// Create a tag entity.
TagEntity tagEntity = new TagEntity();
tagEntity.setTagCode(tagKey.getTagCode());
tagEntity.setTagType(tagTypeEntity);
// Mock the call to the index search service
when(tagDaoHelper.getTagEntity(tagKey)).thenReturn(tagEntity);
when(alternateKeyHelper.validateStringParameter("An", "index search result type", INDEX_SEARCH_RESULT_TYPE)).thenReturn(INDEX_SEARCH_RESULT_TYPE);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name())).thenReturn(SEARCH_INDEX_NAME);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name())).thenReturn(SEARCH_INDEX_NAME_2);
when(indexSearchDao.indexSearch(indexSearchRequest, fields, match, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2)).thenReturn(indexSearchResponse);
// Call the method under test.
IndexSearchResponse result = indexSearchService.indexSearch(indexSearchRequest, fields, match);
// Verify the external calls.
verify(tagHelper).validateTagKey(tagKey);
verify(tagDaoHelper).getTagEntity(tagKey);
verify(alternateKeyHelper).validateStringParameter("An", "index search result type", INDEX_SEARCH_RESULT_TYPE);
verify(searchIndexTypeDaoHelper).getSearchIndexTypeEntity(INDEX_SEARCH_RESULT_TYPE);
verify(indexSearchDao).indexSearch(indexSearchRequest, fields, match, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2);
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name());
verifyNoMoreInteractionsHelper();
// Validate the result.
assertEquals(indexSearchResponse, result);
}
use of org.finra.herd.model.api.xml.IndexSearchResultKey in project herd by FINRAOS.
the class IndexSearchServiceTest method testIndexSearch.
@Test
public void testIndexSearch() {
// Create a tag key.
TagKey tagKey = new TagKey(TAG_TYPE_CODE, TAG_CODE);
// Create an index search request.
final IndexSearchRequest indexSearchRequest = new IndexSearchRequest(SEARCH_TERM, ImmutableList.of(new IndexSearchFilter(EXCLUSION_SEARCH_FILTER, ImmutableList.of(new IndexSearchKey(tagKey, NO_INDEX_SEARCH_RESULT_TYPE_KEY))), new IndexSearchFilter(EXCLUSION_SEARCH_FILTER, ImmutableList.of(new IndexSearchKey(NO_TAG_KEY, new IndexSearchResultTypeKey(INDEX_SEARCH_RESULT_TYPE))))), Collections.singletonList(ElasticsearchHelper.TAG_FACET), ENABLE_HIT_HIGHLIGHTING);
// Create a set of fields.
final Set<String> fields = Sets.newHashSet(FIELD_DISPLAY_NAME, FIELD_SHORT_DESCRIPTION);
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyBusinessObjectDefinition = new IndexSearchResultKey(null, new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME));
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyTag = new IndexSearchResultKey(new TagKey(TAG_TYPE, TAG_CODE), null);
// Create a new index search results
final IndexSearchResult indexSearchResultBusinessObjectDefinition = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name(), new SearchIndexKey(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), indexSearchResultKeyBusinessObjectDefinition, BDEF_DISPLAY_NAME, BDEF_SHORT_DESCRIPTION, null);
final IndexSearchResult indexSearchResultTag = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.TAG.name(), new SearchIndexKey(TAG_SEARCH_INDEX_NAME), indexSearchResultKeyTag, TAG_DISPLAY_NAME, TAG_DESCRIPTION, null);
// Create a list to contain the index search results
final List<IndexSearchResult> indexSearchResults = new ArrayList<>();
indexSearchResults.add(indexSearchResultBusinessObjectDefinition);
indexSearchResults.add(indexSearchResultTag);
// Construct an index search response
final IndexSearchResponse indexSearchResponse = new IndexSearchResponse(TOTAL_INDEX_SEARCH_RESULTS, indexSearchResults, null);
// Construct a search index entity
SearchIndexTypeEntity searchIndexTypeEntity = new SearchIndexTypeEntity();
searchIndexTypeEntity.setCode(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
// Create a tag type entity.
TagTypeEntity tagTypeEntity = new TagTypeEntity();
tagTypeEntity.setCode(tagKey.getTagTypeCode());
// Create a tag entity.
TagEntity tagEntity = new TagEntity();
tagEntity.setTagCode(tagKey.getTagCode());
tagEntity.setTagType(tagTypeEntity);
// Mock the call to the index search service
when(tagDaoHelper.getTagEntity(tagKey)).thenReturn(tagEntity);
when(alternateKeyHelper.validateStringParameter("An", "index search result type", INDEX_SEARCH_RESULT_TYPE)).thenReturn(INDEX_SEARCH_RESULT_TYPE);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name())).thenReturn(SEARCH_INDEX_NAME);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name())).thenReturn(SEARCH_INDEX_NAME_2);
when(indexSearchDao.indexSearch(indexSearchRequest, fields, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2)).thenReturn(indexSearchResponse);
// Call the method under test.
IndexSearchResponse result = indexSearchService.indexSearch(indexSearchRequest, fields, NO_MATCH);
// Verify the external calls.
verify(tagHelper).validateTagKey(tagKey);
verify(tagDaoHelper).getTagEntity(tagKey);
verify(alternateKeyHelper).validateStringParameter("An", "index search result type", INDEX_SEARCH_RESULT_TYPE);
verify(searchIndexTypeDaoHelper).getSearchIndexTypeEntity(INDEX_SEARCH_RESULT_TYPE);
verify(indexSearchDao).indexSearch(indexSearchRequest, fields, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2);
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name());
verifyNoMoreInteractionsHelper();
// Validate the result.
assertEquals(indexSearchResponse, result);
}
use of org.finra.herd.model.api.xml.IndexSearchResultKey in project herd by FINRAOS.
the class IndexSearchServiceTest method testIndexSearchMissingOptionalParameters.
@Test
public void testIndexSearchMissingOptionalParameters() {
// Create an index search request.
final IndexSearchRequest indexSearchRequest = new IndexSearchRequest(SEARCH_TERM, NO_INDEX_SEARCH_FILTERS, NO_INDEX_SEARCH_FACET_FIELDS, NO_ENABLE_HIT_HIGHLIGHTING);
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyBusinessObjectDefinition = new IndexSearchResultKey(null, new BusinessObjectDefinitionKey(NAMESPACE, BDEF_NAME));
// Create a new index search result key and populate it with a tag key
final IndexSearchResultKey indexSearchResultKeyTag = new IndexSearchResultKey(new TagKey(TAG_TYPE, TAG_CODE), null);
// Create a new index search results
final IndexSearchResult indexSearchResultBusinessObjectDefinition = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name(), new SearchIndexKey(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), indexSearchResultKeyBusinessObjectDefinition, BDEF_DISPLAY_NAME, BDEF_SHORT_DESCRIPTION, null);
final IndexSearchResult indexSearchResultTag = new IndexSearchResult(SearchIndexTypeEntity.SearchIndexTypes.TAG.name(), new SearchIndexKey(TAG_SEARCH_INDEX_NAME), indexSearchResultKeyTag, TAG_DISPLAY_NAME, TAG_DESCRIPTION, null);
// Create a list to contain the index search results
final List<IndexSearchResult> indexSearchResults = new ArrayList<>();
indexSearchResults.add(indexSearchResultBusinessObjectDefinition);
indexSearchResults.add(indexSearchResultTag);
// Construct an index search response
final IndexSearchResponse indexSearchResponse = new IndexSearchResponse(TOTAL_INDEX_SEARCH_RESULTS, indexSearchResults, null);
// Construct a search index entity
SearchIndexTypeEntity searchIndexTypeEntity = new SearchIndexTypeEntity();
searchIndexTypeEntity.setCode(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
// Mock the call to the index search service
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name())).thenReturn(SEARCH_INDEX_NAME);
when(searchIndexDaoHelper.getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name())).thenReturn(SEARCH_INDEX_NAME_2);
when(indexSearchDao.indexSearch(indexSearchRequest, NO_FIELDS, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2)).thenReturn(indexSearchResponse);
// Call the method under test.
IndexSearchResponse result = indexSearchService.indexSearch(indexSearchRequest, NO_FIELDS, NO_MATCH);
// Verify the external calls.
verify(indexSearchDao).indexSearch(indexSearchRequest, NO_FIELDS, NO_MATCH, SEARCH_INDEX_NAME, SEARCH_INDEX_NAME_2);
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name());
verify(searchIndexDaoHelper).getActiveSearchIndex(SearchIndexTypeEntity.SearchIndexTypes.TAG.name());
verifyNoMoreInteractionsHelper();
// Validate the result.
assertEquals(indexSearchResponse, result);
}
Aggregations