forked from zlyuancn/zes-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_ids.go
More file actions
44 lines (36 loc) · 1001 Bytes
/
Copy pathsearch_ids.go
File metadata and controls
44 lines (36 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package zes_search
import (
"context"
"time"
"github.com/olivere/elastic/v7"
)
// 搜索ids, 功能同SearchIds
func SearchIdsWithTimeout(timeout time.Duration, ss *elastic.SearchService) (ids []string, resp *elastic.SearchResult, total int, err error) {
ctx, cancel := makeTimeoutCtx(timeout)
defer cancel()
return SearchIds(ctx, ss)
}
// 搜索ids, 返回匹配结果的id列表
func SearchIds(ctx context.Context, ss *elastic.SearchService) (ids []string, resp *elastic.SearchResult, total int, err error) {
if ctx == nil {
ctx = context.Background()
}
// 获取id不需要 _source
ss.FetchSourceContext(elastic.NewFetchSourceContext(false))
// 执行
resp, err = ss.Do(ctx)
if err != nil {
return
}
// 获取总数
total = int(resp.TotalHits())
if total == 0 || resp.Hits == nil || len(resp.Hits.Hits) == 0 {
return nil, resp, total, nil
}
// 写入id
ids = make([]string, len(resp.Hits.Hits))
for i, hit := range resp.Hits.Hits {
ids[i] = hit.Id
}
return
}