[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort on the referenced field, left JOIN #2082

Open
AilabWorker opened this issue Nov 28, 2024 · 0 comments
Open

Sort on the referenced field, left JOIN #2082

AilabWorker opened this issue Nov 28, 2024 · 0 comments

Comments

@AilabWorker
Copy link

Description

I have a queries collection which contains recommendations for the query. And when an account makes a search it is recorded in acount_query_searched collection as a history of searches.

When I am trying to join without sorting, everything works. When I try to sort, the following error is returned:
{'results': [{'code': 400, 'error': 'No references found to sort by on acount_query_searched.account_id.'}]}
Also when sorting is done like "sort_by": f"$acount_query_searched( _eval(account_id:={account_id}):desc)" I get the following error:
{'results': [{'code': 404, 'error': 'Referenced collection acount_query_searched: Could not find a field named _eval in the schema for sorting.'}]}

`

query_schema = {
    'name': 'queries',
    'fields': [
        {'name': 'query', 'type': 'string' }
    ]
}

account_schema = {
    'name': 'accounts',
    'fields': [
        {'name': 'id', 'type': 'string', 'sort': True },
        {"name": "username", "type": "string"}

    ]
}

acount_query_searched = {
  "name":  "acount_query_searched",
  "fields": [
    {"name": "account_id", "type": "string", "reference": "accounts.id", "sort": True},
    {"name": "query_id", "type": "string", "reference": "queries.id", "sort": True},
  ]
}

 "searches": [
        {
            "q": "My query",
            "query_by": "query",
            "collection": "queries",
            "filter_by": f"id:* || $acount_query_searched(account_id:={account_id})", 
            "include_fields": "id, query, $acount_query_searched(*)",
             "sort_by": "$acount_query_searched(account_id:desc)"
        }
    ]

Steps to reproduce

// 1. create queries shcema
curl -X POST "http://localhost:8108/collections" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "name": "queries",
  "fields": [
    {"name": "query", "type": "string"}
  ]
}'

//2. Create accounts Schema

curl -X POST "http://localhost:8108/collections" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "name": "accounts",
  "fields": [
    {"name": "id", "type": "string", "sort": true},
    {"name": "username", "type": "string"}
  ]
}

// 3. Create acount_query_searched Schema
curl -X POST "http://localhost:8108/collections" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "name": "acount_query_searched",
  "fields": [
    {"name": "account_id", "type": "string", "reference": "accounts.id", "sort": true},
    {"name": "query_id", "type": "string", "reference": "queries.id", "sort": true}
  ]
}'


// 4.  Populate queries Collection
curl -X POST "http://localhost:8108/collections/queries/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "id": "1",
  "query": "My query"
}'

curl -X POST "http://localhost:8108/collections/queries/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "id": "2",
  "query": "My queries"
}'



curl -X POST "http://localhost:8108/collections/accounts/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "id": "1",
  "username": "user1"
}'

curl -X POST "http://localhost:8108/collections/accounts/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "id": "2",
  "username": "user2"
}'



curl -X POST "http://localhost:8108/collections/acount_query_searched/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "account_id": "1",
  "query_id": "1"
}'

curl -X POST "http://localhost:8108/collections/acount_query_searched/documents" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "account_id": "2",
  "query_id": "1"
}'


curl -X POST "http://localhost:8108/multi_search" \
-H "X-TYPESENSE-API-KEY: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "searches": [
    {
      "q": "My query",
      "query_by": "query",
      "collection": "queries",
      "filter_by": "id:* || $acount_query_searched(account_id:=1)",
      "include_fields": "id, query, $acount_query_searched(*)",
      "sort_by": "$acount_query_searched(account_id:desc)"
    }
  ]
}'

Expected Behavior

My goal is to make a left join to the queries collection, so that searches made by that account are ranked higher than ones that are not.

Actual Behavior

{'results': [{'code': 400, 'error': 'No references found to sort by on acount_query_searched.account_id.'}]}

Metadata

Typesense Version: 27.1

OS: in docker containter image: typesense/typesense:27.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant