This message was deleted.
# community-help
s
This message was deleted.
k
Does it happen only for this version?
a
I have pulled the docker image and running it in WSL still receving the error
Yes I was using stable version
0.25.1
earlier but I want to use JOINs
k
Can you post the value of common search params
a
common_search_params = {'query_by': 'name,code'}
k
That looks fine to me. Can you please share a fully reproduceable code snippet when you create a collection, index one document and try querying it which showcases this error?
a
search.py
Copy code
import typesense

client = typesense.Client(
    {
        "nodes": [
            {
                "host": host,  # For Typesense Cloud use xxx.a1.typesense.net
                "port": port,  # For Typesense Cloud use 443
                "protocol": protocol,  # For Typesense Cloud use https
            }
        ],
        "api_key": api_key,
        "connection_timeout_seconds": connection_timeout_seconds,
    }
)


collection1 = {
    "name": "collection1",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "Pizza", "priority": 53, "code": "0"},
    {"name": "Burger", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

collection2 = {
    "name": "collection2",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
        {
            "name": "name_tag",
            "type": "string[]",
            "facet": True,
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "name_tag": ["0"]},
    {"name": "Burger", "priority": 20, "code": "1", "name_tag": ["1"]},
    {"name": "Cake", "priority": 23, "code": "2", "name_tag": ["2"]},
    {"name": "Kiosk", "priority": 72, "code": "3", "name_tag": ["3"]},
    {"name": "Icecream", "priority": 49, "code": "4", "name_tag": ["4"]},
]
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)


def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return


collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = "pizza"
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    if collection=="collection2":
        search_request["filter_by"] = "$collection1(code:=name_tag)"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}

common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
Docker run command:
docker run -p 8108:8108 -v/tmp/data:/data typesense/typesense:0.26.0.rc22 --data-dir /data --api-key=$api_key
Python run command:
python search.py
Error:
typesense.exceptions.RequestMalformed: [Errno 400] One or more search parameters are malformed.
I tried it with rc21 as well but the error still persists
k
Thanks, will check and get back to you
This should be a string and not a tuple:
Copy code
search_request["filter_by"] = ("$tags(code:=name_tag)",)
a
I've update the code above. The error got away but the response is like:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 0,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 0,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
It's not getting any matches in the response and hence I'm not able to test the build. Please suggest if there's any other update in the code. Thanks!
h
Hey @Aadarsh I think no documents have been indexed in either
collection1
or
collection2
.
Both their schemas declare
priority
field which is absent in the documents. Also both the responses have:
Copy code
"out_of": 0,
a
My bad, updated the code. Thanks!
🙌🏼 1
Hi, I'm not able to understand how the JOINs is working as the response is not clear from which collection did it JOIN the results. Also, all the referred results are not obtained in the response. Here's the code:
Copy code
import typesense

client = typesense.Client(
    {
        "nodes": [
            {
                "host": host,  # For Typesense Cloud use xxx.a1.typesense.net
                "port": port,  # For Typesense Cloud use 443
                "protocol": protocol,  # For Typesense Cloud use https
            }
        ],
        "api_key": api_key,
        "connection_timeout_seconds": connection_timeout_seconds,
    }
)


collection1 = {
    "name": "collection1",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "0"},
    {"name": "Burger", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

collection2 = {
    "name": "collection2",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
        {
            "name": "name_tag",
            "type": "string[]",
            "facet": True,
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "name_tag": ["0", "1"]},
    {"name": "Burger", "priority": 20, "code": "1", "name_tag": ["1"]},
    {"name": "Cake", "priority": 23, "code": "2", "name_tag": ["2"]},
    {"name": "Kiosk", "priority": 72, "code": "3", "name_tag": ["3"]},
    {"name": "Icecream", "priority": 49, "code": "4", "name_tag": ["4"]},
]
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)


def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return


collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = "pizza"
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    if collection=="collection2":
        search_request["filter_by"] = "$collection1(code:=name_tag)"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}

common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
The response:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 4,
      "hits": [
        {
          "document": {
            "code": "0",
            "id": "50",
            "name": "Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        },
        {
          "document": {
            "code": "0",
            "id": "45",
            "name": "Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        },
        {
          "document": {
            "code": "0",
            "id": "60",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        },
        {
          "document": {
            "code": "0",
            "id": "55",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 20,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 2
    },
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
Do you have a documentation for this branch? I guess I may not be using it right.
h
Since you have common field names and you've not provided any alias the field values are being overwritten by the joined collection. Try adding this in your code
Copy code
if collection=="collection2":
        search_request["filter_by"] = "$collection1(code:=name_tag)"
        search_request["include_fields"] = "$collection1(*) as collection1"
a
Did not work well. The response should have matched results in both the collections and there should be a collection name label attached to the result. Code:
Copy code
import typesense

client = typesense.Client(
    {
        "nodes": [
            {
                "host": host,  # For Typesense Cloud use xxx.a1.typesense.net
                "port": port,  # For Typesense Cloud use 443
                "protocol": protocol,  # For Typesense Cloud use https
            }
        ],
        "api_key": api_key,
        "connection_timeout_seconds": connection_timeout_seconds,
    }
)


collection1 = {
    "name": "collection1",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "0"},
    {"name": "tags Burger", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]
# delete_collection(collection_name=collection1.strip())
# client.collections.create(collection1)
# client.collections["collection1"].documents.import_(document1)

collection2 = {
    "name": "collection2",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
        {
            "name": "name_tag",
            "type": "string[]",
            "facet": True,
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "name_tag": ["0", "1"]},
    {"name": "Burger", "priority": 20, "code": "1", "name_tag": ["1"]},
    {"name": "Cake", "priority": 23, "code": "2", "name_tag": ["2"]},
    {"name": "Kiosk", "priority": 72, "code": "3", "name_tag": ["3"]},
    {"name": "Icecream", "priority": 49, "code": "4", "name_tag": ["4"]},
]
# delete_collection(collection_name=collection2.strip())
# client.collections.create(collection2)
# client.collections["collection2"].documents.import_(document2)


def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return

def delete_collection(collection_name, verbose=False):
    client.collections[collection_name].delete()
    if verbose:
        print(f'"{collection_name}" deleted successfully')
    return

delete_collection(collection_name="collection1")
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

delete_collection(collection_name="collection2")
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)

query = "burger"

collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = query
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    # if collection=="collection2":
    #     search_request["filter_by"] = "$collection1(code:=name_tag)"
    if collection=="collection2":
        search_request["filter_by"] = "$collection1(code:=name_tag)"
        search_request["include_fields"] = "$collection1(*) as collection1"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}

common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
Response:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "1",
            "id": "1",
            "name": "tags Burger",
            "priority": 20
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Burger"
              ],
              "snippet": "tags <mark>Burger</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Burger"
              ],
              "snippet": "tags <mark>Burger</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "burger"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "burger"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
h
I don't see any document having a code
name_tag
Copy code
"$collection1(code:=name_tag)"
Also since you're searching the collection that has the reference field, you can directly use reference
include_by
. You can remove the
filter_by
.
a
name_tag
is a
string[]
field in collection2 that contains all the
code
from collection1. Basically, I want to get all the codes from collection1 if a result gets matched in collection2. I'm following this structure of documents from this github issue.
h
Okay just to make sure I've understood your situation: You want to search on
collection2
and it has the field
name_tag
that contains a list of
code
on the basis of which you want to include the related document of
collection1
that has the same
code
.
a
Yes, also it should search through all the collections that are retreived.
h
it should search through all the collections that are retreived.
I didn't understand this part
a
Just like it was happening in 0.25.1, when we do this:
client.collections[collection_name].retrieve()
I'm retreiving all the collections in the code above to search in those for the text matches
Hi, any suggestions?
h
Since there are multiple codes (reference values) in your
collection2
document, you'll have to create a junction collection with the following schema:
Copy code
{
    "name": "code_links",
    "fields": [
        {
            "name": "coll1",
            "type": "string",
            "reference": "collection1.code",
        },
        {
            "name": "coll2",
            "type": "string",
            "reference": "collection2.id",
        }
    ],
}
coll1
field will store the
code
value and
coll2
field will store the document id of the
collection2
document. We have to use this approach since there are multiple reference values in each doc.
You can remove the reference field from the
collection2
now. The search would look like:
Copy code
if collection=="collection2":
        search_request["filter_by"] = "$code_links(id:*)",
        search_request["include_fields"] = "$collection1(*) as collection1"
I'm going to look into supporting the use of a
string[]
field as
reference
too so you won't have to create an extra collection for this use case. I'll let you know.
✅ 1
a
You're right, there's one step added with this way. Anyway, the above filter_by or include_fields parameters or the collection schema might be incorrect as I'm getting
typesense.exceptions.RequestMalformed: [Errno 400] One or more search parameters are malformed.
h
Can you share your current
search_request
code snippet?
a
I got the catch! The filter_by was a tuple
The current response with the search request code is:
Copy code
search_request: {'collection': 'collection2', 'q': 'pizza', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'filter_by': '$code_links(id:*)', 'include_fields': '$collection1(*) as collection1'}
Response:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "code": 400,
      "error": "Failed to join on `code_links`: No reference field found."
    }
  ]
}
h
Try this:
Copy code
collection1 = {
    "name": "collection1",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "0"},
    {"name": "tags Burger", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]
# delete_collection(collection_name=collection1.strip())
# client.collections.create(collection1)
# client.collections["collection1"].documents.import_(document1)

collection2 = {
    "name": "collection2",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
        {
            "name": "name_tag",
            "type": "string[]",
            "facet": True,
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "name_tag": ["0", "1"]},
    {"name": "Burger", "priority": 20, "code": "1", "name_tag": ["1"]},
    {"name": "Cake", "priority": 23, "code": "2", "name_tag": ["2"]},
    {"name": "Kiosk", "priority": 72, "code": "3", "name_tag": ["3"]},
    {"name": "Icecream", "priority": 49, "code": "4", "name_tag": ["4"]},
]
# delete_collection(collection_name=collection2.strip())
# client.collections.create(collection2)
# client.collections["collection2"].documents.import_(document2)

code_links = {
    "name": "code_links",
    "fields": [
        {
            "name": "coll1",
            "type": "string",
            "reference": "collection1.code",
        },
        {
            "name": "coll2",
            "type": "string",
            "reference": "collection2.id",
        }
    ],
}

documents = [
    {"coll1": "0", "coll2": "0"},
    {"coll1": "1", "coll2": "0"},
    {"coll1": "1", "coll2": "1"},
    {"coll1": "2", "coll2": "2"},
    {"coll1": "3", "coll2": "3"},
    {"coll1": "4", "coll2": "4"}
]

def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return

def delete_collection(collection_name, verbose=False):
    client.collections[collection_name].delete()
    if verbose:
        print(f'"{collection_name}" deleted successfully')
    return

delete_collection(collection_name="collection1")
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

delete_collection(collection_name="collection2")
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)

delete_collection(collection_name="code_links")
client.collections.create(code_links)
client.collections["code_links"].documents.import_(documents)

query = "burger"

collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = query
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    # if collection=="collection2":
    #     search_request["filter_by"] = "$collection1(code:=name_tag)"
    if collection=="collection2":
        search_request["filter_by"] = "$code_links(id:*)"
        search_request["include_fields"] = "$collection1(*) as collection1"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}

common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
a
For the query
pizza
, shouldn't Collection2 Response contain
tags Burger
as well rather than just the name_tag array?
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "tags <mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "0",
            "code_sequence_id": 0,
            "coll1": [
              "0",
              "1"
            ],
            "coll1_sequence_id": [
              0,
              1
            ],
            "coll2": [
              "0",
              "0"
            ],
            "coll2_sequence_id": [
              0,
              0
            ],
            "collection1.code": "0",
            "collection1.name": "tags Pizza",
            "collection1.priority": 53,
            "id": "0",
            "name": "Pizza",
            "name_tag": [
              "0",
              "1"
            ],
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "pizza"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
I changed
tags Burger
to
tags Pizzeria
in collection1 but it doesn't show up in the response as matched with the query
pizza
h
I could not replicate your error. With the following search request
Copy code
export TYPESENSE_API_KEY=abcd

curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
     -X DELETE \
    "http://localhost:8108/collections/collection1"
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
     -X DELETE \
    "http://localhost:8108/collections/collection2"
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
     -X DELETE \
    "http://localhost:8108/collections/code_links"

curl "http://localhost:8108/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
                "name": "collection1",
                "fields": [
                    {"name": "name", "type": "string", "facet": true},
                    {"name": "priority", "type": "int32", "facet": true},
                    {"name": "code", "type": "string", "facet": true}
                ]
            }'

curl "http://localhost:8108/collections/collection1/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"name": "tags Pizza", "priority": 53, "code": "0"}
{"name": "tags Burger", "priority": 20, "code": "1"}
{"name": "Cake", "priority": 23, "code": "2"}
{"name": "Kiosk", "priority": 72, "code": "3"}
{"name": "Icecream", "priority": 49, "code": "4"}'

curl "http://localhost:8108/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
                "name": "collection2",
                "fields": [
                    {"name": "name", "type": "string", "facet": true},
                    {"name": "priority", "type": "int32", "facet": true},
                    {"name": "name_tag", "type": "string[]", "facet": true}
                ]
            }'

curl "http://localhost:8108/collections/collection2/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"name": "Pizza", "priority": 53, "name_tag": ["0", "1"]}
{"name": "Burger", "priority": 20, "name_tag": ["1"]}
{"name": "Cake", "priority": 23, "name_tag": ["2"]}
{"name": "Kiosk", "priority": 72, "name_tag": ["3"]}
{"name": "Icecream", "priority": 49, "name_tag": ["4"]}'

curl "http://localhost:8108/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
                "name": "code_links",
                "fields": [
                    {"name": "coll1", "type": "string", "reference": "collection1.code"},
                    {"name": "coll2", "type": "string", "reference": "collection2.id"}
                ]
            }'

curl "http://localhost:8108/collections/code_links/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"coll1": "0", "coll2": "0"}
{"coll1": "1", "coll2": "0"}
{"coll1": "1", "coll2": "1"}
{"coll1": "2", "coll2": "2"}
{"coll1": "3", "coll2": "3"}
{"coll1": "4", "coll2": "4"}'

curl 'http://localhost:8108/multi_search' -X POST -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '
  {"searches":[
        {
            "collection":"collection2",
            "q":"pizza",
            "query_by": "name",
            "filter_by":"$code_links(id:*)",
            "include_fields": "$collection1(*) as collection1",
            "exclude_fields": "$code_links(*)"
        }
    ]}' | jq
I get following response
Copy code
"hits": [
        {
          "document": {
            "collection1.code": [
              "0",
              "1"
            ],
            "collection1.name": [
              "tags Pizza",
              "tags Burger"
            ],
            "collection1.priority": [
              53,
              20
            ],
            "id": "0",
            "name": "Pizza",
            "name_tag": [
              "0",
              "1"
            ],
            "priority": 53
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Pizza"
              ],
              "snippet": "<mark>Pizza</mark>"
            }
          ],
          "text_match": 578730123365712000,
          "text_match_info": {
            "best_field_score": "1108091339008",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365711993",
            "tokens_matched": 1
          }
        }
a
The code you had sent didn't remove the reference of collection1 from collection2 that was the catch.
Works fine now 👍
h
Great!
a
Do you have a documentation of
0.26.0.rc22
branch?
h
Related to joins?
a
Yes and overall
h
Joins documentation. I'm not sure about the rest of the features.
k
We haven't started putting together release notes for the 0.26 release yet. Individual major features are documented with the GitHub issues.
a
Aware of this documentation but needed more frequent updates there as some of the solutions are not present as we discussed here in this thread. Good job though. Hoping to get an update regarding elimination of the middleware collection (
code_links
) for efficiency soon. Thanks a lot!
k
We update it as we come across more scenarios. It's a complicated feature so we are still addressing gaps as we come across them.
a
Understandable
Hi, I want to search with documents having single reference fields to other collection documents. I've made this python code but did not get the required response.
Copy code
import typesense

client = typesense.Client(
    {
        "nodes": [
            {
                "host": host,  # For Typesense Cloud use xxx.a1.typesense.net
                "port": port,  # For Typesense Cloud use 443
                "protocol": protocol,  # For Typesense Cloud use https
            }
        ],
        "api_key": api_key,
        "connection_timeout_seconds": connection_timeout_seconds,
    }
)


collection1 = {
    "name": "collection1", ## tags
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "3"},
    {"name": "tags Pizzeria", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]

collection2 = {
    "name": "collection2",  # items
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
        {
            "name": "reference_collection_id",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "reference_collection_id": "0"},
    {"name": "Burger", "priority": 20, "code": "1", "reference_collection_id": "1"},
    {"name": "Cake", "priority": 23, "code": "2", "reference_collection_id": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3", "reference_collection_id": "3"},
    {"name": "Icecream", "priority": 49, "code": "4", "reference_collection_id": "4"},
]

code_links = {
    "name": "code_links",
    "fields": [
        {
            "name": "coll1",
            "type": "string",
            "reference": "collection1.code",
        },
        {
            "name": "coll2",
            "type": "string",
            "reference": "collection2.id",
        }
    ],
}

documents = [
    {"coll1": "0", "coll2": "0"},
    {"coll1": "1", "coll2": "1"},
    {"coll1": "2", "coll2": "2"},
    {"coll1": "3", "coll2": "3"},
    {"coll1": "4", "coll2": "4"}
]

def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return

def delete_collection(collection_name, verbose=False):
    client.collections[collection_name].delete()
    if verbose:
        print(f'"{collection_name}" deleted successfully')
    return

delete_collection(collection_name="collection1")
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

delete_collection(collection_name="collection2")
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)

delete_collection(collection_name="code_links")
client.collections.create(code_links)
client.collections["code_links"].documents.import_(documents)

query = "kiosk"

collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = query
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    if collection=="collection2":
        search_request["filter_by"] = "$code_links(id:*)"
        search_request["include_fields"] = "$collection1(*) as collection1"
        search_request["exclude_fields"] = "$code_links(*)"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}
common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
Copy code
search_request = {'collection': 'collection2', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'filter_by': '$code_links(id:*)', 'include_fields': '$collection1(*) as collection1', 'exclude_fields': '$code_links(*)'}
Response:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "markKiosk/mark"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "markKiosk/mark"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 3,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
Here, as I searched for
kiosk
it should have searched within the collection2 as well with references to collection1. Please let me know the correct way to do this. I'm using
typesense/typesense:0.26.0.rc24
h
Shouldn't the reference to
collection2
be
collection2.reference_collection_id
?
Copy code
code_links = {
    "name": "code_links",
    "fields": [
        {
            "name": "coll1",
            "type": "string",
            "reference": "collection1.code",
        },
        {
            "name": "coll2",
            "type": "string",
            "reference": "collection2.reference_collection_id", # here
        }
    ],
}
a
It does not change the response
Also, why do we need to introduce a middleware collection
code_links
if we have single IDs to refer and not array as earlier?
h
It does not change the response
I think it's because of this in
collection2
Copy code
{
            "name": "reference_collection_id",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
Also, why do we need to introduce a middleware collection
code_links
if we have single IDs to refer and not array as earlier?
That's right you don't need
code_links
collection in this scenario
You only need a reference field in
collection2
and the following search request
Copy code
if collection=="collection2":
        search_request["include_fields"] = "$collection1(*) as collection1"
There's no need to specify join in
filter_by
since you are searching the collection that already has reference to
collection1
a
Got it. Works
🙌🏼 1
Please look at this issue... If there is even a single document that doesn't contain reference IDs from collection1, typesense does not hit request or fetch matching results for the whole collection2 itself. Code:
Copy code
import typesense

client = typesense.Client(
    {
        "nodes": [
            {
                "host": host,  # For Typesense Cloud use xxx.a1.typesense.net
                "port": port,  # For Typesense Cloud use 443
                "protocol": protocol,  # For Typesense Cloud use https
            }
        ],
        "api_key": api_key,
        "connection_timeout_seconds": connection_timeout_seconds,
    }
)


collection1 = {
    "name": "collection1", ## tags
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
    ],
}
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "3"},
    {"name": "tags Pizzeria", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]

collection2 = {
    "name": "collection2",  # items
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
        {
            "name": "reference_collection_id",
            "type": "string",
            "facet": True,
            "reference": "collection1.code",
        },
    ],
}
document2 = [
    {"name": "Pizza", "priority": 53, "code": "0", "reference_collection_id": "10101010"},  ## random number not in collection1.code
    {"name": "Burger", "priority": 20, "code": "1", "reference_collection_id": "10101010"},
    {"name": "Cake", "priority": 23, "code": "2", "reference_collection_id": "10101010"},
    {"name": "Kiosk", "priority": 72, "code": "3", "reference_collection_id": "3"},  ## code there in collection1
    {"name": "Icecream", "priority": 49, "code": "4", "reference_collection_id": "10101010"},
]

def retreive_collection(collection_name, verbose=False):
    client.collections[collection_name].retrieve()
    if verbose:
        print(f'"{collection_name}" retreived successfully')
    return

def delete_collection(collection_name, verbose=False):
    client.collections[collection_name].delete()
    if verbose:
        print(f'"{collection_name}" deleted successfully')
    return

delete_collection(collection_name="collection1")
client.collections.create(collection1)
client.collections["collection1"].documents.import_(document1)

delete_collection(collection_name="collection2")
client.collections.create(collection2)
client.collections["collection2"].documents.import_(document2)

query = "kiosk"

collection_names = ["collection1", "collection2"]
filtered_search_requests = list()
for collection in collection_names:
    search_request = dict()
    search_request["collection"] = collection
    search_request["q"] = query
    search_request["sort_by"] = "priority:desc,_text_match:desc"
    search_request["prioritize_token_position"] = "true"
    if collection=="collection2":
        search_request["include_fields"] = "$collection1(*) as collection1"
    filtered_search_requests.append(search_request)

search_requests = {"searches": filtered_search_requests}
print("search_request =", search_requests)
common_search_params = {
    "query_by": "name,code",
}

for collection in collection_names:
    retreive_collection(collection_name=collection)

response = client.multi_search.perform(search_requests, common_search_params)
print(response)
Copy code
search_request = {'searches': [{'collection': 'collection1', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}, {'collection': 'collection2', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'include_fields': '$collection1(*) as collection1'}]}
Response:
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 0,
      "hits": [],
      "out_of": 0,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
h
"out_of": 0,
is returned. No documents have been indexed.
All of the docs of
collection2
have the same non-existent reference.
a
Yes, but >
{"name": "Kiosk", "priority": 72, "code": "3", "reference_collection_id": "3"},  ## code there in collection1
Document2 has this row having correct reference... Please explain
h
What is the response now?
a
I didn't change the code... It's there in the document already with the response above
h
If you've received the same response for the search, you should log the response of document indexing API.
a
I didn't get your point. I just pointed out the nature of
document2
and there's no new response after the above addressed issue.
If the
document2
has one correct reference, why does the response still return
"out_of" : 0
?
h
"out_of": 0,
is returned. No documents have been indexed.
This indicates there are no documents in the
collection2
.
a
Okay, if I eliminate the field
reference_collection_id
then the document2 gets indexed and search works properly without referencing. This also works when I add the
reference_collection_id
with correct reference IDs. But, as soon as I add the field with even one incorrect reference ID, the document doesn't get indexed. Am I declaring the document incorrectly?
h
I wonder if it's failing the whole import because of the wrong reference. Could you try this and let me know?
Copy code
document2 = [
    {"name": "Kiosk", "priority": 72, "code": "3", "reference_collection_id": "3"},  ## code there in collection1
    {"name": "Pizza", "priority": 53, "code": "0", "reference_collection_id": "10101010"},  ## random number not in collection1.code
    {"name": "Burger", "priority": 20, "code": "1", "reference_collection_id": "10101010"},
    {"name": "Cake", "priority": 23, "code": "2", "reference_collection_id": "10101010"},
    {"name": "Icecream", "priority": 49, "code": "4", "reference_collection_id": "10101010"},
]
a
Did not work. Still returns
"out_of": 0
h
Hmm should not happen, thanks for the confirmation. I'll look into it.
a
👍
h
Could you share the response of
Copy code
print(client.collections["collection2"].documents.import_(document2))
a
Hi, sorry I was not notified. Here's the output:
Copy code
[{'code': 400, 'document': '{"name": "Kiosk", "priority": 72, "code": "3", "reference_collection_id": "3"}', 'error': 'Multiple documents having `code: 3` found in the collection `collection1`.', 'success': False}, {'code': 400, 'document': '{"name": "Pizza", "priority": 53, "code": "0", "reference_collection_id": "10101010"}', 'error': 'Referenced document having `code: 10101010` not found in the collection `collection1`.', 'success': False}, {'code': 400, 'document': '{"name": "Burger", "priority": 20, "code": "1", "reference_collection_id": "10101010"}', 'error': 'Referenced document having `code: 10101010` not found in the collection `collection1`.', 'success': False}, {'code': 400, 'document': '{"name": "Cake", "priority": 23, "code": "2", "reference_collection_id": "10101010"}', 'error': 'Referenced document having `code: 10101010` not found in the collection `collection1`.', 'success': False}, {'code': 400, 'document': '{"name": "Icecream", "priority": 49, "code": "4", "reference_collection_id": "10101010"}', 'error': 'Referenced document having `code: 10101010` not found in the collection `collection1`.', 'success': False}]
h
Oh. Now I see why indexing fails
Copy code
Multiple documents having `code: 3` found in the collection `collection1`
code: 3
is not a unique value in
collection1
a
Right, I made it
Copy code
document1 = [
    {"name": "tags Pizza", "priority": 53, "code": "0"},
    {"name": "tags Pizzeria", "priority": 20, "code": "1"},
    {"name": "Cake", "priority": 23, "code": "2"},
    {"name": "Kiosk", "priority": 72, "code": "3"},
    {"name": "Icecream", "priority": 49, "code": "4"},
]
And there's still no hits in collection2
h
out_of
is still 0 ?
a
"out_of": 1
h
And what's the query?
a
kiosk
h
All of the search parameters on
collection2
a
Got it. I was using filter_by as an experiment earlier but removed it from search requests. Working fine now 👍. Thanks!
🙌🏼 1
Hi @Harpreet Sangar. I have both the use cases (as discussed and solved individually above) combined in one collection. I need to refer two different collections from a single collection1, one field has multiple reference IDs (
string[]
) of collection2 and the other field has a single ID (
string
) to refer in some other collection3. I have created a mid collection code_links as we figured out. I get
"out_of": 0
in the response as I'd need to use
filter_by
to refer collection2 and should avoid using
filter_by
to refer collection3. Can you suggest a way to handle this?
h
"out_of": 0
This indicates that no document is present in the collection. You should check the response of the import API to see why no document was indexed. Also, I've added the ability to have a
reference
in an array type field so you won't have to create
code_links
collection.
I'd need to use
filter_by
to refer collection2 and should avoid using
filter_by
to refer collection3
The suggestion regarding not needing the
filter_by
expression in your second case was just meant for the scenario where you're searching the collection that has the
reference
field (collection1) and you only need to include the fields of the referenced documents. You still can have any
filter_by
expression. If for example, you wanted to filter by a field in the collection3, you can specify
$collection3(field_name:value)
.
a
Okay. Have you added the array reference feature in rc25? Can we use it the same way to refer among collections?
h
Yes. You can read the summary here for an example https://github.com/typesense/typesense/pull/1303 @Kishore Nallan will let you know in which version this feature will land.
a
That's great. Thanks
k
This is available in rc25 published yesterday
✅ 1
a
How should I define
"include_fields"
to include all fields from two different collections? I have a collection like this:
Copy code
collection2 = {
    "name": "collection2",
    "fields": [
        {
            "name": "name",
            "type": "string",
            "facet": True,
        },
        {
            "name": "priority",
            "type": "int32",
            "facet": True,
        },
        {
            "name": "code",
            "type": "string",
            "facet": True,
        },
        {
            "name": "name_tag",
            "type": "string[]",
            "facet": True,
            "reference": "collection1.code",
        },
        {
            "name": "coll3_ref",
            "type": "string",
            "facet": True,
            "reference": "collection3.code",
        },
    ],
}
k
Try
$collection1(*)
to fetch all fields from
collection1
a
Need to get all fields from both the referred collections, i.e.
collection1
and
collection3
k
Comma separate and add the other in the same way
a
Like this, right?
Copy code
search_requests =  {'searches': [{'collection': 'collection1', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}, {'collection': 'collection2', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'include_fields': '$collection1(*),$collection3(*)'}, {'collection': 'collection3', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}]}
k
Yes, doesn't work?
a
No,
client.multi_search.perform(search_requests, common_search_params)
gets stuck
h
@Aadarsh what is the response of this?
Copy code
{'collection': 'collection2', 'q': '*'}
a
Here's the response to
search_requests =  {'searches': [{'collection': 'collection2', 'q': '*'}]}
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "0",
            "name": "Kiosk",
            "name_tag": [
              "0",
              "1"
            ],
            "name_tag_sequence_id": [
              0,
              1
            ],
            "priority": 72,
            "reference_collection_id": "0",
            "reference_collection_id_sequence_id": 0
          },
          "highlight": {},
          "highlights": []
        }
      ],
      "out_of": 1,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
h
Also the response of
Copy code
{'collection': 'collection1', 'q': '*'},
{'collection': 'collection3', 'q': '*'}
a
search_requests =  {'searches': [{'collection': 'collection1', 'q': '*'}, {'collection': 'collection3', 'q': '*'}]}
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 5,
      "hits": [
        {
          "document": {
            "code": "4",
            "id": "4",
            "name": "Icecream",
            "priority": 49
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "2",
            "id": "2",
            "name": "Cake",
            "priority": 23
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "1",
            "id": "1",
            "name": "tags Pizzeria",
            "priority": 20
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {},
          "highlights": []
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 5,
      "hits": [
        {
          "document": {
            "code": "4",
            "id": "4",
            "name": "Icecream",
            "priority": 49
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "2",
            "id": "2",
            "name": "Cake",
            "priority": 23
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "1",
            "id": "1",
            "name": "coll3 Pizzeria",
            "priority": 20
          },
          "highlight": {},
          "highlights": []
        },
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "coll3 Pizza",
            "priority": 53
          },
          "highlight": {},
          "highlights": []
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection3",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
h
Cannot think of any reason why this would get stuck. I'll try to reproduce it.
👍 1
Does the query also get stuck with only one reference include?
a
Yes, if I only refer to
$collection1(*)
It works fine when I refer to only
$collection3(*)
(the
string
type reference)
search_requests =  {'searches': [{'collection': 'collection1', 'q': '*', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}, {'collection': 'collection2', 'q': '*', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'include_fields': '$collection3(*)'}, {'collection': 'collection3', 'q': '*', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}]}
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 5,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "tags Pizza",
            "priority": 53
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "4",
            "id": "4",
            "name": "Icecream",
            "priority": 49
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "2",
            "id": "2",
            "name": "Cake",
            "priority": 23
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "1",
            "id": "1",
            "name": "tags Pizzeria",
            "priority": 20
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "coll3 Pizza",
            "name_tag": [
              "0",
              "1"
            ],
            "name_tag_sequence_id": [
              0,
              1
            ],
            "priority": 53,
            "reference_collection_id": "0",
            "reference_collection_id_sequence_id": 0
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        }
      ],
      "out_of": 1,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 5,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "0",
            "id": "0",
            "name": "coll3 Pizza",
            "priority": 53
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "4",
            "id": "4",
            "name": "Icecream",
            "priority": 49
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "2",
            "id": "2",
            "name": "Cake",
            "priority": 23
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        },
        {
          "document": {
            "code": "1",
            "id": "1",
            "name": "coll3 Pizzeria",
            "priority": 20
          },
          "highlight": {},
          "highlights": [],
          "text_match": 100,
          "text_match_info": {
            "best_field_score": "0",
            "best_field_weight": 12,
            "fields_matched": 4,
            "score": "100",
            "tokens_matched": 0
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection3",
        "per_page": 10,
        "q": "*"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
Along with this, could you please tell me the significance of
{field}_sequence_id
in the response? How can it be used in the search?
h
{field}_sequence_id
field is used internally to create a reference. The
name_tag
field in
collection2
has a reference to
collection1.code
. So we store the doc ids of
collection1
that match those values in
name_tag_sequence_id
.
Copy code
"name_tag": ["0", "1"],
						"name_tag_sequence_id": [0, 1],
It's not meant to be used directly for search.
I've confirmed the bug locally. For the time being, you can send
Copy code
'include_fields': '$collection1(*) as collection1,$collection3(*) as collection3'
a
It works fine now
search_requests =  {'searches': [{'collection': 'collection1', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}, {'collection': 'collection2', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true', 'include_fields': '$collection1(*) as collection1,$collection3(*) as collection3'}, {'collection': 'collection3', 'q': 'kiosk', 'sort_by': 'priority:desc,_text_match:desc', 'prioritize_token_position': 'true'}]}
Copy code
{
  "results": [
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "tags Kiosk",
            "priority": 72
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "tags <mark>Kiosk</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "tags <mark>Kiosk</mark>"
            }
          ],
          "text_match": 578730123365705900,
          "text_match_info": {
            "best_field_score": "1108091339005",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123365705849",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection1",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "collection1.code": [
              "0",
              "1",
              "3"
            ],
            "collection1.name": [
              "tags Pizza",
              "tags Pizzeria",
              "tags Kiosk"
            ],
            "collection1.priority": [
              53,
              20,
              72
            ],
            "collection3.code": "0",
            "collection3.name": "coll3 Pizza",
            "collection3.priority": 53,
            "id": "0",
            "name": "Kiosk",
            "name_tag": [
              "0",
              "1",
              "3"
            ],
            "name_tag_sequence_id": [
              0,
              1,
              3
            ],
            "priority": 72,
            "reference_collection_id": "0",
            "reference_collection_id_sequence_id": 0
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 1,
      "page": 1,
      "request_params": {
        "collection_name": "collection2",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    },
    {
      "facet_counts": [],
      "found": 1,
      "hits": [
        {
          "document": {
            "code": "3",
            "id": "3",
            "name": "Kiosk",
            "priority": 72
          },
          "highlight": {
            "name": {
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          },
          "highlights": [
            {
              "field": "name",
              "matched_tokens": [
                "Kiosk"
              ],
              "snippet": "<mark>Kiosk</mark>"
            }
          ],
          "text_match": 578730123366232200,
          "text_match_info": {
            "best_field_score": "1108091339262",
            "best_field_weight": 15,
            "fields_matched": 1,
            "score": "578730123366232185",
            "tokens_matched": 1
          }
        }
      ],
      "out_of": 5,
      "page": 1,
      "request_params": {
        "collection_name": "collection3",
        "per_page": 10,
        "q": "kiosk"
      },
      "search_cutoff": false,
      "search_time_ms": 0
    }
  ]
}
h
Yeah the bug was due to the same field names in
collection2
and
collection1
.
a
How did it work now? Earlier it was not. I didn't change anything in my documents or schema 😅
h
Notice the field names in the returned doc of
collection2
a
It's totally correct and I got your point but earlier
'include_fields': '$collection1(*) as collection1,$collection3(*) as collection3'
wasn't working. It used to get stuck.
h
Weren't you sending
'include_fields': '$collection1(*),$collection3(*)'
earlier? The difference is we've now added an alias by using
as
a
Exactly. Got it