-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
169 lines (150 loc) · 4.6 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package main
import (
"encoding/json"
_ "expvar"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/blevesearch/bleve/v2"
bleveHttp "github.com/blevesearch/bleve/v2/http"
)
var batchSize = flag.Int("batchSize", 100, "batch size for indexing")
var bindAddr = flag.String("addr", ":8094", "http listen address")
var jsonDir = flag.String("jsonDir", "data/", "json directory")
var indexPath = flag.String("index", "beer-search.bleve", "index path")
var staticEtag = flag.String("staticEtag", "", "A static etag value.")
var staticPath = flag.String("static", "static/", "Path to the static content")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var memprofile = flag.String("memprofile", "", "write mem profile to file")
func main() {
flag.Parse()
log.Printf("GOMAXPROCS: %d", runtime.GOMAXPROCS(-1))
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
// open the index
beerIndex, err := bleve.Open(*indexPath)
if err == bleve.ErrorIndexPathDoesNotExist {
log.Printf("Creating new index...")
// create a mapping
indexMapping, err := buildIndexMapping()
if err != nil {
log.Fatal(err)
}
beerIndex, err = bleve.New(*indexPath, indexMapping)
if err != nil {
log.Fatal(err)
}
// index data in the background
go func() {
err = indexBeer(beerIndex)
if err != nil {
log.Fatal(err)
}
pprof.StopCPUProfile()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}()
} else if err != nil {
log.Fatal(err)
} else {
log.Printf("Opening existing index...")
}
// create a router to serve static files
router := staticFileRouter()
// add the API
bleveHttp.RegisterIndexName("beer", beerIndex)
searchHandler := bleveHttp.NewSearchHandler("beer")
router.Handle("/api/search", searchHandler).Methods("POST")
listFieldsHandler := bleveHttp.NewListFieldsHandler("beer")
router.Handle("/api/fields", listFieldsHandler).Methods("GET")
debugHandler := bleveHttp.NewDebugDocumentHandler("beer")
debugHandler.DocIDLookup = docIDLookup
router.Handle("/api/debug/{docID}", debugHandler).Methods("GET")
// start the HTTP server
http.Handle("/", router)
log.Printf("Listening on %v", *bindAddr)
log.Fatal(http.ListenAndServe(*bindAddr, nil))
}
func indexBeer(i bleve.Index) error {
// open the directory
dirEntries, err := ioutil.ReadDir(*jsonDir)
if err != nil {
return err
}
// walk the directory entries for indexing
log.Printf("Indexing...")
count := 0
startTime := time.Now()
batch := i.NewBatch()
batchCount := 0
for _, dirEntry := range dirEntries {
filename := dirEntry.Name()
// read the bytes
jsonBytes, err := ioutil.ReadFile(*jsonDir + "/" + filename)
if err != nil {
return err
}
// parse bytes as json
var jsonDoc interface{}
err = json.Unmarshal(jsonBytes, &jsonDoc)
if err != nil {
return err
}
ext := filepath.Ext(filename)
docID := filename[:(len(filename) - len(ext))]
batch.Index(docID, jsonDoc)
batchCount++
if batchCount >= *batchSize {
err = i.Batch(batch)
if err != nil {
return err
}
batch = i.NewBatch()
batchCount = 0
}
count++
if count%1000 == 0 {
indexDuration := time.Since(startTime)
indexDurationSeconds := float64(indexDuration) / float64(time.Second)
timePerDoc := float64(indexDuration) / float64(count)
log.Printf("Indexed %d documents, in %.2fs (average %.2fms/doc)", count, indexDurationSeconds, timePerDoc/float64(time.Millisecond))
}
}
// flush the last batch
if batchCount > 0 {
err = i.Batch(batch)
if err != nil {
log.Fatal(err)
}
}
indexDuration := time.Since(startTime)
indexDurationSeconds := float64(indexDuration) / float64(time.Second)
timePerDoc := float64(indexDuration) / float64(count)
log.Printf("Indexed %d documents, in %.2fs (average %.2fms/doc)", count, indexDurationSeconds, timePerDoc/float64(time.Millisecond))
return nil
}