This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.go
61 lines (50 loc) · 1.59 KB
/
search.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gotinydb
import (
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search"
)
type (
// SearchResult is returned bu *Collection.Search or *Collection.SearchWithOptions.
// It provides a easy listing of the result.
SearchResult struct {
BleveSearchResult *bleve.SearchResult
position int
c *Collection
}
// Response are returned by *SearchResult.NextResponse if the caller needs to
// have access to the byte stream
Response struct {
ID string
Content []byte
DocumentMatch *search.DocumentMatch
}
)
// Next fills up the destination by marshaling the saved byte stream.
// It returns an error if any and the coresponding id of the element.
func (s *SearchResult) Next(dest interface{}) (id string, err error) {
id, _, _, err = s.next(dest)
return id, err
}
// NextResponse fills up the destination by marshaling the saved byte stream.
// It returns the byte stream and the id of the document inside a Response pointer or an error if any.
func (s *SearchResult) NextResponse(dest interface{}) (resp *Response, _ error) {
resp = new(Response)
id, content, docMatch, err := s.next(dest)
if err != nil {
return nil, err
}
resp.ID = id
resp.Content = content
resp.DocumentMatch = docMatch
return resp, err
}
func (s *SearchResult) next(dest interface{}) (id string, content []byte, docMatch *search.DocumentMatch, err error) {
if s.position >= s.BleveSearchResult.Hits.Len() {
return "", nil, nil, ErrEndOfQueryResult
}
docMatch = s.BleveSearchResult.Hits[s.position]
id = docMatch.ID
content, err = s.c.Get(id, dest)
s.position++
return
}