Message Boards
Headless: How to filter by structure field?
Headless: How to filter by structure field?
Expert Posts: 396 Join Date: 8/22/07 Recent PostsHi guys,
DXP 7.2 in this case.
I'm trying to filter a webcontent search with a structure field and failing. Maybe someone has done it before?
I'm following this article that briefly mentions filtering by a structure field, showing no example, and none of my attempts is working. I can filter by title, though.
What works: filter=title eq 'SomeTitle'
https://servername/o/headless-delivery/v1.0/content-structures/182251/structured-contents?filter=title%20eq%20%27SomeTitle%27
What doesn't work: filter=structFieldName eq 'SomeValue'
https://servername/o/headless-delivery/v1.0/content-structures/182251/structured-contents?filter=structFieldName%20eq%20%27SomeValue%27
The answer, in these cases is:
{
"status": "BAD_REQUEST",
"title": "A property used in the filter criteria is not supported: structFieldName eq 'SomeValue'"
}
Taking note of the article's paragraph saying "Filtering mainly applies to fields indexed as keywords in Liferay DXP’s search" (why mainly?), I checked and my failed examples are respecting this rule (indexed - keyword). Also tried with a Date field and got the same result.
Any ideas?
TIA
Fernando
RE: Headless: How to filter by structure field? (Answer)
Expert Posts: 348 Join Date: 2/12/15 Recent PostsSorry for the documentation, we are in the process of improving it right now.
The fields of the webcontent are inside the property contentFields, so following OData syntax it should be something like:
contentFields/property eq 'example'
- Filtering mainly applies to fields indexed as keywords in Liferay DXP’s search" (why mainly?)
This is an Elasticsearch limitation/characteristic. Text indexed-fields are tokenized/transformed so they can not be used in sorting and several filtering operations. They are used in search because search does not do strict comparisons like 'eq' does. You can see a brief explanation here.
RE: RE: Headless: How to filter by structure field?
Expert Posts: 396 Join Date: 8/22/07 Recent PostsThanks Javier. The error is now a differet one.
I'm trying to select content based on date and, when I try with a syntax as per the mentioned article, an "Incompatible types" 400 error is returned.
The request is:
https://someserver/o/headless-delivery/v1.0/content-structures/182251/structured-contents?filter=contentFields/dateSchedule%20le%202018-02-13T12:33:12Z
Also tried with single quotes, like %27, surrounding the date value.
The field dateSchedule is an indexed ddm-date field in the structure.
Thanks
Fernando
RE: RE: Headless: How to filter by structure field?
Expert Posts: 348 Join Date: 2/12/15 Recent PostsThe Date fields of the structure are just dates, without time information (dateModified/dateCreated those have time information and are indexed as datetime).
So the filter should be like this: contentFields/DateReference le 2021-05-29
curl "http://localhost:8080/o/headless-delivery/v1.0/content-structures/58512/structured-contents?restrictFields=actions&filter=contentFields%2FDateReference%20le%202021-05-29" \
-u 'test@liferay.com:test'
BUT if you are using 7.4 or 7.3, you could be affected by this bug (Filter does not work for contentFields when content is upperCase, nor contains/startsWith in the new indexing). We are working on a fix but meanwhile if you are affected (you don't get any results back), you can enable the legacy indexer for DDM (no performance/results difference) and reindex, when it's fixed you can switch back.
RE: Headless: How to filter by structure field?
Expert Posts: 396 Join Date: 8/22/07 Recent PostsThanks Javier, that was it!