diff --git a/mapping/document.go b/mapping/document.go index 6b903889..d62675e5 100644 --- a/mapping/document.go +++ b/mapping/document.go @@ -513,21 +513,25 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, } dm.walkDocument(property, path, indexes, context) case reflect.Ptr: - switch property := property.(type) { - case encoding.TextMarshaler: - txt, err := property.MarshalText() - if err == nil && subDocMapping != nil { - // index by explicit mapping - for _, fieldMapping := range subDocMapping.Fields { - if fieldMapping.Type == "text" { - fieldMapping.processString(string(txt), pathString, path, indexes, context) + if !propertyValue.IsNil() { + switch property := property.(type) { + case encoding.TextMarshaler: + + txt, err := property.MarshalText() + if err == nil && subDocMapping != nil { + // index by explicit mapping + for _, fieldMapping := range subDocMapping.Fields { + if fieldMapping.Type == "text" { + fieldMapping.processString(string(txt), pathString, path, indexes, context) + } } + } else { + dm.walkDocument(property, path, indexes, context) } - } else { + + default: dm.walkDocument(property, path, indexes, context) } - default: - dm.walkDocument(property, path, indexes, context) } default: dm.walkDocument(property, path, indexes, context) diff --git a/mapping/mapping_test.go b/mapping/mapping_test.go index e16052b1..5cd86015 100644 --- a/mapping/mapping_test.go +++ b/mapping/mapping_test.go @@ -19,6 +19,7 @@ import ( "fmt" "reflect" "testing" + "time" "github.com/blevesearch/bleve/analysis/tokenizer/exception" "github.com/blevesearch/bleve/analysis/tokenizer/regexp" @@ -966,3 +967,27 @@ func TestMappingForTextMarshaler(t *testing.T) { } } + +func TestMappingForNilTextMarshaler(t *testing.T) { + tm := struct { + Marshalable *time.Time + }{ + Marshalable: nil, + } + + // now verify that when a mapping explicity + m := NewIndexMapping() + txt := NewTextFieldMapping() + m.DefaultMapping.AddFieldMappingsAt("Marshalable", txt) + doc := document.NewDocument("x") + err := m.MapDocument(doc, tm) + if err != nil { + t.Fatal(err) + } + + if len(doc.Fields) != 0 { + t.Fatalf("expected 1 field, got: %d", len(doc.Fields)) + + } + +}