Skip to content

Commit

Permalink
Merge pull request #15 from varfrog/13
Browse files Browse the repository at this point in the history
13
  • Loading branch information
varfrog authored May 9, 2023
2 parents 0471c85 + b8e09a6 commit 8bfbe50
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Tests

on: [push]

jobs:
run_tests:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
2 changes: 2 additions & 0 deletions index_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Type string `json:"type"`
Format *string `json:"format,omitempty"`
IndexPrefixes *map[string]string `json:"index_prefixes,omitempty"`
Analyzer *string `json:"analyzer,omitempty"`
}
)

Expand Down Expand Up @@ -103,6 +104,7 @@ func (g *IndexGenerator) buildProperties(mappingProperties []MappingProperty) ma
node := leafNode{
Type: mp.FieldType,
Format: mp.FieldFormat,
Analyzer: mp.Analyzer,
IndexPrefixes: mp.IndexPrefixes,
}
m[mp.FieldName] = node
Expand Down
26 changes: 26 additions & 0 deletions index_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ func TestIndexGenerator_GenerateIndexJson_addsCustomProps(t *testing.T) {
}`))
}

func TestIndexGenerator_GenerateIndexJson_addsAnalyzer(t *testing.T) {
g := gomega.NewGomegaWithT(t)

mappingProperties := []MappingProperty{
{
FieldName: "company_name",
FieldType: "text",
Analyzer: MakePtr("standard"),
},
}

resultJson, err := NewIndexGenerator().GenerateIndexJson(mappingProperties, nil)
g.Expect(err).To(gomega.BeNil())

g.Expect(string(resultJson)).To(gomega.Equal(`{
"mappings": {
"properties": {
"company_name": {
"analyzer": "standard",
"type": "text"
}
}
}
}`))
}

func makeJsonObj(jsonBytes []byte) (map[string]interface{}, error) {
var m map[string]interface{}
if err := json.Unmarshal(jsonBytes, &m); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions mapping_properties_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (b *MappingPropertiesBuilder) addProperties(resolvedField fieldWrapper, map
}
}

analyzer := getTagOptionValue(resolvedField.field, tagKey, tagOptionAnalyzer)
if analyzer != "" {
mappingProperty.Analyzer = MakePtr(analyzer)
}

return nil
}

Expand Down
15 changes: 15 additions & 0 deletions mapping_properties_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@ func TestMappingPropertiesBuilder_BuildMappingProperties_SetsCustomProps(t *test
"max_chars": "10",
}))
}

func TestMappingPropertiesBuilder_BuildMappingProperties_SetsAnalyzer(t *testing.T) {
g := gomega.NewGomegaWithT(t)

type person struct {
CompanyName string `opensearch:"analyzer:keyword"`
}

builder := NewMappingPropertiesBuilder()
mps, err := builder.BuildMappingProperties(person{})
g.Expect(err).To(gomega.BeNil())
g.Expect(mps).To(gomega.HaveLen(1))
g.Expect(mps[0].Analyzer).ToNot(gomega.BeNil())
g.Expect(*mps[0].Analyzer).To(gomega.Equal("keyword"))
}
5 changes: 3 additions & 2 deletions opensearchutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ const (
tagKey = "opensearch"
tagOptionType = "type"
tagOptionFormat = "format"
tagOptionAnalyzer = "analyzer"
tagOptionIndexPrefixes = "index_prefixes"
)

// MappingProperty corresponds to mappings.properties of a mapping JSON. See
// https://opensearch.org/docs/1.3/opensearch/mappings/#explicit-mapping.
// MappingProperty corresponds to mappings.properties of a mapping JSON.
// MappingProperty defines either a primitive data type, in which case FieldType != "", or an object, in which case
// len(Children) > 0.
type MappingProperty struct {
FieldName string
FieldType string
FieldFormat *string
Analyzer *string
IndexPrefixes *map[string]string
Children []MappingProperty
}
Expand Down
4 changes: 4 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ func MakePtr[V any](v V) *V {
return &v
}

// getTagOptionValue gets a tag option value. For example, given a tag "type:keyword", getTagOptionValue("type")
// returns "keyword".
func getTagOptionValue(structField reflect.StructField, tagKey string, optionKey string) string {
const tagOptionSep = ","
const keyValSep = ":"
Expand All @@ -25,6 +27,8 @@ func getTagOptionValue(structField reflect.StructField, tagKey string, optionKey
return ""
}

// parseCustomPropertyValue parses a string like "min_chars=2;foo=bar" into a map like
// map[string]string{"min_chars":"2", "foo":"bar"}.
func parseCustomPropertyValue(str string) map[string]string {
const keyValSep = "="
pairs := strings.Split(str, ";")
Expand Down

0 comments on commit 8bfbe50

Please sign in to comment.