Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

GraphQL filter on _metadata

Avatar

Level 1

We have a requirement for filtering content fragment by created date and last modified date, which are stored in the _metadata. Since it's an out of the box field, we are unable to apply any filters against it. Is there a workaround for this? Our other option is to filter dates after querying the entire data, but we'd like to avoid this if possible.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

18 Replies

Avatar

Employee Advisor

@dsong12 Can you please share the Filter query you are using?

Avatar

Level 2

dear, krati,  We have a requirement for filtering content fragment by  start date and end date, also OR title, content, but it is not work, colud you help to suggest for me ? 

OneCh_0-1702372622100.png

 

Avatar

Employee Advisor

Hi @OneCh 

Can you please write the query here, so that it can be checked for syntax error.

 

Also, please define the filter in text, that you are trying to put in, I see three logical test here, but not sure, where do you want to apply logOp : Or / logOp and

Avatar

Level 2

query landingSearch($offset: Int, $limit: Int, $lang: String, $val: String, $nowDate:Calendar) {
activityList(
offset: $offset
limit: $limit
sort: "publishTime DESC"
_locale: $lang
filter: {
# publishStartTime and publishEndTime
publishStartTime:{_expressions:[{value:$nowDate, _operator:BEFORE}]}
publishEndTime:{_expressions:[{value:$nowDate, _operator:AFTER}]}
_logOp : OR
# title OR content
title: {_expressions: [{value: $val, _operator: CONTAINS,_ignoreCase:true}]},
content: {plaintext: {_expressions: [{value: $val, _operator: CONTAINS,_ignoreCase:true}]}}

}
) {
items {
_path
title
}
}
}

 

thx

Avatar

Employee Advisor

can you also share what value are being passes for these:

$offset: Int, $limit: Int, $lang: String, $val: String, $nowDate:Calendar

 

Basically the query is working fine syntactically. However, your expectations out of query largely depends upon the Data you have and logic you are trying to apply.
So I need above info and sample data.

@OneCh 

Avatar

Level 2

yes, 
 offset : 0 , limit : 10 , val : "test", nowDate : 2023-12-18T14:56:00.000+08:00

Avatar

Level 2

by the way, my graphql logic  is :  (publishStartTime and publishEndTime) OR (title OR content) ,but i don't know how to wirt on graphql

Avatar

Employee Advisor

Alright, below is my observation:

  • Date that you are passing is too exact. Try providing yyyy-mm-dd (2023-12-18)
  • Below Logic means only those records would be enlisted where PublishStartTime(PST) is before ($nowDate) and PublishEndTime(PET) after ($nowDate) . Considering PST should be before PET (Eg PST 2023-12-01 and PET 2023-12-05) , we need to make sure that the dates in your data is following PST<PET
    # publishStartTime and publishEndTime
    publishStartTime:{_expressions:[{value:$nowDate, _operator:BEFORE}]}
    publishEndTime:{_expressions:[{value:$nowDate, _operator:AFTER}]}
  • If you don't mention any operator, AND is considered by Default. So your query is fine as per the logic you shared above.
  • Also try implementing Date Filter and Title-Content Filter separately, so that you can zero the problematic Filter

 

Please validate your date data, if you are still not getting the results

 

Avatar

Level 2

sorry, @krati_garg , my graphql logic  is :  (publishStartTime < nowDate < publishEndTime) AND  (title OR content), how to wirt on graphql?

Avatar

Level 2

Hi @dsong12 ,

From what I understand working on the GraphQL query editor in AEM is that the _metadata is only available in the ModelResults Type and not the ModelFilter Type, this means that you would not be able to filter your content fragments based on the 'created by' and 'last modified' date since they are a part of _metadata. 

You can also check and confirm this in the Docs section in your GraphQL query editor.

Avatar

Level 2

hi, @varun_m1  may i use  'last modified' date to order  in graphql ?

Avatar

Level 2

Hi @OneCh, after doing some hands-on on this one, I noticed that the sort takes up String value of a model attribute, but the problem now arises that there's no way to exactly target the last modified date because the query and json response pair as follows,

_metadata{
          calendarMetadata{
            name
            value
          }
"_metadata": {
            "calendarMetadata": [
              {
                "name": "jcr:created",
                "value": "2023-09-25T12:39:11.047+05:30"
              },
              {
                "name": "cq:lastPublished",
                "value": "2023-09-26T11:55:04.939Z"
              },
              {
                "name": "cq:lastModified",
                "value": "2023-09-26T11:55:04.486Z"
              }
            ]

and if I try to sort it based on this sort in the query,

sort:"_metadata.calendarMetadata.name"

it actually sorts the content fragments based on the jcr:created property. 

 

Avatar

Administrator

@dsong12 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 1

Hello! Did you find any workaround for your problem? I have same issues here