#community-help

Resolving 'Cannot use `id` as a query by field' error in Typesense

TLDR Alberto was encountering a 'Cannot use id as a query by field' error in Typesense. Jason clarified that the 'id' field is used internally and suggested duplicating data in a new field for searching.

Powered by Struct AI

1

Oct 28, 2023 (1 month ago)
Alberto
Photo of md5-f7dd019896dcb8aafdeb865ce17569b2
Alberto
09:26 PM
Hello every one.
Could please somebody help me with this error! It is drving me crazy, I cannot find what is wrong.
I'm using python3.10.11

This is my schema

my_schema = {
'name': 'oias',
'fields': [
{'name': 'number', 'type': 'int32'},
{'name': 'id', 'type': 'string'},
{'name': 'name', 'type': 'string'},
]
]

This is an example of my jsonl

{"number": 1, "name": "name_1", "id": "A000001"}
{"number": 2, "name": "name_2", "id": "A000002"}
{"number": 3, "name": "name_3", "id": "A000003"}
{"number": 4, "name": "name_4", "id": "A000004"}

When I do the search:

search_parameters = {
'q': A000002
'query_by': 'id',
"exhaustive_search": 'true',
}

client.collections['oias'].documents.search(search_parameters)

I get this error:

RequestMalformed: [Errno 400] Cannot use id as a query by field.

PLEASE, HELP!
I would really appreciate any comment

###########################
HERE IS THE WHOLE LOG:
###########################
---------------------------------------------------------------------------
RequestMalformed Traceback (most recent call last)
/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb Cell 11 line 1
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=4|5> print('################################################################################')
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=5|6> search_parameters = {
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=6|7> 'q': query_str,
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=7|8> 'query_by': 'id',
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=8|9> "exhaustive_search": 'true',
<vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=9|10> }
---> <vscode-notebook-cell:/Users/beto/Documents/Projects/search_engine/10_oia_typesense_parallel_search.ipynb#X12sZmlsZQ%3D%3D?line=11|12> client.collections['oias'].documents.search(search_parameters)

File ~/Documents/Projects/search_engine/venv/lib/python3.10/site-packages/typesense/documents.py:104, in Documents.search(self, search_parameters)
102 stringified_search_params = stringify_search_params(search_parameters)
103 validate_search(stringified_search_params)
--> 104 return self.api_call.get(self._endpoint_path('search'), stringified_search_params)

File ~/Documents/Projects/search_engine/venv/lib/python3.10/site-packages/typesense/api_call.py:146, in ApiCall.get(self, endpoint, params, as_json)
144 def get(self, endpoint, params=None, as_json=True):
145 params = params or {}
--> 146 return self.make_request(session.get, endpoint, as_json,
147 params=params,
148 timeout=self.config.connection_timeout_seconds)

File ~/Documents/Projects/search_engine/venv/lib/python3.10/site-packages/typesense/api_call.py:116, in ApiCall.make_request(self, fn, endpoint, as_json, *kwargs)
*114
error_message = 'API error.'
115 # Raised exception will be caught and retried
--> 116 raise ApiCall.get_exception(r.status_code)(r.status_code, error_message)
118 return r.json() if as_json else r.text
119 except (requests.exceptions.Timeout, requests.exceptions.ConnectionError, requests.exceptions.HTTPError,
120 requests.exceptions.RequestException, requests.exceptions.SSLError,
121 HTTPStatus0Error, ServerError, ServiceUnavailable) as e:
122 # Catch the exception and retry

RequestMalformed: [Errno 400] Cannot use id as a query by field.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:32 PM
id is a special field in Typesense that is used internally to identify documents and it cannot be used for searching. If you need to search on the contents of the id field, you want to duplicate the same data in new field and use that in the q parameter
Alberto
Photo of md5-f7dd019896dcb8aafdeb865ce17569b2
Alberto
09:33 PM
oh! that make sense!!
THANK YOU!!!

1