Errors using filters for analytics queries with AppSearch Python client API

Hello, I will appreciate some help to explain what is the correct way to use analytics filters with the AppSearch python client API?

I have an engine running in AppSearch and I want to track clicks and queries through tags. The AppSearch Analytics UI is good, but I want to download the data and do my own analysis. So I’m testing access through the Python API.
Looking the documentation at
https://github.com/elastic/enterprise-search-python/blob/main/docs/guide/app-search-api.asciidoc and https://www.elastic.co/guide/en/enterprise-search-clients/python/current/app-search-api.html#app-search-engine-apis
I couldn’t find any example of using the following functions

get_count_analytics()
get_top_clicks_analytics()
get_top_queries_analytics()

The functions are here:
https://github.com/elastic/enterprise-search-python/blob/main/elastic_enterprise_search/client/_app_search.py

I want to translate the following Analytic query to the API

curl -X POST "https:/xxxxx-mycloud-deployment.elastic-cloud.com/api/as/v1/engines/my-engine/analytics/queries" ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer private-xxxxxxxxxxxxx" ^
-d "{'filters': {'all': [{ 'tag': ['my-tag'] },  { 'date': { 'from': '2021-08-10T12:00:00+00:00', 'to': '2021-10-15T00:00:00+00:00'} } ] } }" "

A snippet of my code:

app_search = AppSearch(api_endpoint, http_auth= private_key)

signed_search_key = app_search.create_signed_search_key(
    api_key= app_search.http_auth,
    api_key_name="private-key",
    search_fields={
        "body": {}
    }
    )
 
my_filter1 = {'all': [{'tag': ['my-tag']}, {'date': {
    'from': '2021-08-10T12:00:00+00:00', 'to': '2021-10-15T00:00:00+00:00'}}]}
my_filter2 = {'filters': {'all': [{'tag': ['my-tag']}, {'date': {'from': '2021-08-10T12:00:00+00:00', 'to': '2021-10-15T00:00:00+00:00'}}]}}

print(app_search.get_top_queries_analytics(engine_name, filters=my_filter1))
print(app_search.get_top_queries_analytics(engine_name, filters=my_filter2))

Querying the engine without any filter works well , but when I use a filter (either of them) it gives the following error:

Traceback (most recent call last):
  File "c:/…my-project/appsearch_analytics.py", line 30, in <module>
    print(app_search.get_top_queries_analytics(engine_name, filters=my_filter2))
  File "…py38\lib\site-packages\elastic_enterprise_search\client\_app_search.py", line 3656, in get_top_queries_analytics
    return self.perform_request(
  File "…-py38\lib\site-packages\elastic_enterprise_search\client\_base.py", line 187, in perform_request
    return self.transport.perform_request(
  File "…-py38\lib\site-packages\elastic_transport\transport.py", line 311, in perform_request
    resp_status, resp_headers, data = connection.perform_request(
  File "…-py38\lib\site-packages\elastic_transport\connection\http_urllib3.py", line 251, in perform_request
    self._raise_error(
  File "…-py38\lib\site-packages\elastic_transport\connection\base.py", line 192, in _raise_error
    raise HTTP_EXCEPTIONS.get(status, APIError)(
elastic_transport.exceptions.BadRequestError: [400] {'errors': ['Filters contains an invalid value for item inside of field: filters root; must be an object']}

I’m still quite new to elastic, so any help on what is the correct way to format filters with the API will be really appreciated!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.