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.
1
Oct 28, 2023 (1 month ago)
Alberto
09:26 PMCould 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
09:32 PMid
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
09:33 PMTHANK YOU!!!
1
Typesense
Indexed 3015 threads (79% resolved)
Similar Threads
Python Error: Misusing `id` as a Query Field
Alberto was having trouble querying based on 'id' in their Python code. Kishore Nallan clarified that 'id' is a special field and cannot be queried by it just yet.
Sorting Results in Typesense and Handling Errors
Ramees asks about sorting results by distance in Typesense, entering a list of strings as a field, and resolving an error with `fields` format. Kishore Nallan assists with these issues and advises on storing timestamps and proper authentication.
Querying with Not-in in Typesense
Masahiro inquired about using not-in queries in Typesense. Kishore Nallan explained how to conduct such queries by using the "-" operator in the query string, and assisted Masahiro with issues stemming from a high number of exclusion tokens. The problem was eventually resolved by switching to the `multi_search` endpoint.