-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolr.go
145 lines (130 loc) · 3.12 KB
/
solr.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
package main
import (
"encoding/json"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
"strings"
"sync"
)
// Response is a SOLR response.
type Response struct {
Header struct {
Status int `json:"status"`
QTime int `json:"QTime"`
Params struct {
Query string `json:"q"`
CursorMark string `json:"cursorMark"`
Sort string `json:"sort"`
Rows string `json:"rows"`
} `json:"params"`
} `json:"header"`
Response struct {
NumFound int `json:"numFound"`
Start int `json:"start"`
Docs []json.RawMessage `json:"docs"` // dependent on SOLR schema
} `json:"response"`
NextCursorMark string `json:"nextCursorMark"`
}
// PrependSchema http, if missing.
func PrependSchema(s string) string {
if !strings.HasPrefix(s, "http") {
return fmt.Sprintf("http://%s", s)
}
return s
}
func ExportOne(wg *sync.WaitGroup, data chan<- string, server string, sortField string) {
defer wg.Done()
query := "*:*"
rows := 10
sort := sortField + " asc"
wt := "json"
server = PrependSchema(server)
v := url.Values{}
v.Set("q", query)
v.Set("sort", sort)
v.Set("rows", fmt.Sprintf("%d", rows))
v.Set("wt", wt)
v.Set("cursorMark", "*")
link := fmt.Sprintf("%s/query?%s", server, v.Encode())
log.Println(link)
resp, err := http.Get(link)
if err != nil {
log.Fatalf("http: %s", err)
}
var response Response
switch wt {
case "json":
// invalid character '\r' in string literal
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&response); err != nil {
log.Fatalf("decode: %s", err)
}
default:
log.Fatalf("wt=%s not implemented", wt)
}
// We do not defer, since we hard-exit on errors anyway.
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
for _, doc := range response.Response.Docs {
data <- string(doc)
}
}
func Export(wg *sync.WaitGroup, data chan<- string, server string, verbose bool, sortField string) {
defer wg.Done()
query := "*:*"
rows := 100
sort := sortField + " asc"
wt := "json"
flag.Parse()
server = PrependSchema(server)
v := url.Values{}
v.Set("q", query)
v.Set("sort", sort)
v.Set("rows", fmt.Sprintf("%d", rows))
v.Set("wt", wt)
v.Set("cursorMark", "*")
var total int
for {
link := fmt.Sprintf("%s/query?%s", server, v.Encode())
if verbose {
log.Println(link)
}
resp, err := http.Get(link)
if err != nil {
log.Fatalf("http: %s", err)
}
var response Response
switch wt {
case "json":
// invalid character '\r' in string literal
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&response); err != nil {
log.Fatalf("decode: %s", err)
}
default:
log.Fatalf("wt=%s not implemented", wt)
}
// We do not defer, since we hard-exit on errors anyway.
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
for _, doc := range response.Response.Docs {
data <- string(doc)
}
total += len(response.Response.Docs)
if verbose {
log.Printf("fetched %d docs", total)
}
if response.NextCursorMark == v.Get("cursorMark") {
break
}
v.Set("cursorMark", response.NextCursorMark)
}
if verbose {
log.Printf("fetched %d docs", total)
}
}