From 8aaa3f56f5949e9fd456bff653b906e87d533c20 Mon Sep 17 00:00:00 2001 From: Haiyan Meng Date: Tue, 3 Dec 2019 10:31:36 -0800 Subject: [PATCH] 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. --- api/internal/crawl/index/elasticsearch.go | 44 +++++++++++++++++++---- api/internal/crawl/index/kustomize.go | 18 ++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/api/internal/crawl/index/elasticsearch.go b/api/internal/crawl/index/elasticsearch.go index 0d5dc6993..8893dc2e8 100644 --- a/api/internal/crawl/index/elasticsearch.go +++ b/api/internal/crawl/index/elasticsearch.go @@ -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) } diff --git a/api/internal/crawl/index/kustomize.go b/api/internal/crawl/index/kustomize.go index 68bfe4cd5..79833d3fd 100644 --- a/api/internal/crawl/index/kustomize.go +++ b/api/internal/crawl/index/kustomize.go @@ -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 }