Search code examples
pythonelasticsearchelasticsearch-dsl

How to select specific fields in elasticsearch-dsl python


I am using elasticsearch dsl to search on elasticsearch : https://elasticsearch-dsl.readthedocs.org/en/latest/

How can i filter specific fields while making a search query:

I know its supported in elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-request-fields.html

Just dont know how to do the same in Elasticsearch-dsl


Solution

  • When you have your Search object you can call the .fields() function on it and specify which fields you want to return:

    s = search.Search()
    s = s.fields(["field1", "field2"])
    ...
    

    It's not explicitly mentioned in the documentation, but you can see the fields function in the source for the search.py file and some test cases for the fields function

    UPDATE

    From ES 2.x onwards, the fields() method has been renamed to source()