# Errors using filters for analytics queries with AppSearch Python client API

**URL:** <https://discuss.elastic.co/t/errors-using-filters-for-analytics-queries-with-appsearch-python-client-api/287054>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [October 19, 2021, 7:14am UTC](https://discuss.elastic.co/t/errors-using-filters-for-analytics-queries-with-appsearch-python-client-api/287054 "2021-10-19T07:14:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcortez](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@pcortez](https://discuss.elastic.co/u/pcortez)\
**Post date:** [October 19, 2021, 7:14am UTC](https://discuss.elastic.co/t/errors-using-filters-for-analytics-queries-with-appsearch-python-client-api/287054/1 "2021-10-19T07:14:57Z")

</div>

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](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](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

```auto
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](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

```auto
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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 16, 2021, 7:15am UTC](https://discuss.elastic.co/t/errors-using-filters-for-analytics-queries-with-appsearch-python-client-api/287054/2 "2021-11-16T07:15:12Z")

</div>

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