我的问题特定于我正在使用的“gopkg.in/olivere/elastic.v2”包。
我正在尝试返回与我的查询匹配的所有文档:
termQuery := elastic.NewTermQuery("item_id", item_id)
searchResult, err := es.client.Search().
Index(index).
Type(SegmentsType). // search segments type
Query(termQuery). // specify the query
Sort("time", true). // sort by "user" field, ascending
From(0).Size(9).
Pretty(true). // pretty print request and response JSON
Do() // execute
if err != nil {
// Handle error
return timeline, err
}
Run Code Online (Sandbox Code Playgroud)
问题是如果我将大小增加到较大的大小,我会收到内部服务器错误。如果我消除了以下内容:
From(0).Size(9).
Run Code Online (Sandbox Code Playgroud)
然后使用默认值(10 个文档)。我怎样才能退回所有文件?
使用滚动条来检索所有结果只是有点不同,为了简洁起见,我没有包括您可能需要的太多错误处理。
基本上你只需要稍微您的代码更改Search到Scroller,然后用循环Scroller调用Do和结果的处理页面。
termQuery := elastic.NewTermQuery("item_id", item_id)
scroller := es.client.Scroller().
Index(index).
Type(SegmentsType).
Query(termQuery).
Sort("time", true).
Size(1)
docs := 0
for {
res, err := scroller.Do(context.TODO())
if err == io.EOF {
// No remaining documents matching the search so break out of the 'forever' loop
break
}
for _, hit := range res.Hits.Hits {
// JSON parse or do whatever with each document retrieved from your index
item := make(map[string]interface{})
err := json.Unmarshal(*hit.Source, &item)
docs++
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3326 次 |
| 最近记录: |