JOINs and response format variability I have two ...
# community-help
o
JOINs and response format variability I have two collections joined through a field being part of a embedded object, ie:
Copy code
{
  "name": "contacts",
  "enable_nested_fields": true,
  "fields": [
    {
      "name": "contactname",
      "type": "string",
      "sort": true, 
      "facet": true,
      "index": true
    },
    {
      "name": "notes",
      "type": "string",
      "sort": true, 
      "facet": false,
      "index": true,
      "optional": true
    },
    {
      "name": "email",
      "type": "string",
      "sort": true, 
      "facet": false,
      "index": true,
      "optional": true
    }
}
And contact linked to issues:
Copy code
{
  "name": "issues",
  "enable_nested_fields": true,
  "fields": [
    {
      "name": "recordidentifier",
      "type": "string",
      "index": true
    },
    {
      "name": "contactsdata",
      "type": "object[]",
      "sort": false,
      "facet": false,
      "index": true,
      "optional": true
    },
    {
      "name": "contactsdata.contactid",
      "type": "string[]",
      "sort": false,
      "facet": false,
      "index": true,
      "reference": "contacts.id",
      "optional": true
    }
}
What I observe is a different response when I query issues, with or without a filter on contacts (ie: "filter_by": "$contacts(contactname:Chris*"). With a filter, the "contacts" payload is separated from contactsdata, while without a filter it is embedded within. With filter:
Copy code
"contacts": {
                            "contactname": "Christopher Brown",
                            "email": "~~"
                        },
                        "contactsdata": [
                            {
                                "contactid": "51"
                            }
                        ],
Without filter:
Copy code
"contactsdata": [
                            {
                                "contactid": "51",
                                "contacts": {
                                    "contactname": "Christopher Brown",
                                    "email": "~~"
                                },
                            }
                        ],
Is this expected? Is this controllable?
a
This looks expected. When a join comes from filter_by like
$contacts(contactname:Chris*)
, Typesense auto-adds that joined collection to the reference includes with the default nest strategy, so the matched joined doc is attached at the root of the hit as
"contacts": {...}
. When there is no join filter, Typesense instead follows the stored
contactsdata.contactid -> contacts.id
object/
object[]
reference and injects the joined doc back into the owning object/array element, which produces
contactsdata[].contacts
. You can control the top-level join payload with explicit
include_fields
on
$contacts(...)
and
strategy: nest | nest_array | merge
, but that only changes the top-level representation. It does not remap the join at
contactsdata[]
🤔 1
o
Thanks Alan! I must admit this behavior looks a bit weird to me. My filter is not defining the join, ie: $contact(contactname) is not our joining key. So I guess typesense follows the stored reference in both cases. It's really not making it easy for consumers to have a payload format change when adding a filter. It would be great to have a toggle to control that, ie: always inserting joined payload either at the root node or at the "reference" node level when the reference is defined in the queried collection.
h
Hey @Olivier can you share the whole query?
I must admit this behavior looks a bit weird to me. My filter is not defining the join, ie: $contact(contactname) is not our joining key. So I guess typesense follows the stored reference in both cases.
Typesense shouldn't return the info of related docs if the Join is neither mentioned in
filter_by
nor in
include_fields
.