Skip to main content
Question

is there a distinct filter for Data modeling sdk instance query method


I'm using python sdk and wanted to query instances based on a condition using “data_modeling.instances.query” method. i have a view “TimeseriesProperty” and it has a direct relation/reference to "TimeseriesPropertyType" view. Is it possible to have a distinct filter on the result, because multiple “TimeseriesProperty” instances can have the same "TimeseriesPropertyType"  instances , we don't want duplicated "TimeseriesPropertyType"  instances coming in response for below query:

 

view_id= ViewId(space="slb-pdm-dm-governed", external_id="TimeseriesProperty",version="2_0")
v_id_2_PROP = ViewId(space="slb-pdm-dm-governed", external_id="TimeseriesPropertyType",version="2_0")
query= Query(
    with_=
            {
                "TimeseriesProperty":NodeResultSetExpression(
                        limit=10,
                        filter= HasData(views=[view_id])),

                "TimeseriesPropertyType":NodeResultSetExpression(
                limit=10000,
                direction="outwards",
                from_ = "TimeseriesProperty",
                through = view_id.as_property_ref("propertyType"),
                filter= HasData(views=[v_id_2_PROP]))
            }
            ,
    select={
        "TimeseriesProperty": Select([SourceSelector(view_id, properties=['propertyType'])]),
            })
result = client.data_modeling.instances.query(query)

 

5 replies

Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • 111 replies
  • April 3, 2025

No, we do not have a distinct filter. But I might be missing something here, but can’t this be solved with aggregation? Something like

from cognite.client.data_classes.aggregations import Count

view_id= ViewId(space="slb-pdm-dm-governed", external_id="TimeseriesProperty",version="2_0")
client.data_modeling.instances.aggregate(view_id, Count("propertyType"), limit=10)

  • Author
  • Seasoned
  • 22 replies
  • April 4, 2025

Hi ​@Anders Albert !
Thank you for the response. This gives the count of "TimeseriesPropertyType" instances that are attached to "TimeseriesProperty" view, which has a lot of duplicates. we only what unique values of "TimeseriesPropertyType" instances that are attached to "TimeseriesProperty" view. is there any way to achieve that?


Anders  Albert
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • 111 replies
  • April 4, 2025

Thanks for the clarification. I think the the following should do the trick.

view_id= ViewId(space="slb-pdm-dm-governed", external_id="TimeseriesProperty",version="2_0")
v_id_2_PROP = ViewId(space="slb-pdm-dm-governed", external_id="TimeseriesPropertyType",version="2_0")
query= Query(
    with_=
            {
                "TimeseriesProperty":NodeResultSetExpression(
                        limit=10,
                        filter= HasData(views=[view_id])),

                "TimeseriesPropertyType":NodeResultSetExpression(
                        limit=10000,
                        direction="outwards",
                        from_ = "TimeseriesProperty",
                        through = view_id.as_property_ref("propertyType"),
                        filter= HasData(views=[v_id_2_PROP]))
            }
            ,
    select={
        "TimeseriesPropertyType": Select([SourceSelector(view_id, properties=['*'])]),
            })
result = client.data_modeling.instances.query(query)

 

Note the difference between this an the original query is the select part. You select ‘TimeseriesPropertyType’, which will return only the unique TimeSeriesPropertyTypes regardless if it is linked to multiple timeseries properties.


  • Author
  • Seasoned
  • 22 replies
  • April 6, 2025

Thank you!


  • Author
  • Seasoned
  • 22 replies
  • April 7, 2025

hi ​@Anders Albert !
This gives the unique TimeSeriesPropertyTypes with its externalId and space, but this doesn’t give all fields from ‘TimeSeriesPropertyTypes’  view, just its space and externalId. do i need to extend this query?


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings