elasticsearch-dsl 使用 from 和 size

Noa*_*oam 6 python-2.7 elasticsearch

我使用 python 2.7 和 Elasticsearch-DSL 包来查询我的弹性集群。

尝试向查询添加“来自和限制”功能,以便在我的 FE 中进行分页,该 FE 呈现文档弹性返回,但“来自”无法正常工作(即我没有正确使用它,我的配偶)。

相关代码是:

s = Search(using=elastic_conn, index='my_index'). \
       filter("terms", organization_id=org_list)

    hits = s[my_from:my_size].execute() # if from = 10, size = 10 then I get 0 documents, altought 100 documents match the filters.
Run Code Online (Sandbox Code Playgroud)

我的索引包含 100 个文档。即使我的过滤器匹配所有结果(即没有过滤掉任何内容), 例如,如果我使用my_from = 10and ,那么我在命中中什么也得不到(没有匹配的文档)my_size = 10

这是为什么?我是否滥用了 from ?

文件指出:

来自和尺寸参数。from 参数定义距您要获取的第一个结果的偏移量。size 参数允许您配置要返回的最大命中数。

所以这看起来很简单,我错过了什么?

小智 5

这个问题的答案可以在搜索 DSL 分页部分下的文档中找到:

分页

要指定 from/size 参数,请使用 Python 切片 API:

s = s[10:20]

# {"from": 10, "size": 10}
Run Code Online (Sandbox Code Playgroud)

在搜索 DSL 中正确使用这些参数就像使用 Python 列表一样,从起始索引切片到结束索引。大小参数将隐式定义为结束索引减去开始索引。

希望这能解决问题!


Aid*_*adi 5

尝试传递fromsize参数如下:

search = Search(using=elastic_conn, index='my_index'). \
        filter("terms", organization_id=org_list). \
        extra(from_=10, size=20)
result = search.execute()
Run Code Online (Sandbox Code Playgroud)