-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolr.go
63 lines (58 loc) · 1.54 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/vanng822/go-solr/solr"
)
func solrHandler(w http.ResponseWriter, r *http.Request) {
service := r.URL.Query().Get("service")
solrRoute := strings.ReplaceAll(service, "/", "")
solrConnectionStr := fmt.Sprintf("http://%s:8983/solr", solrRoute)
fmt.Fprintf(w, convertSolrDoc(solrConnector(solrConnectionStr), solrRoute))
}
func convertSolrDoc(d []solr.Document, version string) string {
solrDoctoString := fmt.Sprintf("%s", d)
results := strings.Fields(solrDoctoString)
var replaced []string
r := regexp.MustCompile(`[\[\]']+`)
for _, str := range results {
cleanSolrString := strings.ReplaceAll(str, "map", "")
replaced = append(replaced, r.ReplaceAllString(cleanSolrString, ""))
}
keyVals := connectorKeyValues(replaced)
solrHost := fmt.Sprintf(`"SERVICE_HOST=%s"`, version)
solrOutput := solrHost + "\n" + keyVals
return solrOutput
}
func solrConnector(connectionString string) []solr.Document {
si, err := solr.NewSolrInterface(connectionString, "mycore")
if err != nil {
log.Print(err)
}
si.DeleteAll()
d := solr.Document{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
d.Set(pair[0], pair[1])
if err != nil {
log.Print(err)
}
}
documents := []solr.Document{}
documents = append(documents, d)
si.Add(documents, 1, nil)
si.Commit()
query := solr.NewQuery()
query.Q("*:*")
query.FieldList("LAGOON_*")
s := si.Search(query)
r, err := s.Result(nil)
if err != nil {
log.Print("Error: ", err)
}
return r.Results.Docs
}