1
0
mirror of https://github.com/stevenferrer/multi-select-facet.git synced 2025-11-23 21:54:45 +02:00
Files
multi-select-facet/cmd/api/main.go

154 lines
3.0 KiB
Go
Raw Normal View History

2020-06-09 00:12:27 +08:00
package main
2020-06-09 02:45:17 +08:00
import (
"context"
2020-06-09 19:15:43 +08:00
"encoding/json"
"flag"
"io/ioutil"
2020-06-09 02:45:17 +08:00
"log"
2020-06-09 19:15:43 +08:00
"net/http"
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
2020-06-12 21:38:11 +08:00
"github.com/pkg/errors"
2020-06-09 02:45:17 +08:00
"github.com/stevenferrer/solr-go"
2020-06-09 19:15:43 +08:00
solrindex "github.com/stevenferrer/solr-go/index"
solrschema "github.com/stevenferrer/solr-go/schema"
2020-06-09 02:45:17 +08:00
)
2020-06-13 23:21:28 +08:00
// Any is a convenience type for interface{}
type Any = interface{}
// Map is a convenience type for map[string]interface{}
type Map = map[string]Any
2020-06-12 18:50:55 +08:00
2020-06-09 19:15:43 +08:00
const (
2020-06-13 23:21:28 +08:00
dataPath = "products.json"
2020-06-09 19:15:43 +08:00
)
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
func main() {
2020-06-13 23:21:28 +08:00
collection := flag.String("collection", "multi-select-demo", "specify the name of collection")
initSchema := flag.Bool("initialize-schema", false, "initialize solr schema")
index := flag.Bool("index-data", false, "index the data")
2020-06-09 19:15:43 +08:00
flag.Parse()
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
solrClient := solr.NewClient("localhost", 8983)
2020-06-09 02:45:17 +08:00
2020-06-12 21:38:11 +08:00
ctx := context.Background()
2020-06-09 19:15:43 +08:00
if *initSchema {
log.Print("initializing solr schema...")
2020-06-13 23:21:28 +08:00
err := initSolrSchema(ctx, *collection, solrClient.Schema())
2020-06-09 19:15:43 +08:00
if err != nil {
log.Fatal(err)
}
}
if *index {
log.Println("indexing products...")
2020-06-13 23:21:28 +08:00
err := indexProducts(ctx, *collection, solrClient.Index())
2020-06-09 19:15:43 +08:00
if err != nil {
log.Fatal(err)
}
}
r := chi.NewRouter()
// basic middlewares
r.Use(middleware.Logger)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
// search handler
r.Method(http.MethodGet, "/search", &searchHandler{
2020-06-13 23:21:28 +08:00
collection: *collection,
2020-06-09 19:15:43 +08:00
solrClient: solrClient,
})
addr := ":8081"
log.Printf("listening on %s\n", addr)
err := http.ListenAndServe(addr, r)
if err != nil {
log.Fatal(err)
}
2020-06-09 02:45:17 +08:00
}
2020-06-13 23:21:28 +08:00
func initSolrSchema(ctx context.Context, collection string,
schemaClient solrschema.Client) error {
2020-06-12 21:38:11 +08:00
// define the fields
2020-06-09 19:15:43 +08:00
fields := []solrschema.Field{
{
Name: "docType",
Type: "string",
Indexed: true,
Stored: true,
2020-06-09 02:45:17 +08:00
},
2020-06-09 19:15:43 +08:00
{
2020-06-12 18:50:55 +08:00
Name: "name",
2020-06-09 19:15:43 +08:00
Type: "text_general",
Indexed: true,
Stored: true,
2020-06-09 02:45:17 +08:00
},
2020-06-09 19:15:43 +08:00
{
2020-06-12 18:50:55 +08:00
Name: "category",
Type: "text_gen_sort",
2020-06-09 19:15:43 +08:00
Indexed: true,
Stored: true,
},
{
Name: "brand",
Type: "text_gen_sort",
Indexed: true,
Stored: true,
},
{
2020-06-12 18:50:55 +08:00
Name: "productType",
2020-06-09 19:15:43 +08:00
Type: "string",
Indexed: true,
Stored: true,
2020-06-09 02:45:17 +08:00
},
}
2020-06-09 19:15:43 +08:00
for _, field := range fields {
2020-06-12 21:38:11 +08:00
err := schemaClient.AddField(ctx, collection, field)
2020-06-09 19:15:43 +08:00
if err != nil {
2020-06-12 21:38:11 +08:00
return errors.Wrap(err, "add field")
2020-06-09 02:45:17 +08:00
}
2020-06-09 19:15:43 +08:00
}
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
return nil
}
2020-06-09 02:45:17 +08:00
2020-06-13 23:21:28 +08:00
func indexProducts(ctx context.Context, collection string,
indexClient solrindex.JSONClient) error {
2020-06-12 18:50:55 +08:00
b, err := ioutil.ReadFile(dataPath)
2020-06-09 19:15:43 +08:00
if err != nil {
return err
}
2020-06-09 02:45:17 +08:00
2020-06-13 23:21:28 +08:00
var docs []Map
2020-06-09 19:15:43 +08:00
err = json.Unmarshal(b, &docs)
if err != nil {
return err
}
2020-06-13 23:21:28 +08:00
err = indexClient.AddDocs(ctx, collection, docs)
if err != nil {
return err
}
// commit updates
err = indexClient.Commit(ctx, collection)
2020-06-09 19:15:43 +08:00
if err != nil {
return err
2020-06-09 02:45:17 +08:00
}
2020-06-09 00:12:27 +08:00
2020-06-09 19:15:43 +08:00
return nil
2020-06-09 02:45:17 +08:00
}