From 122bb8e1c844f2ccc1204eee9bc88e9306e81c3f Mon Sep 17 00:00:00 2001 From: nikolay Date: Mon, 3 Oct 2022 15:03:33 +0300 Subject: [PATCH] added ignore_malformed mapping parameter --- src/Elasticsearch.FSharp/Mapping/Attributes.fs | 12 ++++++++---- src/Elasticsearch.FSharp/Mapping/DSL.fs | 2 ++ src/Elasticsearch.FSharp/Mapping/Json.fs | 3 +++ tests/Elasticsearch.FSharp.Tests/Mapping.fs | 15 ++++++++++++++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Elasticsearch.FSharp/Mapping/Attributes.fs b/src/Elasticsearch.FSharp/Mapping/Attributes.fs index 20d3055..03ac48c 100644 --- a/src/Elasticsearch.FSharp/Mapping/Attributes.fs +++ b/src/Elasticsearch.FSharp/Mapping/Attributes.fs @@ -21,12 +21,14 @@ type ElasticField([] fieldType:str [] enabled:bool, [] format:string, [] useProperties:bool, - [] maxDepth:int) = + [] maxDepth:int, + [] ignoreMalformed:bool) = inherit Attribute() member val FieldType = fieldType with get member val Analyzer = analyzer with get - member val Enabled = enabled with get + member val Enabled = enabled with get + member val IgnoreMalformed = ignoreMalformed with get member val Format = format with get member val UseProperties = useProperties with get member val MaxDepth = maxDepth with get @@ -39,8 +41,9 @@ type ElasticSubField(fieldName: string, [] enabled:bool, [] format:string, [] useProperties:bool, - [] maxDepth:int) = - inherit ElasticField(fieldType, analyzer, enabled, format, useProperties, maxDepth) + [] maxDepth:int, + [] ignoreMalformed:bool) = + inherit ElasticField(fieldType, analyzer, enabled, format, useProperties, maxDepth, ignoreMalformed) member val FieldName = fieldName with get @@ -76,6 +79,7 @@ let fieldToMapping (propAttr: ElasticField) : PropertyMapping = else Some propAttr.Format Enabled = propAttr.Enabled + IgnoreMalformed = propAttr.IgnoreMalformed } let rec private getTypePropertyMappings (t: Type) (depth: int) : Dictionary = diff --git a/src/Elasticsearch.FSharp/Mapping/DSL.fs b/src/Elasticsearch.FSharp/Mapping/DSL.fs index 96c6e61..17b0f28 100644 --- a/src/Elasticsearch.FSharp/Mapping/DSL.fs +++ b/src/Elasticsearch.FSharp/Mapping/DSL.fs @@ -10,6 +10,7 @@ type PropertyMapping = { Type: string option Analyzer: string option Enabled: bool + IgnoreMalformed: bool Format: string option Properties: Dictionary option Fields: Dictionary option @@ -19,6 +20,7 @@ with Type = None Analyzer = None Enabled = true + IgnoreMalformed = false Format = None Properties = None Fields = None diff --git a/src/Elasticsearch.FSharp/Mapping/Json.fs b/src/Elasticsearch.FSharp/Mapping/Json.fs index 23b3754..c3d9ec1 100644 --- a/src/Elasticsearch.FSharp/Mapping/Json.fs +++ b/src/Elasticsearch.FSharp/Mapping/Json.fs @@ -15,6 +15,9 @@ type PropertyMapping with member x.ToJson() = Json.makeObject [ if not x.Enabled then yield Json.makeKeyValue "enabled" (Json.boolToString x.Enabled) + + if x.IgnoreMalformed then + yield Json.makeKeyValue "ignore_malformed" (Json.boolToString x.IgnoreMalformed) match x.Type with | Some t -> yield Json.makeKeyValue "type" (Json.quoteString t) diff --git a/tests/Elasticsearch.FSharp.Tests/Mapping.fs b/tests/Elasticsearch.FSharp.Tests/Mapping.fs index 568a2cd..bfd696c 100644 --- a/tests/Elasticsearch.FSharp.Tests/Mapping.fs +++ b/tests/Elasticsearch.FSharp.Tests/Mapping.fs @@ -15,6 +15,7 @@ type TestEntity = { [] [] + [] [] [] title: string @@ -37,6 +38,7 @@ let ``Type serializes correctly``() = "type": "text", "fields": { "raw": { "type":"keyword" }, + "integer": { "ignore_malformed":true, "type":"integer" }, "en": { "type":"text", "analyzer":"english" }, "ru": { "type":"text", "analyzer":"russian" } } @@ -64,6 +66,7 @@ let ``Type serializes correctly with excluded type name``() = "type": "text", "fields": { "raw": { "type":"keyword" }, + "integer": { "ignore_malformed":true, "type":"integer" }, "en": { "type":"text", "analyzer":"english" }, "ru": { "type":"text", "analyzer":"russian" } } @@ -105,6 +108,7 @@ let ``Type serializes correctly with settings``() = "type": "text", "fields": { "raw": { "type":"keyword" }, + "integer": { "ignore_malformed":true, "type":"integer" }, "en": { "type":"text", "analyzer":"english" }, "ru": { "type":"text", "analyzer":"russian" } } @@ -122,7 +126,7 @@ let ``Type serializes correctly to put mappings json``() = let expected = [| """{"properties":{"id": { "type": "long" }}}""" - """{"properties":{"title": { "type": "text", "fields": { "raw": { "type":"keyword" }, "en": { "type":"text", "analyzer":"english" }, "ru": { "type":"text", "analyzer":"russian" } } }}}""" + """{"properties":{"title": { "type": "text", "fields": { "raw": { "type":"keyword" }, "integer": { "ignore_malformed":true, "type":"integer" }, "en": { "type":"text", "analyzer":"english" }, "ru": { "type":"text", "analyzer":"russian" } } }}}""" |] |> Array.map Helpers.removeWhitespace let actual = mappingJson @@ -132,6 +136,7 @@ let ``Type serializes correctly to put mappings json``() = type Elastic_Message = { [] [] + [] [] [] id: string @@ -156,6 +161,10 @@ let ``Recursive type serializes correctly``() = "raw": { "type": "keyword" }, + "integer": { + "ignore_malformed":true, + "type":"integer" + }, "ru": { "type": "text", "analyzer": "ru" @@ -174,6 +183,10 @@ let ``Recursive type serializes correctly``() = "raw": { "type": "keyword" }, + "integer": { + "ignore_malformed":true, + "type":"integer" + }, "ru": { "type": "text", "analyzer": "ru"