Sorting Entities by Created Time Using GraphQL in DataHub

Original Slack Thread

<@U06TM7M40RK> I am querying the graphQL api and I want to sort the resulting entities by the created time, ie when the entity was first created in datahub. I can see there is a sort option for lastOperationTime , however that is a bit different. What is the equivalent sortCriteria for the created time/date - is this possible to sort by?

To sort entities by their created time using the GraphQL API in DataHub, you can utilize the created field as a sort criterion. This field captures the timestamp of when an entity was first created in DataHub.

According to the https://github.com/datahub-project/datahub/blob/master/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/incident/EntityIncidentsResolver.java|EntityIncidentsResolver.java, the created field is used as a search index field name for sorting. You can specify this field in your sort criteria to sort entities by their creation time.

Here’s an example of how you might set up your GraphQL query to sort by the created field:

{
  dataset(urn: "urn:li:dataset:(urn:li:dataPlatform:mysql,datahub.metadata_aspect_v2,PROD)") {
    urn
    properties {
      created
    }
  }
}

In the context of using the GraphQL API, you would specify the created field in the sortCriteria parameter when making your query. This will ensure that the results are sorted based on the creation time of the entities.

Would you like more detailed guidance on setting up this GraphQL query or any additional information?

Sources:

can you supply me with the graphql query variables to input into this query the sortOrder can be DESCENDING? <@U06TM7M40RK>

To sort entities by their created time in descending order using the GraphQL API, you can use the sortCriteria input with the created field and specify the sortOrder as DESCENDING. Below is an example of how you can structure your GraphQL query along with the variables to achieve this:

GraphQL Query

query GetDatasets($urn: String!, $sortCriteria: [SortCriterion!]) {
  dataset(urn: $urn) {
    urn
    properties {
      created
    }
  }
}

Query Variables

{
  "urn": "urn:li:dataset:(urn:li:dataPlatform:mysql,datahub.metadata_aspect_v2,PROD)",
  "sortCriteria": [
    {
      "field": "created",
      "sortOrder": "DESCENDING"
    }
  ]
}

In this setup:

  • The urn variable specifies the unique identifier for the dataset you are querying.
  • The sortCriteria variable is an array of sorting criteria, where you specify the field as created and the sortOrder as DESCENDING.

This will ensure that the datasets are sorted by their creation time in descending order.

Would you like more detailed guidance on executing this query or any additional information?

Sources:

Can you provide me with a query which searches all entities and sorts descending by created? <@U06TM7M40RK>

To search across all entities and sort the results by the created field in descending order using the GraphQL API, you can use a query that specifies the sortCriteria with the created field. Below is an example of how you can structure your GraphQL query along with the variables:

GraphQL Query

query SearchEntities($input: String!, $sortCriteria: [SortCriterion!]) {
  search(input: $input, sortCriteria: $sortCriteria) {
    entities {
      urn
      properties {
        created
      }
    }
  }
}

Query Variables

{
  "input": "",
  "sortCriteria": [
    {
      "field": "created",
      "sortOrder": "DESCENDING"
    }
  ]
}

In this setup:

  • The input variable is an empty string, which means it will search across all entities.
  • The sortCriteria variable specifies the field as created and the sortOrder as DESCENDING, ensuring that the results are sorted by creation time in descending order.

This query will return all entities sorted by their creation time in descending order.

Would you like more detailed guidance on executing this query or any additional information?

Sources:

Hi :slight_smile: ! Did this interaction resolve your issue or do you still require assistance ?

Please tag @ RunLLM if you still need further assistance !

Or tag @Datahub Community Support if you require a person to take a look :slight_smile:

Hope this helps ! ><