|
| 1 | +import chai from "chai" |
| 2 | +import * as F from "../src" |
| 3 | +import _ from "lodash/fp" |
| 4 | + |
| 5 | +chai.expect() |
| 6 | +const expect = chai.expect |
| 7 | + |
| 8 | +describe("Elastic Tree Functions", () => { |
| 9 | + let input = { |
| 10 | + properties: { |
| 11 | + LineItem: { |
| 12 | + properties: { |
| 13 | + Category: { |
| 14 | + type: "text", |
| 15 | + fields: { |
| 16 | + exact: { type: "text", analyzer: "exact" }, |
| 17 | + untouched: { type: "keyword", ignore_above: 200 }, |
| 18 | + }, |
| 19 | + copy_to: ["FieldGroup.POLineItem", "FieldGroup.All"], |
| 20 | + analyzer: "default", |
| 21 | + search_analyzer: "default_search", |
| 22 | + }, |
| 23 | + CommodityCode: { |
| 24 | + type: "text", |
| 25 | + fields: { |
| 26 | + exact: { type: "text", analyzer: "exact" }, |
| 27 | + untouched: { type: "keyword", ignore_above: 200 }, |
| 28 | + }, |
| 29 | + copy_to: ["FieldGroup.POLineItem", "FieldGroup.All"], |
| 30 | + analyzer: "default", |
| 31 | + search_analyzer: "default_search", |
| 32 | + }, |
| 33 | + CommodityCode2: { |
| 34 | + type: "text", |
| 35 | + fields: { |
| 36 | + exact: { type: "text", analyzer: "exact" }, |
| 37 | + untouched: { type: "keyword", ignore_above: 300 }, |
| 38 | + }, |
| 39 | + copy_to: ["FieldGroup.POLineItem", "FieldGroup.All"], |
| 40 | + analyzer: "default", |
| 41 | + search_analyzer: "default_search", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + let expected = { |
| 49 | + dynamic_templates: { |
| 50 | + "LineItem.Category|LineItem.CommodityCode": { |
| 51 | + match: "LineItem.Category|LineItem.CommodityCode", |
| 52 | + mapping: { |
| 53 | + type: "text", |
| 54 | + fields: { |
| 55 | + exact: { type: "text", analyzer: "exact" }, |
| 56 | + untouched: { type: "keyword", ignore_above: 200 }, |
| 57 | + }, |
| 58 | + copy_to: ["FieldGroup.POLineItem", "FieldGroup.All"], |
| 59 | + analyzer: "default", |
| 60 | + search_analyzer: "default_search", |
| 61 | + }, |
| 62 | + }, |
| 63 | + "LineItem.CommodityCode2": { |
| 64 | + match: "LineItem.CommodityCode2", |
| 65 | + mapping: { |
| 66 | + type: "text", |
| 67 | + fields: { |
| 68 | + exact: { |
| 69 | + type: "text", |
| 70 | + analyzer: "exact", |
| 71 | + }, |
| 72 | + untouched: { |
| 73 | + type: "keyword", |
| 74 | + ignore_above: 300, |
| 75 | + }, |
| 76 | + }, |
| 77 | + copy_to: ["FieldGroup.POLineItem", "FieldGroup.All"], |
| 78 | + analyzer: "default", |
| 79 | + search_analyzer: "default_search", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + let Tree = F.tree((x) => x.properties) |
| 86 | + let flattenedLeaves = _.flow(Tree.flatten(), _.omitBy(Tree.traverse)) |
| 87 | + |
| 88 | + // let getDynamicMappings = _.flow( |
| 89 | + // flattenedLeaves, |
| 90 | + // F.stampKey('match'), |
| 91 | + // _.groupBy(x => JSON.stringify(_.omit('match', x))), |
| 92 | + // _.mapValues(_.map('match')), |
| 93 | + // _.invert, |
| 94 | + // _.mapValues(x => ({ mapping: JSON.parse(x) })), |
| 95 | + // _.mapKeys(_.replace(',', '|')), |
| 96 | + // F.stampKey('match'), |
| 97 | + // x => ({ dynamic_templates: x }) |
| 98 | + // ) |
| 99 | + |
| 100 | + let getDynamicMappings = _.flow( |
| 101 | + flattenedLeaves, |
| 102 | + F.mapIndexed((mapping, match) => ({ mapping, match })), |
| 103 | + _.groupBy((x) => JSON.stringify(x.mapping)), |
| 104 | + _.map((fields) => ({ |
| 105 | + match: _.map("match", fields).join("|"), |
| 106 | + mapping: fields[0].mapping, |
| 107 | + })), |
| 108 | + _.keyBy("match"), |
| 109 | + (x) => ({ dynamic_templates: x }) |
| 110 | + ) |
| 111 | + |
| 112 | + it("should pull out dynamic_mappings", () => { |
| 113 | + let output = getDynamicMappings(input) |
| 114 | + expect(output).to.deep.equal(expected) |
| 115 | + }) |
| 116 | +}) |
0 commit comments