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
|
|
|
. "github.com/stevenferrer/solr-go/types"
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-09 19:15:43 +08:00
|
|
|
const (
|
|
|
|
|
collection = "multi-select-demo"
|
|
|
|
|
productsPath = "products.json"
|
|
|
|
|
)
|
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 {
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
Name: "category",
|
|
|
|
|
Type: "text_general",
|
|
|
|
|
Indexed: true,
|
|
|
|
|
Stored: true,
|
2020-06-09 02:45:17 +08:00
|
|
|
},
|
2020-06-09 19:15:43 +08:00
|
|
|
{
|
|
|
|
|
Name: "productType",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Indexed: true,
|
|
|
|
|
Stored: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "brand",
|
|
|
|
|
Type: "text_gen_sort",
|
|
|
|
|
Indexed: true,
|
|
|
|
|
Stored: true,
|
|
|
|
|
},
|
|
|
|
|
// sku fields
|
|
|
|
|
{
|
|
|
|
|
Name: "color",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Indexed: true,
|
|
|
|
|
Stored: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "size",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Indexed: true,
|
|
|
|
|
Stored: true,
|
2020-06-09 02:45:17 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:15:43 +08:00
|
|
|
// _text_ copy field
|
|
|
|
|
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 {
|
|
|
|
|
b, err := ioutil.ReadFile(productsPath)
|
|
|
|
|
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
|
|
|
}
|