Skip to content

Instantly share code, notes, and snippets.

@silashansen
Last active July 15, 2022 07:59
Show Gist options
  • Save silashansen/df42c7190a69772d0da33e7cc4a8dfd4 to your computer and use it in GitHub Desktop.
Save silashansen/df42c7190a69772d0da33e7cc4a8dfd4 to your computer and use it in GitHub Desktop.
ES Scroll Query Helper
public static void Main()
{
var esClient = new ElasticClient(new Uri("http://localhost:9200"));
var response = esClient
.Search<object>(s => s
.Index("sample_index")
.AllTypes()
.Scroll("1m"));
foreach (IHit<dynamic> item in response.ToEnumerable(esClient, "1m"))
{
Console.WriteLine(item.Source.Title);
}
}
namespace Cludo.ElasticSearch
{
public static class SearchResponseExtensions
{
public static IEnumerable<IHit<T>> ToEnumerable<T>(this ISearchResponse<T> searchResponse, IElasticClient client, Time scrollTime)
where T : class
{
return new ElasticsearchScrollIterator<T>(client, searchResponse, scrollTime);
}
}
public class ElasticsearchScrollIterator<T> : IEnumerable<IHit<T>> where T : class
{
private IElasticClient _client;
private ISearchResponse<T> _searchResponse;
private readonly Time _scrollTime;
public ElasticsearchScrollIterator(IElasticClient client, ISearchResponse<T> searchResponse, Time scrollTime)
{
_client = client;
_searchResponse = searchResponse;
_scrollTime = scrollTime;
}
public IEnumerator<IHit<T>> GetEnumerator()
{
while (_searchResponse.Documents.Any())
{
foreach (var hit in _searchResponse.Hits)
{
yield return hit;
}
_searchResponse = _client.Scroll<T>(_scrollTime, _searchResponse.ScrollId);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment