From 7db27aeba1e7587fbbd5cf30f14f834fb2084487 Mon Sep 17 00:00:00 2001 From: Patrick Mezard Date: Tue, 22 Sep 2015 19:40:20 +0200 Subject: [PATCH] implement document static mappings DocumentMapping.Dynamic was ignored by everything but the marshalling code. Issue #235 --- index_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ mapping_document.go | 6 ++--- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/index_test.go b/index_test.go index 9e0a308c..5488adde 100644 --- a/index_test.go +++ b/index_test.go @@ -15,6 +15,7 @@ import ( "log" "os" "reflect" + "sort" "strings" "sync" "testing" @@ -1039,6 +1040,67 @@ func TestTermVectorArrayPositions(t *testing.T) { } } +func TestDocumentStaticMapping(t *testing.T) { + defer func() { + err := os.RemoveAll("testidx") + if err != nil { + t.Fatal(err) + } + }() + + m := NewIndexMapping() + m.DefaultMapping = NewDocumentStaticMapping() + m.DefaultMapping.AddFieldMappingsAt("Text", NewTextFieldMapping()) + m.DefaultMapping.AddFieldMappingsAt("Date", NewDateTimeFieldMapping()) + m.DefaultMapping.AddFieldMappingsAt("Numeric", NewNumericFieldMapping()) + + index, err := New("testidx", m) + if err != nil { + t.Fatal(err) + } + + doc1 := struct { + Text string + IgnoredText string + Numeric float64 + IgnoredNumeric float64 + Date time.Time + IgnoredDate time.Time + }{ + Text: "valid text", + IgnoredText: "ignored text", + Numeric: 10, + IgnoredNumeric: 20, + Date: time.Unix(1, 0), + IgnoredDate: time.Unix(2, 0), + } + + err = index.Index("a", doc1) + if err != nil { + t.Fatal(err) + } + + fields, err := index.Fields() + if err != nil { + t.Fatal(err) + } + sort.Strings(fields) + expectedFields := []string{"Date", "Numeric", "Text", "_all"} + if len(fields) != len(expectedFields) { + t.Fatalf("invalid field count: %d", len(fields)) + } + for i, expected := range expectedFields { + if expected != fields[i] { + t.Fatalf("unexpected field[%d]: %s", i, fields[i]) + } + } + + err = index.Close() + if err != nil { + t.Fatal(err) + } +} + func TestIndexEmptyDocId(t *testing.T) { defer func() { err := os.RemoveAll("testidx") diff --git a/mapping_document.go b/mapping_document.go index 0024be63..bdd36b4c 100644 --- a/mapping_document.go +++ b/mapping_document.go @@ -322,7 +322,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, for _, fieldMapping := range subDocMapping.Fields { fieldMapping.processString(propertyValueString, pathString, path, indexes, context) } - } else { + } else if dm.Dynamic { // automatic indexing behavior // first see if it can be parsed by the default date parser @@ -347,7 +347,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, for _, fieldMapping := range subDocMapping.Fields { fieldMapping.processFloat64(propertyValFloat, pathString, path, indexes, context) } - } else { + } else if dm.Dynamic { // automatic indexing behavior fieldMapping := NewNumericFieldMapping() fieldMapping.processFloat64(propertyValFloat, pathString, path, indexes, context) @@ -361,7 +361,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, for _, fieldMapping := range subDocMapping.Fields { fieldMapping.processTime(property, pathString, path, indexes, context) } - } else { + } else if dm.Dynamic { fieldMapping := NewDateTimeFieldMapping() fieldMapping.processTime(property, pathString, path, indexes, context) }