Question for the Facet Pros among you: Is it possi...
# community-help
j
Question for the Facet Pros among you: Is it possible to somehow define a nested property of a field with type
object[]
as facet? My data looks like the following (example):
Copy code
"nutritionalValues": [
  {
    "field": "dietaryFiber",
    "perPortion": 4.43,
    "unit": "g"
  },
  {
    "field": "protein",
    "perPortion": 10.65,
    "unit": "g"
  },
  {
    "field": "energyCal",
    "perPortion": 366,
    "unit": "kcal"
  },
  ...
]
Now I want to range-facet e.g. by
energyCal
. My plan b is to extract the relevant property from the array and make it a top-level field. But this would mess up my data model a bit so I am looking for another way to achieve this.
k
energyCal
is a value here, so it can't be defined as a facet field. So you can facet on
nutritionalValues.perPortion
and use
facet_return_parent=nutritionalValues.perPortion
to make the parent object of that value be returned in the response, which you can use for rendering on the UI.
j
Thanks Kishore. How do I specify this in the schema definition? At the moment I have
nutritionalValues
as field:
Copy code
{
  name: 'nutritionalValues',
  type: 'object[]',
  optional: true,
}
I am guessing it's not done by setting the
facet: true
flag on the field?
k
You can refer to nested field using dot notation, so
nutritionalValues.perPortion
j
I am aware of that syntax but I don't think that my goal of faceting by e.g. "entries with nutritional value energyCal should be between 200 and 300"?
k
Adding that to filter_by doesn't help?
Yeah that's not going to be possible. You have to change the schema to have that as a proper field.
j
Mhm, I thought so. Just wanted to be sure. Thanks! ✌️
👍 1