Skip to content

Commit e699cbe

Browse files
committed
Implement index creation
1 parent da965f2 commit e699cbe

File tree

14 files changed

+692
-0
lines changed

14 files changed

+692
-0
lines changed

client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package zinc
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/go-resty/resty/v2"
7+
// "github.com/goccy/go-json"
8+
)
9+
10+
// Client is a ZincSearch client.
11+
type Client struct {
12+
c *resty.Client
13+
}
14+
15+
// NewClient returns a fully initialized client.
16+
func NewClient(opts ...clientOpt) *Client {
17+
c := &Client{}
18+
19+
c.c = resty.New().
20+
SetHeader("Accept", "application/json")
21+
22+
c.c.JSONMarshal = json.Marshal
23+
c.c.JSONUnmarshal = json.Unmarshal
24+
25+
for _, opt := range opts {
26+
opt(c)
27+
}
28+
29+
return c
30+
}
31+
32+
// IndexService returns a new IndexService object.
33+
func (c *Client) IndexService() *IndexService {
34+
return NewIndexService(c)
35+
}

error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package zinc
2+
3+
// TODO: Add errors

example/create_index/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/fabmation-gmbh/zinc-go"
9+
"github.com/fabmation-gmbh/zinc-go/pkg/meta"
10+
)
11+
12+
func main() {
13+
const user = "admin"
14+
const passwd = "admin"
15+
16+
cli := zinc.NewClient(
17+
zinc.SetZincServer("http://localhost:4080"),
18+
zinc.SetBasicAuth(user, passwd),
19+
)
20+
21+
idx := cli.IndexService().
22+
Name("test_index").
23+
IndexStorageType(meta.IndexStorageDisk).
24+
AddMappingProperty(meta.NewIndexMappingProperty("@timestamp", meta.IdxPropertyDate)).
25+
AddMappingProperty(meta.NewIndexMappingProperty("text", meta.IdxPropertyText))
26+
27+
resp, err := idx.Create(context.Background())
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
fmt.Printf("==> %+v\n", resp)
33+
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/fabmation-gmbh/zinc-go
2+
3+
go 1.18
4+
5+
require github.com/go-resty/resty/v2 v2.7.0
6+
7+
require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
2+
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
3+
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
4+
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
5+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
8+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
9+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

index.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package zinc
2+
3+
import (
4+
"context"
5+
6+
"github.com/fabmation-gmbh/zinc-go/pkg/meta"
7+
)
8+
9+
// IndexService provides methods to manage indicies.
10+
type IndexService struct {
11+
cli *Client
12+
13+
// name is the index name.
14+
name string
15+
storageType meta.IndexStorageType
16+
props map[string]meta.IndexMappingProperty
17+
}
18+
19+
// NewIndexService returns a fully initialized IndexService.
20+
func NewIndexService(cli *Client) *IndexService {
21+
return &IndexService{
22+
cli: cli,
23+
props: make(map[string]meta.IndexMappingProperty),
24+
}
25+
}
26+
27+
// Name sets the name of the index.
28+
//
29+
// The IndexService object is returned to allow stacking method calls.
30+
func (i *IndexService) Name(name string) *IndexService {
31+
i.name = name
32+
33+
return i
34+
}
35+
36+
// IndexStorageType sets the storage type of the index.
37+
//
38+
// The IndexService object is returned to allow stacking method calls.
39+
func (i *IndexService) IndexStorageType(storage meta.IndexStorageType) *IndexService {
40+
i.storageType = storage
41+
42+
return i
43+
}
44+
45+
// AddMappingProperty adds the given index property to the index.
46+
//
47+
// The IndexService object is returned to allow stacking method calls.
48+
func (i *IndexService) AddMappingProperty(prop meta.IndexMappingProperty) *IndexService {
49+
i.props[prop.Name] = prop
50+
51+
return i
52+
}
53+
54+
// Create creates the index.
55+
func (i *IndexService) Create(ctx context.Context) (IndexCreateResponse, error) {
56+
type props struct {
57+
Props map[string]meta.IndexMappingProperty `json:"properties"`
58+
}
59+
type idxCreateReq struct {
60+
Name string `json:"name"`
61+
StorageType string `json:"storage_type"`
62+
Mappings props `json:"mappings,omitempty"`
63+
}
64+
65+
data := idxCreateReq{
66+
Name: i.name,
67+
StorageType: i.storageType.String(),
68+
Mappings: props{
69+
Props: i.props,
70+
},
71+
}
72+
73+
var resp IndexCreateResponse
74+
75+
_, err := i.cli.c.R().
76+
SetResult(&resp).
77+
SetBody(data).
78+
Post("/index")
79+
if err != nil {
80+
return IndexCreateResponse{}, err
81+
}
82+
83+
return resp, nil
84+
}

opts.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package zinc
2+
3+
// clientOpt is a Client option function.
4+
type clientOpt func(*Client)
5+
6+
// SetZincServer sets the Zinc server URL.
7+
func SetZincServer(url string) clientOpt {
8+
return clientOpt(func(c *Client) {
9+
c.c.SetHostURL(url + "/api")
10+
})
11+
}
12+
13+
// SetBasicAuth configures the client to authenticate with Basic Auth.
14+
func SetBasicAuth(username, password string) clientOpt {
15+
return clientOpt(func(c *Client) {
16+
c.c.SetBasicAuth(username, password)
17+
})
18+
}

pkg/meta/analyzer.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package meta
2+
3+
// Analyzer is a single text analyzer or token generator.
4+
type Analyzer int16
5+
6+
//go:generate enumer -type=Analyzer -json -trimprefix=Analyzer -transform=lower
7+
8+
// TODO: Support remaining analyzer and filter.
9+
10+
const (
11+
// AnalyzerUnset indicates that no analyzer is set.
12+
AnalyzerUnset Analyzer = iota
13+
// AnalyzerStandard is the standard analyzer.
14+
AnalyzerStandard
15+
// AnalyzerSimple is the simple analyzer.
16+
AnalyzerSimple
17+
AnalyzerKeyword
18+
AnalyzerWeb
19+
AnalyzerRegexp
20+
AnalyzerStop
21+
AnalyzerWhitespace
22+
)

pkg/meta/analyzer_enumer.go

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)