Hi everyone! I have an array of strings to input (...
# community-help
k
Hi everyone! I have an array of strings to input (for example, ingredients) and I want to fetch all documents where all the "ingredient.name" properties in the document match to an item in the input array. So for example:
input_array = ["shrimp", "bread", "oil", "salt", "pepper]
document that should match:
Copy code
{
  "title": "recipe0",
  "ingredients": [
    {
      "id": 0,
      "name": "shrimp"
    },
    {
      "id": 1,
      "name": "oil"
    }
  ]
}
document that should NOT match (because "tomato" is not in input_array):
Copy code
{
  "title": "recipe1",
  "ingredients": [
    {
      "id": 0,
      "name": "shrimp"
    },
    {
      "id": 1,
      "name": "oil"
    },
    {
      "id": 2,
      "name": "tomato"
    }
  ]
}
Anyone know a way to do this? Thanks a lot!
j
This is not possible to do in Typesense with the data structure you shared.
The only work-around would be to put all the keywords in a single
string
field and then set
drop_tokens_threshold: 0
when doing the search
k
Ok thanks, I'll try that workaround
I switched to Typesense from using only Firestore in the hopes that it could help me query recipes based on ingredient name, but with a bit of flexibility. Because the ingredient names that will be used in the query are not necessarily exactly the same as those in the documents. For example, "onion" vs "red onion", or "Chicken (bone-in)" vs "chicken". Is this within Typesense's capabilities? Maybe I'll need a synonyms list or something, i don't know
j
If the document has
Chicken (bone-in)
in a single string field, then searching for "chicken" will fetch it
🙌 1
Same with the onion use-case
k
Great! Are there any plans to implement some kind of
contains
or
contains_all
query?
j
contains
is the default mode of operation in Typesense.
contains_all
is what happens when you set
drop_tokens_threshold: 0
But the key thing is that these work across a single field
k
Ah I see. Yeah i did the workaround, it actually doesn't quite fit what I was looking for, because if all the inputIngredients don't match all the ingredients in the document, it won't return that document. What I need is to return all documents where the document ingredients are a subset of the inputIngredients. Not sure if putting it that way might bring another solution to mind? If not I'll deal with it for now 🙂
l
Wouldn’t this be what you’re looking for ?
Copy code
Filtering Arrays:
filter_by can be used with array fields as well.

For eg: If genres is a string[] field:

- genres:=[Rock, Pop] will return documents where the genres array field contains Rock OR Pop.
So in your case
ingredients:=input_array
would return all documents where one of the elements of input_array is in the ingredient list
k
Not quite, because ALL the ingredients in the document must also be in the input_array. It's just that not all the ingredients in the input_array must be in the document,
So I guess what I'm asking is, is there an
is_subset
query or plans to implement one? https://stackoverflow.com/questions/16109351/find-documents-in-mongodb-whose-with-an-array-field-is-a-subset-of-a-query-array