9

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 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks man. can you help me with this as well : stackoverflow.com/questions/33799486/…, thanks again :)
you can also do all fields with (["_all"]) which is useful.
@ user1903663 fields: _all won't work if _all is disabled, have you tried it?
It looks like .fields() method was changed to .source() in version 2.x of elasticsearch-dsl (link)
@AmiHollander are you talking about .fields() our .source()?
|
5

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

1 Comment

Does not work. s = Search() s.source([asdf, adsf]) s.to_dict() gives me a query without a source field
0

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',]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.