Select Git revision
document_store.go
document_store.go 7.34 KiB
package search
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"strings"
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
)
type DocumentStore struct {
w *Writer
name string
dataType reflect.Type
settings Settings
mappings Mappings
jsonSettings []byte
jsonMappings []byte
created bool
}
// NewDocumentStore purpose:
//
// create a document store index to write e.g. orders then allow one to search them
//
// parameters:
//
// name must be the complete openSearch index name e.g. "uafrica-v3-orders"
// tmpl must be your document data struct consisting of public fields as:
// Xxx string `json:"<name>" search:"keyword|text|long|date"` (can later add more types)
// Xxx time.Time `json:"<name>"` assumes type "date" for opensearch
// Xxx int `json:"<name>"` assumes type "long" for opensearch, specify keyword if required
func (w *Writer) NewDocumentStore(name string, tmpl interface{}) (DocumentStore, error) {
ds := DocumentStore{}
if !indexNameRegex.MatchString(name) {
return ds, errors.Errorf("invalid index_name:\"%s\"", name)
}
// if already created, just return
if existingDocumentStore, ok := w.documentStoreByName[name]; ok {
return existingDocumentStore, nil
}
structType := reflect.TypeOf(tmpl)
if tmpl == nil || structType.Kind() != reflect.Struct {
return ds, errors.Errorf("%T is not a struct", tmpl)
}
ds = DocumentStore{
w: w,
name: name,
dataType: structType,
created: false,
}
// define the OpenSearch index mapping
ds.settings = Settings{
Index: &SettingsIndex{
NumberOfShards: 4,
NumberOfReplicas: 0,
},
}
if properties, err := structMappingProperties(structType); err != nil {