Hello, I am testing my setup. I have created 2 co...
# community-help
a
Hello, I am testing my setup. I have created 2 collections a products one and a stock one. Now the stock has a reference to the products, so they are able to join. I want to test this search with the join in postman, but I don't seem to get stock data in the search. I make a call like this: https://"my cluster"/collections/default_dev_products_00010101_000000_0[…]s=,$default_dev_stock_00010101_000000_00000000() This gives product results but does not seem to give stock results. In the products there is a field called code, the productCode field in stock references this field. How should I correctly make my call in postman to also see stock data together with the product data?
f
Hey @Antoon Van Dessel, Since
stock.productCode
references
products.code
, querying products and pulling in stock is the reverse side of the join. In this case,
include_fields
alone wasn’t enough. You also need a join filter. This worked:
Copy code
curl -G 'https://<cluster>/collections/default_dev_products_00010101_000000_00000000/documents/search' \
    -H 'X-TYPESENSE-API-KEY: <api-key>' \
    --data-urlencode 'q=*' \
    --data-urlencode 'query_by=code' \
    --data-urlencode 'filter_by=$default_dev_stock_00010101_000000_00000000(id:*)' \
    --data-urlencode 'include_fields=code,name,$default_dev_stock_00010101_000000_00000000(*,strategy:nest_array) as stock'
So in Postman, keep your
include_fields
, but add
filter_by=$default_dev_stock_00010101_000000_00000000(id:*)
. After that, the response includes the matching stock rows under stock.
a
That works yes. But then i dont get products that dont have a stock for example. Is there a way to search that would return products, and if they match a join the products with the joined data, if there is no match it would still return the product?
f
Typesense doesn't support reverse joins without filter criteria. So you can't go from products → stocks, but you can go stocks → products without needing to provide a filter. This is based on how the filter map is constructed and saved within Typesense. Maybe CC: @Harpreet Sangar can help here
a
I tried it myseld would this be a viable search for my scenario?: GET https://"cluster"/collections/default_dev_products_00010101_000000_00000000/documents/search?q=&query_by=name&include_fields=,$default_dev_stock_00010101_000000_00000000()&filter_by=(id: || $default_dev_stock_00010101_000000_00000000(id:*))
So using this filter: (id:* || $default_dev_stock_00010101_000000_00000000(id:*))
h
Hi @Antoon Van Dessel! Yes, this is the correct way to implement left join https://typesense.org/docs/30.2/api/joins.html#left-join
🙌 1