Forum Discussion

KenvM's avatar
KenvM
Frequent Visitor
9 years ago
Solved

Getting Data from Elastic Stack with REST with Scroll

Hello,   I'm pulling data into Power BI using the Elastic Stack REST API, but I'm running into a problem when trying to pull more than 10k records.   In order to do so, I need to use the scroll f...
  • KenvM's avatar
    KenvM
    9 years ago

    Thanks for pointing me in the right direction.

     

    For anyone else that runs into this problem, here's the first iteration of a working function: 

     

    let
        RecursiveElasticFetch = (queryUrl, scrollUrl, scrollId, counter) =>
    
        let
            Counter = if (counter = null) then 0 else counter,
    
            Results = if (scrollId = null) then            
                Json.Document(Web.Contents(queryUrl))
            else
                Json.Document(Web.Contents(scrollUrl&scrollId, [Headers=[MyHeader=Text.From(Counter)]])),
    
            ParsedResults = Table.FromList(Results[hits][hits], Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    
            Return = if (Counter < 10) then
                ParsedResults & RecursiveElasticFetch(queryUrl, scrollUrl, Results[_scroll_id], Counter+1)
            else
                ParsedResults 
        in
            Return 
    in
        RecursiveElasticFetch

    For the parameters:

     

    queryUrl - The query URL that kicks off the search: http://elasticsrch-dev:9200/intranet*/_search?scroll=1m&source={....}

    scrollUrl - The URL used for subsequent searches (the scroll id is appended at the end): http://elasticsrch-dev:9200/_search/scroll?scroll=1m&scroll_id=

    scrollId - Used to pass the scroll_id to subsequent calls. Leave it blank for the first call

    counter - Used to limit the number of iterations. Leave it blank for the first call.