Forum Discussion

luk's avatar
luk
Boss
6 years ago

API v2 Problem with ORDER BY multiple fields

SuzieH The docs @https://lithosphere.lithium.com/t5/Developer-Documentation/bd-p/dev-doc-portal?section=commv2&v2.main=usingliql#orderBy say

In the ORDER BY block, you can order your query results. Multiple fields (separated by commas) are supported with ORDER BY as of 17.2.

Therefore I would assume a query like

SELECT id, short_title, view_href, depth, position FROM categories WHERE depth <= 2 AND id IN('<catid_1>', '<catid_2>', '<catid_n>') ORDER BY depth, position ASC

should return a collection of categories ordered by a) depth within the community structure (e.g. top level categories first) and b) their position within the community structure on the same level, but I get an error when executing that:

{
  "status" : "error",
  "message" : "invalid query syntax for SELECT id,short_title,view_href,depth,position FROM categories WHERE depth <= 2 AND id IN (<catid-list>) ORDER BY depth DEFAULT,position ASC",
  "data" : {
    "type" : "error_data",
    "code" : 604,
    "developer_message" : "You must explicitly set a sort order for ORDER BY clause on field: 'depth'. Order can be 'ASC' or 'DESC' in: SELECT id,short_title,view_href,depth,position FROM categories WHERE depth <= 2 AND id IN (<catid-list>) ORDER BY depth DEFAULT,position ASC",
    "more_info" : ""
  },
  "metadata" : { }
}

the community I'm trying that is on v18.6.

Any idea what I'm doing wrong here?

 

EDIT: The more general issue here is that the position is not calculated on a per-level (referring to the community structure in Admin) basis, e.g. top level categories in my case have a position of 5 and higher, while the child categories of each top category start with a position of 0 and up, I therefore need a way to sort the result in a hierarchical way, e.g. top level categories first, then child categories, each ordered by their position within the structure.

EDIT2: The workaround until this is clarified is to manually sort the response items with FreeMarker, e.g. the second ORDER BY field is used in a ?sort_by built-in:

<#assign response = response.data.items?sort_by('depth') />

which preserves the ordering returned by the API (here by position) and then applies the sorting based on the field provided to ?sort_by

  • Hi,

     

    is it not that you have not set a sort order on depth and so the call is assuming your passing a sort order of DEFAULT which is invalid.

    should it not be:

    SELECT id, short_title, view_href, depth, position FROM categories WHERE depth <= 2 AND id IN('<catid_1>', '<catid_2>', '<catid_n>') ORDER BY depth DESC, position ASC

    ?

     

  • Hi,

     

    is it not that you have not set a sort order on depth and so the call is assuming your passing a sort order of DEFAULT which is invalid.

    should it not be:

    SELECT id, short_title, view_href, depth, position FROM categories WHERE depth <= 2 AND id IN('<catid_1>', '<catid_2>', '<catid_n>') ORDER BY depth DESC, position ASC

    ?

     

    • luk's avatar
      luk
      Boss

      Awesome, that's the only combination I didn't think of =)...thanks a lot!

      • SuzieH's avatar
        SuzieH
        Khoros Alumni (Retired)

        Thanks, allensmith81 and luk.

        I'll add a better example in the docs to clarify. Sorry for the confusion.