Skip to content
Snippets Groups Projects
Select Git revision
  • 4a4d4a2b9cbefee413a0ce1acaa5d699200d4636
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

document_store.go

Blame
  • 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 {