Elasticsearch SearchSourceBuilder.size

I am using java High level client API to fetch data, when using searchsourcebuilder by default it takes default 10 documents but i have more than 1,00,000 documents for this i am using searchSourceBuilder.from() and searchSourceBuilder.size() but doesn't show more than 10,000 documents results what i need to do?

sample code:

   	 searchRequest.indices("winlogbeat-*");
   	 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
   	 searchSourceBuilder.query(QueryBuilders.matchAllQuery());
   	searchSourceBuilder.from(0); 
   	 searchSourceBuilder.size(10000); 
   	 searchRequest.source(searchSourceBuilder); 
   	 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
   	 SearchHits hits = searchResponse.getHits();
   		System.out.println("Hits:"+hits);
   		 
   		 List<Object> llist = new ArrayList<Object>();
   		 for (SearchHit hit :  searchResponse.getHits()) 
   		  {
   		     llist.add(hit.getSourceAsMap());
   		  }
     return llist;```

Read the bottom of this page: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html

I have used this scroll api in my code but it throws an ERROR where i have done mistake? is this code is correct?

		 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		 searchSourceBuilder.query(QueryBuilders.matchAllQuery());
		 searchSourceBuilder.size(10000); 
		 searchRequest.source(searchSourceBuilder);
		 searchRequest.scroll(TimeValue.timeValueMinutes(1L)); 
		 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
		 System.out.println("search"+searchResponse);
		 String scrollId = searchResponse.getScrollId(); 
		 SearchHits hits = searchResponse.getHits();
		 System.out.println("hits:"+hits);
		 SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); 
		 scrollRequest.scroll(TimeValue.timeValueSeconds(30));
		 SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
		 scrollId = searchScrollResponse.getScrollId();  
		 System.out.println("scrollId"+scrollId);
		 hits = searchScrollResponse.getHits();
                 return  searchScrollResponse;```

Which error?


org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=search_context_missing_exception, reason=No search context found for id [80]]
	at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509) ~[elasticsearch-6.4.3.jar:6.4.3]
	at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420) ~[elasticsearch-6.4.3.jar:6.4.3]```

The code looks overall correct to me.
Which line is failing?
I guess this is this one:

SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);

Could you print the value of this?

String scrollId = searchResponse.getScrollId();

I doesn't show any results? I can't understand what does this code doing?

I don't understand sorry.
May be reproduce a full example that you can share on Github then?

So we can just run the code.
If you do so, include also the creation of the index, creation of some data, then the search part...

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