Asad Iftikhar
Asad Iftikhar

Reputation: 717

Scroll for all records ElasticSearch C# Nest

Query to scroll at the matching records from the query

this is the query of nest in C# to get all the records from nest C# find many questions which can solve it by using different method linq method but i want to do this this way any suggestions help would be appreciated

string[] MERCHANTNO = MerchantId.Split(",");
var mustClause = new List<QueryContainer>();
var filterClause = new List<QueryContainer>();
var filters = new List<QueryContainer>();
filters.Add(new TermsQuery{
    Field = new Field("MERCHANTNO"),
    Terms = MERCHANTNO,
});

Logger.LogInformation(clsName, funcName, "Filter Clause is:", filters);

var SearchRequest = new SearchRequest<AcquirerDTO>(idxName) {
    Size       = 10000,
    SearchType = Elasticsearch.Net.SearchType.QueryThenFetch,
    Scroll     = "5m",
    Query = new BoolQuery { Must = filters }
};

var searchResponse = await _elasticClient.SearchAsync<AcquirerDTO>( SearchRequest );

Upvotes: 0

Views: 1144

Answers (1)

Asad Iftikhar
Asad Iftikhar

Reputation: 717

The code for Scroll all the Records you have in ElasticSearch is

Filter

  filters.Add(new TermsQuery {
                        Field = new Field("MERCHANTNO"),       >>> Value needs to be searched
                        Terms = MERCHANTNO,
                    });

Date Range Filter

                filterClause.Add(new DateRangeQuery {
                    Boost = 1.1,
                    Field = new Field("filedate"),
                    GreaterThanOrEqualTo = DateMath.Anchored(yesterday),
                    LessThanOrEqualTo = DateMath.Anchored(Today),
                    Format = "yyyy-MM-dd",
                    TimeZone = "+01:00"
                });
               

Search Request for scrolling

                var SearchRequest = new SearchRequest<AcquirerDTO>(idxName) {
                    From = 0,
                    Scroll = scrollTimeoutMinutes,
                    Size = scrollPageSize,
                    Query = new BoolQuery
                    {
                        Must = filters,
                        Filter = filterClause
                    }
                };
                var searchResponse = await _elasticClient.SearchAsync<AcquirerDTO>(SearchRequest);

                if (searchResponse.ApiCall.ResponseBodyInBytes != null) {
                    var requestJson = System.Text.Encoding.UTF8.GetString(searchResponse.ApiCall.RequestBodyInBytes);
                    var JsonFormatQuery = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(requestJson), Formatting.Indented);
                    
                }

This is the code for Scrolling all the results in kibana

       List<AcquirerDTO> results = new List<AcquirerDTO>();
                if (searchResponse.Documents.Any())
                    results.AddRange(searchResponse.Documents);

                string scrollid = searchResponse.ScrollId;
                bool isScrollSetHasData = true;

                while (isScrollSetHasData)
                {
                    ISearchResponse<AcquirerDTO> loopingResponse = _elasticClient.Scroll<AcquirerDTO>(scrollTimeoutMinutes, scrollid);
                    if (loopingResponse.IsValid)
                    {
                        results.AddRange(loopingResponse.Documents);
                        scrollid = loopingResponse.ScrollId;
                    }
                    isScrollSetHasData = loopingResponse.Documents.Any();
                }
                var records = results;

Upvotes: 1

Related Questions