Search in sources :

Example 1 with SearchScroll

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;
}
Also used : SearchScroll(io.searchbox.core.SearchScroll) Search(io.searchbox.core.Search) BusinessObjectDefinitionIndexSearchResponseDto(org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto) ArrayList(java.util.ArrayList) TimeValue(org.elasticsearch.common.unit.TimeValue) JestResult(io.searchbox.client.JestResult)

Example 2 with SearchScroll

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;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ArrayList(java.util.ArrayList) ElasticsearchClientImpl(org.finra.herd.dao.helper.ElasticsearchClientImpl) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchScroll(io.searchbox.core.SearchScroll) JsonElement(com.google.gson.JsonElement) Search(io.searchbox.core.Search) TimeValue(org.elasticsearch.common.unit.TimeValue) JestResult(io.searchbox.client.JestResult) JsonParser(com.google.gson.JsonParser)

Example 3 with SearchScroll

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());
}
Also used : SearchScroll(io.searchbox.core.SearchScroll) JestClient(io.searchbox.client.JestClient) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Aggregations

JestResult (io.searchbox.client.JestResult)3 SearchScroll (io.searchbox.core.SearchScroll)3 Search (io.searchbox.core.Search)2 ArrayList (java.util.ArrayList)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 JestClient (io.searchbox.client.JestClient)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 ElasticsearchClientImpl (org.finra.herd.dao.helper.ElasticsearchClientImpl)1 BusinessObjectDefinitionIndexSearchResponseDto (org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto)1 Test (org.junit.Test)1