use of io.searchbox.core.SearchScroll in project herd by FINRAOS.
the class BusinessObjectDefinitionIndexSearchDaoImpl method scrollSearchResultsIntoBusinessObjectDefinitionDto.
/**
* Private method to handle scrolling through the results from the search request and adding them to a business object definition entity list.
*
* @param searchRequestBuilder the the search request to scroll through
* @param indexName the index name
* @return list of business object definition entities
*/
private List<BusinessObjectDefinitionIndexSearchResponseDto> scrollSearchResultsIntoBusinessObjectDefinitionDto(final SearchRequestBuilder searchRequestBuilder, String indexName) {
// Retrieve the search response
final Search.Builder searchBuilder = new Search.Builder(searchRequestBuilder.toString()).addIndex(indexName);
searchBuilder.setParameter(Parameters.SIZE, ELASTIC_SEARCH_SCROLL_PAGE_SIZE);
searchBuilder.setParameter(Parameters.SCROLL, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString());
JestResult jestResult = jestClientHelper.searchExecute(searchBuilder.build());
List<BusinessObjectDefinitionIndexSearchResponseDto> businessObjectDefinitionIndexSearchResponseDtoList = new ArrayList<>();
List<BusinessObjectDefinitionIndexSearchResponseDto> resultList = jestResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class);
while (resultList.size() != 0) {
businessObjectDefinitionIndexSearchResponseDtoList.addAll(resultList);
String scrollId = jestResult.getJsonObject().get(SCROLL_ID).getAsString();
SearchScroll scroll = new SearchScroll.Builder(scrollId, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()).build();
jestResult = jestClientHelper.searchScrollExecute(scroll);
resultList = jestResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class);
}
return businessObjectDefinitionIndexSearchResponseDtoList;
}
use of io.searchbox.core.SearchScroll in project herd by FINRAOS.
the class IndexFunctionsDaoImpl method getIdsInIndex.
/**
* The ids in index function will take as arguments the index name and the document type and will return a list of all the ids in the index.
*/
@Override
public final List<String> getIdsInIndex(String indexName, String documentType) {
// Create an array list for storing the ids
List<String> idList = new ArrayList<>();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// Create a search request and set the scroll time and scroll size
final SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(new ElasticsearchClientImpl(), SearchAction.INSTANCE);
searchRequestBuilder.setIndices(indexName).setTypes(documentType).setScroll(new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME)).setSize(ELASTIC_SEARCH_SCROLL_PAGE_SIZE).setSource(searchSourceBuilder);
// Retrieve the search response
final Search.Builder searchBuilder = new Search.Builder(searchRequestBuilder.toString()).addIndex(indexName);
searchBuilder.setParameter(Parameters.SIZE, ELASTIC_SEARCH_SCROLL_PAGE_SIZE);
searchBuilder.setParameter(Parameters.SCROLL, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString());
JestResult jestResult = jestClientHelper.searchExecute(searchBuilder.build());
// While there are hits available, page through the results and add them to the id list
while (jestResult.getSourceAsStringList().size() != 0) {
for (String jsonString : jestResult.getSourceAsStringList()) {
JsonElement root = new JsonParser().parse(jsonString);
idList.add(root.getAsJsonObject().get("id").getAsString());
}
String scrollId = jestResult.getJsonObject().get("_scroll_id").getAsString();
SearchScroll scroll = new SearchScroll.Builder(scrollId, new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME).toString()).build();
jestResult = jestClientHelper.searchScrollExecute(scroll);
}
return idList;
}
use of io.searchbox.core.SearchScroll in project herd by FINRAOS.
the class JestClientHelperTest method testSearchExecuteScroll.
@Test
public void testSearchExecuteScroll() throws Exception {
// Mock
SearchScroll search = mock(SearchScroll.class);
JestResult searchResult = mock(JestResult.class);
JestClient jestClient = mock(JestClient.class);
when(jestClientFactory.getJestClient()).thenReturn(jestClient);
when(jestClient.execute(search)).thenReturn(searchResult);
// Test
JestResult result = jestClientHelper.searchScrollExecute(search);
// Validate
assertThat(result, is(not(nullValue())));
// Verify
verify(jestClientFactory).getJestClient();
verify(jestClient).execute(search);
verifyNoMoreInteractions(createdMocks.toArray());
}
Aggregations