示例#1
0
        public async Task <IList <IHit <T> > > GetScroll <T>(string indexName, string scrollTimeoutMinutes = "2m", int scrollPageSize = 1000) where T : class
        {
            var searchResponse = await client.SearchAsync <T>(sd => sd
                                                              .Index(indexName)
                                                              .From(0)
                                                              .Take(scrollPageSize)
                                                              .MatchAll()
                                                              .Scroll(scrollTimeoutMinutes));

            var results = new List <IHit <T> >();

            while (true)
            {
                if (!searchResponse.IsValid || string.IsNullOrEmpty(searchResponse.ScrollId))
                {
                    throw new Exception($"Search error: {searchResponse.ServerError.Error.Reason}");
                }

                if (!searchResponse.Documents.Any())
                {
                    break;
                }

                results.AddRange(searchResponse.Hits);
                searchResponse = await client.ScrollAsync <T>(scrollTimeoutMinutes, searchResponse.ScrollId);
            }

            await client.ClearScrollAsync(new ClearScrollRequest(searchResponse.ScrollId));

            return(results);
        }
示例#2
0
        public async Task <ConcurrentBag <IHit <T> > > BackupAndScroll <T>(string _default_index, QueryContainer query, SourceFilter so, string scroll_timeout = "5m", int scroll_pageize = 2000) where T : class
        {
            if (query == null)
            {
                query = new MatchAllQuery();
            }
            if (so == null)
            {
                so = new SourceFilter()
                {
                }
            }
            ;
            ConcurrentBag <IHit <T> > bag = new ConcurrentBag <IHit <T> >();

            try
            {
                var searchResponse = await client.SearchAsync <T>(sd => sd.Source(s => so).Index(_default_index).From(0).Take(scroll_pageize).Query(q => query).Scroll(scroll_timeout));

                List <Task> lst_task = new List <Task>();
                while (true)
                {
                    if (!searchResponse.IsValid || string.IsNullOrEmpty(searchResponse.ScrollId))
                    {
                        break;
                    }

                    if (!searchResponse.Documents.Any())
                    {
                        break;
                    }

                    var tmp = searchResponse.Hits;
                    foreach (var item in tmp)
                    {
                        bag.Add(item);
                    }
                    searchResponse = await client.ScrollAsync <T>(scroll_timeout, searchResponse.ScrollId);
                }

                await client.ClearScrollAsync(new ClearScrollRequest(searchResponse.ScrollId));

                await Task.WhenAll(lst_task);
            }
            catch (Exception)
            {
            }
            finally
            {
            }
            return(bag);
        }