0
0
Fork 0

add the ability to not store dynamic fields globally

This commit is contained in:
Marty Schoch 2016-01-12 17:27:11 -05:00
parent b7c03dae1a
commit 81f8509890
3 changed files with 66 additions and 4 deletions

View File

@ -331,11 +331,11 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
parsedDateTime, err := dateTimeParser.ParseDateTime(propertyValueString)
if err != nil {
// index as text
fieldMapping := NewTextFieldMapping()
fieldMapping := newTextFieldMappingDynamic()
fieldMapping.processString(propertyValueString, pathString, path, indexes, context)
} else {
// index as datetime
fieldMapping := NewDateTimeFieldMapping()
fieldMapping := newDateTimeFieldMappingDynamic()
fieldMapping.processTime(parsedDateTime, pathString, path, indexes, context)
}
}
@ -349,7 +349,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
}
} else if dm.Dynamic {
// automatic indexing behavior
fieldMapping := NewNumericFieldMapping()
fieldMapping := newNumericFieldMappingDynamic()
fieldMapping.processFloat64(propertyValFloat, pathString, path, indexes, context)
}
case reflect.Struct:
@ -362,7 +362,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
fieldMapping.processTime(property, pathString, path, indexes, context)
}
} else if dm.Dynamic {
fieldMapping := NewDateTimeFieldMapping()
fieldMapping := newDateTimeFieldMappingDynamic()
fieldMapping.processTime(property, pathString, path, indexes, context)
}
default:

View File

@ -16,6 +16,12 @@ import (
"github.com/blevesearch/bleve/document"
)
// control the default behavior for dynamic fields (those not explicitly mapped)
var (
IndexDynamic = true
StoreDynamic = true
)
// A FieldMapping describes how a specific item
// should be put into the index.
type FieldMapping struct {
@ -53,6 +59,13 @@ func NewTextFieldMapping() *FieldMapping {
}
}
func newTextFieldMappingDynamic() *FieldMapping {
rv := NewTextFieldMapping()
rv.Store = StoreDynamic
rv.Index = IndexDynamic
return rv
}
// NewNumericFieldMapping returns a default field mapping for numbers
func NewNumericFieldMapping() *FieldMapping {
return &FieldMapping{
@ -63,6 +76,13 @@ func NewNumericFieldMapping() *FieldMapping {
}
}
func newNumericFieldMappingDynamic() *FieldMapping {
rv := NewNumericFieldMapping()
rv.Store = StoreDynamic
rv.Index = IndexDynamic
return rv
}
// NewDateTimeFieldMapping returns a default field mapping for dates
func NewDateTimeFieldMapping() *FieldMapping {
return &FieldMapping{
@ -73,6 +93,13 @@ func NewDateTimeFieldMapping() *FieldMapping {
}
}
func newDateTimeFieldMappingDynamic() *FieldMapping {
rv := NewDateTimeFieldMapping()
rv.Store = StoreDynamic
rv.Index = IndexDynamic
return rv
}
// Options returns the indexing options for this field.
func (fm *FieldMapping) Options() document.IndexingOptions {
var rv document.IndexingOptions

View File

@ -324,3 +324,38 @@ func TestMappingWithTokenizerDeps(t *testing.T) {
t.Fatal(err)
}
}
func TestEnablingDisablingStoringDynamicFields(t *testing.T) {
data := map[string]interface{}{
"name": "bleve",
}
doc := document.NewDocument("x")
mapping := NewIndexMapping()
err := mapping.mapDocument(doc, data)
if err != nil {
t.Fatal(err)
}
for _, field := range doc.Fields {
if field.Name() == "name" && !field.Options().IsStored() {
t.Errorf("expected field 'name' to be stored, isn't")
}
}
StoreDynamic = false
defer func() {
StoreDynamic = true
}()
doc = document.NewDocument("y")
err = mapping.mapDocument(doc, data)
if err != nil {
t.Fatal(err)
}
for _, field := range doc.Fields {
if field.Name() == "name" && field.Options().IsStored() {
t.Errorf("expected field 'name' to be not stored, is")
}
}
}