### Run Typesense via Docker ########################################
export TYPESENSE_API_KEY=xyz
    
mkdir "$(pwd)"/typesense-data

docker run -p 8108:8108 \
            -v"$(pwd)"/typesense-data:/data typesense/typesense:27.1 \
            --data-dir /data \
            --api-key=$TYPESENSE_API_KEY \
            --enable-cors

### Reproduction Steps ###############################################
export TYPESENSE_API_KEY=xyz

curl "http://localhost:8108/debug" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"

echo $"\n\nDeleting collections\n"

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


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



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


curl "http://localhost:8108/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
         "name": "transactions",
         "fields": [
           {"name": "redemption_code", "type": "string", "reference": "redemption_codes.code", "optional": true },
            {"name": "amount", "type": "int64" },
            {"name": "created_at", "type": "int64" }
         ]
       }'


curl "http://localhost:8108/collections/transactions/documents/import?action=create" \
      -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
      -H "Content-Type: text/plain" \
      -X POST \
      -d '{"id": "124","redemption_code": "1234","amount": 100,"created_at": 12345678 }
          {"id": "125","amount": 200,"created_at": 12345678 }' # note that this transaction does not have a redemption code



curl "http://localhost:8108/collections/redemption_codes/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"id": "124","code": "1234","created_at": 12345678 }
            {"id": "125","code": "5678","created_at": 12345678 }'

echo $"\n\nTrying to multi search\n"
            
curl "http://localhost:8108/multi_search" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
          "searches": [
            {
            "q": "*",
          "collection": "redemption_codes",
          "filter_by": "id:* || $transactions(id:*)",
          "include_fields": "$transactions(*)"
            }
          ]
        }'


echo $"\n\nTrying to export\n"

curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -X GET \
    "http://localhost:8108/collections/redemption_codes/documents/export?filter_by=id%3A%2A%20%7C%7C%20%24transactions%28id%3A%2A%29&include_fields=%24transactions%28%2A%29"


