Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new FormatSpecifier option for FieldMapping #1430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mapping/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,12 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
if subDocMapping != nil {
// index by explicit mapping
for _, fieldMapping := range subDocMapping.Fields {
fieldMapping.processFloat64(propertyValFloat, pathString, path, indexes, context)
if fieldMapping.Type == "text" && fieldMapping.FormatSpecifier != "" {
fieldMapping.processString(fmt.Sprintf(fieldMapping.FormatSpecifier, propertyValFloat),
pathString, path, indexes, context)
} else {
fieldMapping.processFloat64(propertyValFloat, pathString, path, indexes, context)
}
}
} else if closestDocMapping.Dynamic {
// automatic indexing behavior
Expand Down
2 changes: 2 additions & 0 deletions mapping/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type FieldMapping struct {
// DocValues, if true makes the index uninverting possible for this field
// It is useful for faceting and sorting queries.
DocValues bool `json:"docvalues,omitempty"`

FormatSpecifier string `json:"format_specifier,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some commentary here that "%f" is the thing to use for correct translation of numeric (float) values?

}

// NewTextFieldMapping returns a default field mapping for text
Expand Down
50 changes: 50 additions & 0 deletions mapping/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,3 +1151,53 @@ func TestDefaultAnalyzerInheritance(t *testing.T) {
t.Fatalf("Expected analyzer: xyz to be inherited by field, but got: '%v'", analyzer)
}
}

func TestNumericValueWithFormatSpecifier(t *testing.T) {
numericTextField := NewTextFieldMapping()
numericTextField.FormatSpecifier = "%.0f"
mapping := NewIndexMapping()
mapping.DefaultMapping.AddFieldMappingsAt("productId", numericTextField)

doc := document.NewDocument("x")
err := mapping.MapDocument(doc, map[string]interface{}{
"productId": float64(20201001),
})
if err != nil {
t.Fatal(err)
}

if len(doc.Fields) != 1 {
t.Fatalf("expected 1 field, got: %d", len(doc.Fields))
}
if _, ok := doc.Fields[0].(*document.TextField); !ok {
t.Fatalf("expected field to be type *document.TextField, got %T", doc.Fields[0])
}
if string(doc.Fields[0].(*document.TextField).Value()) != "20201001" {
t.Fatalf("expected string value 20201001, got %s", string(doc.Fields[0].(*document.TextField).Value()))
}
}

func TestNumericValueWithFormatSpecifierZeroPadding(t *testing.T) {
numericTextField := NewTextFieldMapping()
numericTextField.FormatSpecifier = "%03.0f"
mapping := NewIndexMapping()
mapping.DefaultMapping.AddFieldMappingsAt("age", numericTextField)

doc := document.NewDocument("x")
err := mapping.MapDocument(doc, map[string]interface{}{
"age": float64(9),
})
if err != nil {
t.Fatal(err)
}

if len(doc.Fields) != 1 {
t.Fatalf("expected 1 field, got: %d", len(doc.Fields))
}
if _, ok := doc.Fields[0].(*document.TextField); !ok {
t.Fatalf("expected field to be type *document.TextField, got %T", doc.Fields[0])
}
if string(doc.Fields[0].(*document.TextField).Value()) != "009" {
t.Fatalf("expected string value 009, got %s", string(doc.Fields[0].(*document.TextField).Value()))
}
}