-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
56 lines (46 loc) · 1.13 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
package rindex
import (
"io/ioutil"
"path/filepath"
"github.com/blugelabs/bluge/search"
)
type SearchResultVisitor = func() bool
type FieldVisitor = func(field string, value []byte) bool
func (i Indexer) readyForSearch() bool {
files, err := ioutil.ReadDir(i.IndexPath)
if err != nil {
return false
}
for _, f := range files {
if ok, _ := filepath.Match("*.seg", f.Name()); ok {
return true
}
}
return false
}
// Search searches the index and calls srVisitor for every result obtained and
// fVisitor for every field in that search result.
func (i *Indexer) Search(query string, fVisitor FieldVisitor, srVisitor SearchResultVisitor) (uint64, error) {
if !i.readyForSearch() {
return 0, ErrSearchNotReady
}
count := uint64(0)
err := i.IndexEngine.Search(query, func(iter search.DocumentMatchIterator) error {
match, err := iter.Next()
for err == nil && match != nil {
count++
if fVisitor != nil {
err = match.VisitStoredFields(fVisitor)
if err != nil {
return err
}
}
if srVisitor != nil && !srVisitor() {
break
}
match, err = iter.Next()
}
return err
})
return count, err
}