feat(esmole): mapping flatten

This commit is contained in:
admin committed 2026-06-16 20:48:10 +05:00
1 parent 7381db1189
commit b2a9c3323f
2 files changed
+77

No files matched your search

@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { flattenMapping } from '../../../src/es/mapping.js'
const raw = {
myindex: {
mappings: {
properties: {
id: { type: 'keyword' },
title: { type: 'text', fields: { raw: { type: 'keyword' } } },
author: { properties: { name: { type: 'text' }, age: { type: 'integer' } } }
}
}
}
}
describe('flattenMapping', () => {
it('flattens nested properties to dotted field:type rows', () => {
const rows = flattenMapping(raw)
expect(rows).toContainEqual({ field: 'id', type: 'keyword' })
expect(rows).toContainEqual({ field: 'title', type: 'text' })
expect(rows).toContainEqual({ field: 'title.raw', type: 'keyword' })
expect(rows).toContainEqual({ field: 'author.name', type: 'text' })
expect(rows).toContainEqual({ field: 'author.age', type: 'integer' })
})
it('handles multiple indices by prefixing the index name', () => {
const rows = flattenMapping({
a: { mappings: { properties: { x: { type: 'long' } } } },
b: { mappings: { properties: { y: { type: 'date' } } } }
})
expect(rows).toContainEqual({ index: 'a', field: 'x', type: 'long' })
expect(rows).toContainEqual({ index: 'b', field: 'y', type: 'date' })
})
it('returns empty array for an empty mapping', () => {
expect(flattenMapping({ idx: { mappings: {} } })).toEqual([])
})
it('returns empty array for a non-object input', () => {
expect(flattenMapping(null)).toEqual([])
expect(flattenMapping('nope')).toEqual([])
})
})