Elasticsearch missing one record via High level rest api


(Ashutosh Tripathi) #1

Hi,
We are having trouble after moving to elasticsearch 5.6.x version from 5.0.x version. We always get one record less than the total number of records expected.
We can verify the missing record since same elastic json query when run by curl gives us correct output.

Anyone has idea about it ? I am attaching the code snippet below.

    String parameter = "search keyword";

    final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
    SearchRequest searchRequest = new SearchRequest(elasticIndex);
    searchRequest.types("typename");
    searchRequest.scroll(scroll);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    BoolQueryBuilder query = QueryBuilders.boolQuery();
    searchSourceBuilder.query(query);
    // searchSourceBuilder.from(0);
    // searchSourceBuilder.size(1);
    searchRequest.source(searchSourceBuilder);

    MultiMatchQueryBuilder.Type type = MultiMatchQueryBuilder.Type.CROSS_FIELDS;
    Operator operator = Operator.AND;

    MultiMatchQueryBuilder multiMatchQuery = 
    QueryBuilders.multiMatchQuery(parameter,"first_name","last_name");
    multiMatchQuery.operator(operator);
    multiMatchQuery.type(type);
    query.filter(multiMatchQuery);
   
    SearchResponse response = null;
    try {
        response = elasticService.getElasticClientRest().search(searchRequest);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String scrollId = response.getScrollId();
    SearchHit[] searchHits = response.getHits().getHits();
    System.out.println(searchSourceBuilder.toString());
    while (searchHits != null && searchHits.length > 0)  {
        SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
        scrollRequest.scroll(scroll);
        try {
            response = elasticService.getElasticClientRest().searchScroll(scrollRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        scrollId = response.getScrollId();
        searchHits = response.getHits().getHits();
        Arrays.stream(searchHits).forEach(o->{

            ObjectMapper mapper = new ObjectMapper();
            JsonNode actualObj = null;
            try{
                actualObj = mapper.readTree(o.getSourceAsString());
            }catch(Exception e){
                e.printStackTrace();
            }
            PropertiesDTO properties = Json.fromJson(actualObj, PropertiesDTO.class);
           //Results are processed
        });
     }

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.