6

我正在使用 elasticsearch dsl 搜索 elasticsearch:https ://elasticsearch-dsl.readthedocs.org/en/latest/

如何在进行搜索查询时过滤特定字段:

我知道它在弹性搜索中受支持: https ://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-request-fields.html

只是不知道如何在 Elasticsearch-dsl 中做同样的事情

4

3 回答 3

12

当你有你的Search对象时,你可以调用.fields()它的函数并指定你想要返回的字段:

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

文档中没有明确提及,但是您可以在文件的源代码中fields看到该函数以及该函数的一些测试用例search.pyfields

更新

从 ES 2.x 开始,该fields()方法已重命名为source()

于 2015-11-19T08:09:07.670 回答
4

在 elasticsearch-dsl 版本 6.xxfields()中已弃用,结果出现异常。

用于列source(fields=None, **kwargs)_projection

例如

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"])
于 2018-03-26T06:26:28.870 回答
0

我将它用于所有领域:

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

或针对特定领域:

fields=['name',]
于 2018-03-08T11:38:28.827 回答