Skip to content

object ObjectApiResponse can't be used in 'await' expression #1976

Open
@772201902

Description

@772201902

elasticsearch-dsl~=8.17.1

I want to use ES's asynchronous query. I have verified that it is available during synchronization, but there will be an error when using the corresponding asynchronous library

The code is as follows:
EsClient = elasticsearch.Elasticsearch(config.esArgs.uri, basic_auth=(config.esArgs.user, config.esArgs.passwd))
query_obj = elasticsearch_dsl.AsyncSearch(using=EsClient, index=index)
query_obj = query_obj.query(body)
response =await query_obj.execute()
logging.debug(f"response: {response}")

Activity

miguelgrinberg

miguelgrinberg commented on May 12, 2025

@miguelgrinberg
Collaborator

This works well for me:

import asyncio
import logging
import os
import elasticsearch
import elasticsearch_dsl

logging.basicConfig(level=logging.INFO)

async def main():
    EsClient = elasticsearch.AsyncElasticsearch(os.environ['ELASTICSEARCH_URL'])
    query_obj = elasticsearch_dsl.AsyncSearch(using=EsClient, index='my-index')
    query_obj = query_obj.query(elasticsearch_dsl.query.MatchAll())
    response = await query_obj.execute()
    logging.info(f"response: {response}")
    await EsClient.close()

asyncio.run(main())

My guess is that your mistake is using the Elasticsearch client class, which is synchronous. You should use AsyncElasticsearch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      object ObjectApiResponse can't be used in 'await' expression · Issue #1976 · elastic/elasticsearch-dsl-py