6
votes

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

3

3 Answers

12
votes

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

4
votes

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"])
0
votes

i use that for all fields :

from elasticsearch_dsl import Q, Search
from elasticsearch import Elasticsearch 

client = Elasticsearch('localhost:9200')

q = Q("multi_match", query='STRING_TO_SEARCH', fields=['_all'])
result = Search(index='YOUR_INDEX').using(client).query(q).execute()

or for specific fields :

fields=['name',]