Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
petercappelle
Partner - Contributor III
Partner - Contributor III

How to filter with Qlik Engine API

Hi,

About a month ago i asked on the community what to do best. Working with the Qlik Engine API or go the Capabilities way.

Against all advices (sorry for that) i went the Qlik Engine API way and made an AngularJS enviroment that communicates with Sense through the Qlik Engine API.

I pretty sure that what i have now is the way it has to be done, it's fast and works like a charm.

The only thing i don't understand is how i can "filter" inside of my app.

Imagine this request:

{

            "method": "CreateSessionObject",

            "handle": 1,

            "params": [

                {

                    "qInfo": {

                        "qType": "Chart"

                    },

                    "qHyperCubeDef": {

                        "qDimensions": [

                            {

                                "qLibraryId": "",

                                "qDef": {

                                    "qFieldDefs": [

                            "Year"

                          ]

                                }

                      }

                    ],

                        "qMeasures": [

                            {

                                "qLibraryId": "",

                                "qDef": {

                                    "qLabel": "Saldo",

                                    "qDef": "Sum(Saldo)"

                                }

                            }

                    ],

                        "qSuppressZero": false,

                        "qSuppressMissing": false

                    }

                }

              ]

}

Then i send my get request:

{

            "method": "GetHyperCubeData",

            "handle": 2,

            "params": [

    "/qHyperCubeDef",

    [

                    {

                        "qTop": 0,

                        "qLeft": 0,

                        "qHeight": 99,

                        "qWidth": 99

      }

    ]

  ]

}

That gives me the data i want, Sum(Saldo)/Years

But what if want to filter and i want to update that cube to just Year 2009 and 2010, Which request should i have to send?

Also pretend i have multiple cubes all with Year as a dimension, can i update the Year dimension of every cube?

1 Solution

Accepted Solutions
Alexander_Thor
Employee
Employee

That's awesome! Great to hear success stories around the Engine API

There are a few different ways, either you can define a Generic Object with a qListObjectDef that contains the Year field and you make your selections in that. Or you can use the Field class and select through that, for example Select method

Now the beauty with Qlik is that once you have selecting for example 2009 in the Year field all you would have to do is to call GetLayout on your other Generic Objects and they will automatically have filtered to reflect your selections in the app.

View solution in original post

8 Replies
Alexander_Thor
Employee
Employee

That's awesome! Great to hear success stories around the Engine API

There are a few different ways, either you can define a Generic Object with a qListObjectDef that contains the Year field and you make your selections in that. Or you can use the Field class and select through that, for example Select method

Now the beauty with Qlik is that once you have selecting for example 2009 in the Year field all you would have to do is to call GetLayout on your other Generic Objects and they will automatically have filtered to reflect your selections in the app.

petercappelle
Partner - Contributor III
Partner - Contributor III
Author

Hi Alexander en thx for your reply!

I don't know if this is a good practice but i always try to send requests through the dev-hub > Engine API Explorer first to see if what i get back from Qlik is the data i want.

There and with the Qlik dev help i found something i can use like this:

1. Make just a random object:

{

  "jsonrpc": "2.0",

  "method": "CreateObject",

  "handle": 1,

  "params": [

  {

  "qInfo": {

  "qId": "LB01",

  "qType": "ListObject"
  },

  "qListObjectDef": {

  "qDef": {

  "qFieldDefs": [

  "Country"
  ],

  "qFieldLabels": [

  "Country Label"
  ],

  "qSortCriterias": [

  {

  "qSortByLoadOrder": 1
  }

  ]

  },

  "qInitialDataFetch": [

  {

  "qTop": 0,

  "qLeft": 0,

  "qHeight": 3,

  "qWidth": 1
  }

  ]

  }

  }

  ]

}


It is clear for me that with this request i select the value on the second position of the previous response


{

    "jsonrpc": "2.0",

    "id": 4,

    "method":

    "SelectListObjectValues",

    "handle": 2,

    "params":

        [

            "/qListObjectDef",

            [ 1 ],

            false

        ]

}

So now when i do getLayout i got something like this:

"qMatrix": [

    [

        {

            "qText": "United States",

            "qNum": "NaN",

            "qElemNumber": 0,

            "qState": "X"

        }

    ],

    [

        {

            "qText": "Denmark",

            "qNum": "NaN",

            "qElemNumber": 1,

            "qState": "S"

        }

    ], ....

In fact if i do getLayout on my other cubes i see that the selection i made previously is extended to there to! So this is what i want!

Is this the way to go? Am i on the right track to succesfully filter?

If i am, then where can i find the parameters that i can apply in here "params": [ ... ]?

I have seem quiet some examples using:


    "params":

        [

            "/qListObjectDef",

            [ 1 ],

            false

        ]

or

    "params":

        [

            "/qListObjectDef",

            [ 0 ],

            [ 0 ],

            false

        ]

But is it possible to do something like this?


    "params":

        [

            "/qListObjectDef",

            [ "Denmark" ],

            false

        ]

Thanks again!

Peter

Øystein_Kolsrud
Employee
Employee

I would probably go with akls suggestion of going through the field class select method, but I just want to mention that it is also possible to do selections based on your hypercube date. You do that through methods in the GenericObject class just like you can for list objects (either SelectHyperCubeCells or SelectHyperCubeValues). If your GetHyperCubeData-call returned the following table:

YearSaldo
201140123
200939123
201245032
201037856

then you could call SelectHyperCubeCells on your session object and select the 2009 and 2010 values like this:

{

  "method": "SelectHyperCubeCells",

  "handle": 2,

  "params": [

    "/qHyperCubeDef",

    [ 0 ],

    [ 1, 3 ],

    True

  ]

}

This call will select row 1 and 3 from column 0, which means that the values 2009 and 2010 will be selected.

petercappelle
Partner - Contributor III
Partner - Contributor III
Author

thx yko for this example, Now i know how to use these index numbers.

I have another question... I am having problems with my web socket connection, mostly because i never used it before. I don't know if there is someone who has ever made an angular enviroment with web sockets and Qlik Sense? My question is, if i have to make a new web socket connection every time when i make a listObject or hyperCubeObject? Or do i have to make a run block and put the "new websocket" inside of that so that the connection opens before the rest of my code and stays open the whole time?

Alexander_Thor
Employee
Employee

Yes, you open the socket once and then you keep it open and send requests over the socket.

This is a shameless self-plug but you could use GitHub - mindspank/qsocks: A lightweight promise wrapper around the Qlik Sense Engine API that will simplify the communication for you. I know of several implementations that uses qsocks as a angular service to establish the communication layer and then pull in the established connection in their controllers.

Alexander_Thor
Employee
Employee

PS. You can check out this example Service Charges which uses the method described above built by a company called Axis Group.

petercappelle
Partner - Contributor III
Partner - Contributor III
Author

Thx for this example! To be honest i spend quiet some time looking to this example (service charges). I even tried to use qSocks (with luck!). I even to ask here on the community if it was a good aproach to use qSocks.

Unfortunately everyone tried to push me to the capabilities API and against anyone's advice i stucked with the engine API. We're not making an application just for ourself but for all of our customers so we have to look far enough and consider all the extras a customer could ask for.

Do you have a good understanding of AngularJS in combination with web sockets? If you do, could we get in touch through Skype ? We are urgently looking for some assistence with the app we are building. Since 5 year i'm doing web development jobs (shop websites and small web shops) but i'm not that pro in Javascript and AngularJS to just write a large BI application in a web enviroment.

Alexander_Thor
Employee
Employee

I'm not super experienced in AngularJS either but I would recommend you come join us in the Qlik Branch Slack channel. Once signed in to http://branch.qlik.com you'll find the invite url in the footer.

It's not a support channel but there are usually a lot of experienced devs online that has tackled similar issues before that should be able to point you in the right direction