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/api/main.go

158 lines
2.9 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-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-12 18:50:55 +08:00
type M = map[string]interface{}
2020-06-09 19:15:43 +08:00
const (
2020-06-12 18:50:55 +08:00
collection = "multi-select-demo"
dataPath = "phones.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() {
initSchema := flag.Bool("init-schema", false, "initialize solr schema")
index := flag.Bool("index", false, "index products")
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-09 19:15:43 +08:00
if *initSchema {
log.Print("initializing solr schema...")
err := initSolrSchema(solrClient.Schema())
if err != nil {
log.Fatal(err)
}
}
if *index {
log.Println("indexing products...")
err := indexProducts(solrClient.Index())
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{
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-09 19:15:43 +08:00
func initSolrSchema(schemaClient solrschema.Client) error {
2020-06-12 18:50:55 +08:00
// the sku fields are not defined in here coz they are
// assumed to be dynamic (i.e. by adding a type suffix)
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-12 18:50:55 +08:00
// copy field
2020-06-09 19:15:43 +08:00
copyFields := []solrschema.CopyField{
{
Source: "*",
Dest: "_text_",
},
}
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
ctx := context.Background()
2020-06-09 02:45:17 +08:00
2020-06-09 19:15:43 +08:00
var err error
for _, field := range fields {
err = schemaClient.AddField(ctx, collection, field)
if err != nil {
return err
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
for _, copyField := range copyFields {
err = schemaClient.AddCopyField(ctx, collection, copyField)
if err != nil {
return err
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-09 19:15:43 +08:00
func indexProducts(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-09 19:15:43 +08:00
var docs []M
err = json.Unmarshal(b, &docs)
if err != nil {
return err
}
err = indexClient.AddMultiple(context.Background(), collection, docs)
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
}