/
simple_search.go
91 lines (79 loc) · 2.49 KB
/
simple_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/registry"
"github.com/peterwilliams97/pdf-search/doclib"
)
const usage = `Usage: go run simple_search.go [OPTIONS] Adobe PDF
Performs a full text search for "Adobe PDF" in Bleve index "store.simple" that was created with
simple_index.go`
var persistDir = "store.simple"
func main() {
flag.StringVar(&persistDir, "s", persistDir, "Index store directory name.")
doclib.MakeUsage(usage)
flag.Parse()
doclib.SetLogging()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
indexPath := filepath.Join(persistDir, "bleve")
term := strings.Join(flag.Args(), " ")
fmt.Printf("term=%q\n", term)
fmt.Printf("indexPath=%q\n", indexPath)
// Open existing index.
index, err := bleve.Open(indexPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open Bleve index %q.\n", indexPath)
panic(err)
}
query := bleve.NewMatchQuery(term)
search := bleve.NewSearchRequest(query)
types, _ := registry.HighlighterTypesAndInstances()
fmt.Printf("Higlighters=%+v\n", types)
// search.Highlight = bleve.NewHighlightWithStyle("html")
search.Highlight = bleve.NewHighlight()
search.Fields = []string{"Contents"}
search.Highlight.Fields = search.Fields
searchResults, err := index.Search(search)
if err != nil {
panic(err)
}
fmt.Println("=================!!!=====================")
fmt.Printf("searchResults=%s\n", searchResults)
fmt.Println("=================***=====================")
if len(searchResults.Hits) == 0 {
fmt.Println("No matches")
os.Exit(0)
}
for i, hit := range searchResults.Hits {
text := hit.Fields["Contents"].(string)
locations := hit.Locations
contents := locations["Contents"]
// fmt.Printf("%2d: %s\n\tLocations=%T\n\tcontents=%T\n\tencoding=%#v\n",
// i, hit, hit.Locations, hit.Locations["Contents"], hit.Locations["Contents"]["encoding"])
fmt.Printf("%2d: %s Hit=%T Locations=%d %T contents=%d %T text=%d %T\n",
i, hit, hit,
len(locations), locations,
len(contents), contents,
len(text), text)
k := 0
for term, termLocations := range contents {
fmt.Printf("%6d: term=%q matches=%d\n", k, term, len(termLocations))
k++
for j, loc := range termLocations {
l := *loc
snip := text[l.Start:l.End]
fmt.Printf("%9d: %d [%d:%d] %q\n", j, l.Pos, l.Start, l.End, snip)
}
}
}
fmt.Println("=================@@@=====================")
fmt.Printf("term=%q\n", term)
fmt.Printf("indexPath=%q\n", indexPath)
}