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

Fix index field count #675

Merged
Merged
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
8 changes: 5 additions & 3 deletions collector/indices_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ func NewIndicesMappings(logger log.Logger, client *http.Client, url *url.URL) *I
func countFieldsRecursive(properties IndexMappingProperties, fieldCounter float64) float64 {
// iterate over all properties
for _, property := range properties {
if property.Type != nil {
// property has a type set - counts as a field

if property.Type != nil && *property.Type != "object" {
// property has a type set - counts as a field unless the value is object
// as the recursion below will handle counting that
fieldCounter++

// iterate over all fields of that property
Expand All @@ -103,7 +105,7 @@ func countFieldsRecursive(properties IndexMappingProperties, fieldCounter float6

// count recursively in case the property has more properties
if property.Properties != nil {
fieldCounter = +countFieldsRecursive(property.Properties, fieldCounter)
fieldCounter = 1 + countFieldsRecursive(property.Properties, fieldCounter)
}
}

Expand Down
219 changes: 219 additions & 0 deletions collector/indices_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,222 @@ func TestMapping(t *testing.T) {
}
}
}

func TestIndexMappingFieldCount(t *testing.T) {

testIndexNumFields := 40.0
testIndexName := "test-data-2023.01.20"

rawMapping := `{
"test-data-2023.01.20": {
"mappings": {
"properties": {
"data": {
"type": "object",
"properties": {
"field1": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field10": {
"type": "long"
},
"field2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field3": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field4": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field5": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field6": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field7": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field8": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field9": {
"type": "long"
}
}
},
"data2": {
"properties": {
"field1": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field3": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field4": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field5": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"nested_field6": {
"properties": {
"field1": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field3": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field4": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"field5": {
"type": "long"
}
}
}
}
}
}
}
}
}`

for _, handler := range map[string]http.Handler{
"plain": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, rawMapping)
}),
} {

ts := httptest.NewServer(handler)
defer ts.Close()

u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
c := NewIndicesMappings(log.NewNopLogger(), http.DefaultClient, u)
indicesMappingsResponse, err := c.fetchAndDecodeIndicesMappings()
if err != nil {
t.Fatalf("Failed to fetch or decode indices mappings: %s", err)
}

response := *indicesMappingsResponse
mapping := response[testIndexName]
totalFields := countFieldsRecursive(mapping.Mappings.Properties, 0)
if totalFields != testIndexNumFields {
t.Errorf("Number of actual fields in index doesn't match the count returned by the recursive countFieldsRecursive function")
}

}

}