Set the ElasticSearch index creation configuration

Currently, the `kustomize` index in ElasticSearch is using dynamic
mapping, which sets the types of all the fields to `text`. However,
`text` field type is good for full-text value matching, and not good for
exact-value matching.  For exact-value matching, the `keyword` filed
type should be used.
This commit is contained in:
Haiyan Meng
2019-12-03 10:31:36 -08:00
parent 4a7ade6421
commit 8aaa3f56f5
2 changed files with 56 additions and 6 deletions

View File

@@ -13,6 +13,40 @@ import (
"github.com/elastic/go-elasticsearch/v6/esapi"
)
const IndexConfig = `
{
"mappings": {
"_doc": {
"properties": {
"repositoryUrl": {
"type": "keyword"
},
"filePath": {
"type": "keyword"
},
"defaultBranch": {
"type": "keyword"
},
"document": {
"type": "text"
},
"creationTime": {
"type": "date"
},
"kinds": {
"type": "text"
},
"identifiers": {
"type": "text"
},
"values": {
"type": "text"
}
}
}
}
}`
// TODO(damienr74) Split index into reader and writer?
type index struct {
ctx context.Context
@@ -118,21 +152,19 @@ func (idx *index) UpdateSetting(settings []byte) error {
res, err, ignoreResponseBody)
}
// Create an index providing both the mappings and the settings.
func (idx *index) CreateIndex(mappings []byte, settings []byte) error {
request := byteJoin(`{ "mappings":`, mappings, `, "settings":`, settings, `}`)
// Create an index providing the config for both the mappings and the settings.
func (idx *index) CreateIndex(config []byte) error {
op := idx.client.Indices.Create
res, err := op(
idx.name,
op.WithBody(bytes.NewReader(request)),
op.WithBody(bytes.NewReader(config)),
op.WithContext(idx.ctx),
op.WithHuman(),
op.WithPretty(),
op.WithIncludeTypeName(true),
)
return idx.responseErrorOrNil(
fmt.Sprintf("could not create index with config '%s'", request),
fmt.Sprintf("could not create index with config '%s'", config),
res, err, ignoreResponseBody)
}

View File

@@ -96,6 +96,24 @@ func NewKustomizeIndex(ctx context.Context) (*KustomizeIndex, error) {
if err != nil {
return nil, err
}
indicesExistsOp := idx.client.Indices.Exists
resp, err := indicesExistsOp([]string{"kustomize"},
indicesExistsOp.WithContext(idx.ctx),
indicesExistsOp.WithPretty())
if err != nil {
return nil, err
}
if resp.StatusCode == 200 {
fmt.Printf("The kustomize index already exists\n")
} else {
fmt.Printf("Creating the kustomize index\n")
if err := idx.CreateIndex([]byte(IndexConfig)); err != nil {
return nil, err
}
}
return &KustomizeIndex{idx}, nil
}