Search exact array (with facet?)

facets
array
#1

Hi there,

In my documents I have the following kind of array representing my document is structured:

{
  "my-property": [
      "green",
      "orange",
      "green",
      "red"   
  ]
}

I’m already using a facet to search documents that contain the value green (e.g. my-property: green), but now I’m trying to search an exact array. For example, the documents that exactly match ["green", "green", "red"] (the duplicate value is on purpose in my use case).

Is this something possible? If that’s possible to search the values with the exact match and with a different order, that’s would be even better (e.g. searching ["green", "red"] and matching ["red", "green"] but not ["red", "green", "green"]).

Otherwise, I guess I will need serialize the array to a new string to search it (e.g. green_green_red), but there is probably a better way.

Thank you!

#2

Hi @vincent2,

Thanks for contacting Algolia and providing the issue clearly, as well as the example record structure - very helpful on our part to give you the best advice.

You’re already ahead of the game - the array value would not be able to match in the way you described.

Indeed, you would need to add another attribute (or attributes) with values that the engine could match more easily.

  • This concatenated version you suggested will work:
{
  "altMyProperty": "green_orange_green_red",
  "my-property": [
      "green",
      "orange",
      "green",
      "red"   
  ]
}
  • Another possibility, if the attribute options are finite is to use a numeric variation which also would work will with filtering:
{
  "green": 2,
  "orange": 1,
   "red": 1,
  "my-property": [
      "green",
      "orange",
      "green",
      "red"   
  ]
}

We hope this helps!