Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 731
Vikash Singh Avatar asked Nov 19 '15 07:11

Vikash Singh


2 Answers

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

like image 61
Val Avatar answered Nov 13 '22 09:11

Val


In elasticsearch-dsl version 6.x.x fields() is deprecated and result with exception.

use source(fields=None, **kwargs) in order to projection columns

e.g.

s = Search()
s = s.source(["field1", "field2"])
s = Search()
s = s.source(include=['obj1.*'], exclude=["*.description"])
s = Search()
s = s.source(include=['obj1.*']).source(exclude=["*.description"])
like image 25
Ami Hollander Avatar answered Nov 13 '22 07:11

Ami Hollander