Forum Discussion

snaveen's avatar
snaveen
Guide
3 years ago

Batch Multiple queries Help

 

Hi Team,

I have the below queries, i want to run these 2 multiple calls to Community API v2 in a single request using Batch Multiple requests.

Kindly help me with how to combine test 2 queries in Batch requests

SELECT * FROM floated_messages WHERE message.board.id = 'VxRail' and scope = 'global'

(https://www.dell.com/community/s/api/2.0/search?q=SELECT%20*%20FROM%20floated_messages%20WHERE%20message.board.id%20=%20%27VxRail%27%20and%20scope%20=%20%27global%27)

 

SELECT * FROM messages WHERE board.id = 'vxrail' and id='8122626'

(https://www.dell.com/community/s/api/2.0/search?q=SELECT%20*%20FROM%20messages%20WHERE%20board.id%20=%20%27vxrail%27%20and%20id=%278122626%27)

    • snaveen's avatar
      snaveen
      Guide

      Hi SuzieH 

      Actually, I am new to this, I tried to follow the link shared but was getting confused.
      Can you help me with this here, in combining the above 2 queries using batching query requests?
      • SuzieH's avatar
        SuzieH
        Khoros Alumni (Retired)

        Hi snaveen 

        I think the JSON body of our request will look something like the following. Note that I removed the * from SELECT *. We recommend against using SELECT * as much as possible in LiQL queries (particularly for the Messages collection because a Message object is quite large) and instead be as specific as possible in the SELECT statement. While a query with * might technically return data, * is not officially supported in the SELECT statement.

        Also, notice that I added both a LIMIT for both queries and I added a SORT option for the query to the Messages collection (also a best practice). See the Best Practices section of Using LiQL for more performance tips.

        [
          {
            "floated_messages":{
              "fields":[
                "id",
                "href",
                "message",
                "scope",
                "user"
              ],
              "constraints":[
                {
                  "message.board.id":"VxRail"
                },
                {
                  "scope":{
                    "matches":"global"
                  }
                }
              ],
              "limit":5
            }
          },
          {
            "messages":{
              "fields":[
                "id",
                "view_href",
                "href"
              ],
              "constraints":[
                {
                  "board.id":"VxRail"
                },
                {
                  "id":{
                    "matches":"8122626"
                  }
                }
              ],
              "sorts":[
                "post_time desc"
              ],
              "limit":5
            }
          }
        ]
        

         

        To build the JSON body for the request, I copied the format from the  Request body format for JSON-based queries section of the Community API v2 request guide and basically filled in the <field>, <collection>, and <sort> placeholders with the related fields, constraints, and sorts in the LIQL queries you posted.

        A cURL example would look something like this using Session Key authentication. If you're using OAuth, then you'd use the client-id and Authorization headers (described in the Headers section of the guide I mentioned above) instead of the li-api-session-key header.

        curl -L -X POST 'https://[COMMUNITY DOMAIN]/api/2.0/search' \
        -H 'li-api-session-key: [SESSION KEY]' \
        -H 'Content-Type: application/json' \
        --data-raw '[
          {
            "floated_messages":{
              "fields":[
                "id",
                "href",
                "message",
                "scope",
                "user"
              ],
              "constraints":[
                {
                  "message.board.id":"VxRail"
                },
                {
                  "scope":{
                    "matches":"global"
                  }
                }
              ],
              "limit":5
            }
          },
          {
            "messages":{
              "fields":[
                "id",
                "view_href",
                "href"
              ],
              "constraints":[
                {
                  "board.id":"VxRail"
                },
                {
                  "id":{
                    "matches":"8122626"
                  }
                }
              ],
              "sorts":[
                "post_time desc"
              ],
              "limit":5
            }
          }
        ]'
        

        Please let me know if this helps.