Description
Elasticsearch version: 8.12.0
elasticsearch-py
version: 8.12.0
Description of the problem including expected versus actual behavior:
I am encountering a type mismatch error when attempting to use the Elasticsearch.search
method with the fields
parameter to retrieve a subset of stored fields from matched documents. According to the Elasticsearch documentation, passing a list of field names as strings to the fields
parameter should be sufficient for this purpose Elasticsearch Fields Request.
However, when I specify the fields
parameter as a list of strings, both Pylance and PyCharm raise type errors indicating that the expected type is t.Optional[t.Sequence[t.Mapping[str, t.Any]]]
, rather than t.Optional[t.Union[str, t.Sequence[str]]]
. This discrepancy seems inconsistent with the type hints used in other methods like search_mvt
or knn_search
, where the fields
parameter accepts a union of string or sequence of strings.
Despite the type checking errors reported by the IDEs, the actual execution of the search query does yield the expected results, with the returned hits including the requested fields. It appears that the type hints may need updating to reflect the correct usage of the fields
parameter for the search
method.
Steps to reproduce:
from elasticsearch import Elasticsearch
es_instance = Elasticsearch("http://localhost:9200")
query = {"match": {"title": {"query": "Framework Laptop"}}}
fields = ["title", "content"]
es_instance.search(index="products", query=query, fields=fields)
In this snippet, fields
is passed as a list of strings, but the type hint suggests that a sequence of mappings is expected. This leads to the type error in Pylance and PyCharm:

Could the type hint for the fields
parameter in the search
method be updated to align with the expected usage, or is there another way to address this issue without changing the type hint? Am I misunderstanding how to use the fields
parameter?