hi TS team, i was looking over the github issue fo...
# community-help
j
hi TS team, i was looking over the github issue for supporting nested fields, and came across this question that very closely mirrors a nested field search use-case i'm encountering. i was just wondering if the ability to search within object arrays is supported yet? essentially, if i have a document like:
Copy code
{
  "employees": [
    {
      "first_name": "John",
      "last_name": "Doe"
    },
    {
      "first_name": "John",
      "last_name": "Smith"
    }
  ]
}
can i search for an employee with
first_name
of
John
and
last_name
of
Smith
?
...and at the same time, avoid having it return this document:
Copy code
{
  "employees": [
    {
      "first_name": "John",
      "last_name": "Doe"
    },
    {
      "first_name": "Jim",
      "last_name": "Smith"
    }
  ]
}
j
This is not possible to do yet, but we’re tracking demand in this issue: https://github.com/typesense/typesense/issues/828
j
thank you!
j
If you are willing to accept the search being more restrictive, you can use the
filter_by
option. To match exactly those fields and values:
Copy code
filter_by: "employees.first_name:=John && employees.last_name:=Smith"
or (no
:=
)
Copy code
filter_by: "employees.first_name:John && employees.last_name:Smith"
j
@JP Erasmus thanks for the tip. just to confirm -- that will match only documents with employees named
John Smith
? (and not, say, documents with employees
John Doe
and
Jim Smith
)
j
I believe it does - we are following similar logic in one of our use cases. With that said, I have only been using Typesense for ~3 weeks, so I could be wrong.
🙏 1
j
hmm, just ran a couple queries to test the
filter_by
syntax, and unfortunately it seems like it does not currently scope to values in the same object. IOW, it will consider the document
Copy code
{
  "employees": [
    {
      "first_name": "John",
      "last_name": "Doe"
    },
    {
      "first_name": "Jim",
      "last_name": "Smith"
    }
  ]
}
as matching the filter
Copy code
filter_by: "employees.first_name:John && employees.last_name:Smith"
j
I checked in more detail now and you are absolutely right. I guess it makes sense, albeit not intuitive, if this document's data is flattened to:
Copy code
employees.first_name: ['John', 'Jim']
employees.last_name: ['Doe', 'Smith']
1
In our case, why the problem isn't as obvious, is because we use it for filtering and the filters have fairly unique values.
I see ElasticSearch solves this problem with a special "nested" object type that under the hood creates separate indexes for each object in the array - essentially treating them as sub-documents that can be matched against as a whole. https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html The Typesense devs will likely have to do something similar to support this use case.
j
agreed. i even googled
typesense sub-document
to see if that was a thing 😃
🔥 1
j
That’s interesting to see how ES handles this! @JP Erasmus could you copy-paste your last message into the GitHub issue?
Haa! I just saw you did this! Thank you! This is good context to have.
💯 1
j
@Jason Bosco in the meantime, when flattening fields for inclusion in the index using regexes, can the field names contain spaces? if so, what is the correct query syntax? e.g.,
employees.first name.value: Jim
? or
employees.'first name'.value: Jim
?
j
Hmm,
employees.first name.value: Jim
should be fine, as long you’re using multi_search or if you’re using single-search and you’re URL encoding the spaces. That said, we haven’t explicitly tested for field names with spaces in them in filtering, so I wouldn’t be surprised if you run into code paths where this unaccounted for and this might cause an issue… If you notice any issues, could you open a GitHub issue with a reproducible example by cloning this gist
1
j
closing the loop here: field names with spaces seem to work just fine!
j
Thank you for confirming! 😅