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

added ignore_malformed mapping parameter #19

Merged
merged 1 commit into from
Oct 25, 2022
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
12 changes: 8 additions & 4 deletions src/Elasticsearch.FSharp/Mapping/Attributes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type ElasticField([<Optional; DefaultParameterValue(null:string)>] fieldType:str
[<Optional; DefaultParameterValue(true)>] enabled:bool,
[<Optional; DefaultParameterValue(null:string)>] format:string,
[<Optional; DefaultParameterValue(false)>] useProperties:bool,
[<Optional; DefaultParameterValue(10)>] maxDepth:int) =
[<Optional; DefaultParameterValue(10)>] maxDepth:int,
[<Optional; DefaultParameterValue(false)>] 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
Expand All @@ -39,8 +41,9 @@ type ElasticSubField(fieldName: string,
[<Optional; DefaultParameterValue(true)>] enabled:bool,
[<Optional; DefaultParameterValue(null:string)>] format:string,
[<Optional; DefaultParameterValue(false)>] useProperties:bool,
[<Optional; DefaultParameterValue(10)>] maxDepth:int) =
inherit ElasticField(fieldType, analyzer, enabled, format, useProperties, maxDepth)
[<Optional; DefaultParameterValue(10)>] maxDepth:int,
[<Optional; DefaultParameterValue(false)>] ignoreMalformed:bool) =
inherit ElasticField(fieldType, analyzer, enabled, format, useProperties, maxDepth, ignoreMalformed)

member val FieldName = fieldName with get

Expand Down Expand Up @@ -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<string, PropertyMapping> =
Expand Down
2 changes: 2 additions & 0 deletions src/Elasticsearch.FSharp/Mapping/DSL.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type PropertyMapping = {
Type: string option
Analyzer: string option
Enabled: bool
IgnoreMalformed: bool
Format: string option
Properties: Dictionary<string, PropertyMapping> option
Fields: Dictionary<string, PropertyMapping> option
Expand All @@ -19,6 +20,7 @@ with
Type = None
Analyzer = None
Enabled = true
IgnoreMalformed = false
Format = None
Properties = None
Fields = None
Expand Down
3 changes: 3 additions & 0 deletions src/Elasticsearch.FSharp/Mapping/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion tests/Elasticsearch.FSharp.Tests/Mapping.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TestEntity = {

[<ElasticField("text")>]
[<ElasticSubField("raw", fieldType = "keyword")>]
[<ElasticSubField("integer", fieldType = "integer", ignoreMalformed=true)>]
[<ElasticSubField("en", fieldType = "text", analyzer = "english")>]
[<ElasticSubField("ru", fieldType = "text", analyzer = "russian")>]
title: string
Expand All @@ -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" }
}
Expand Down Expand Up @@ -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" }
}
Expand Down Expand Up @@ -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" }
}
Expand All @@ -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
Expand All @@ -132,6 +136,7 @@ let ``Type serializes correctly to put mappings json``() =
type Elastic_Message = {
[<ElasticField("keyword")>]
[<ElasticSubField("raw", fieldType="keyword")>]
[<ElasticSubField("integer", fieldType = "integer", ignoreMalformed = true)>]
[<ElasticSubField("ru", fieldType="text", analyzer="ru")>]
[<ElasticSubField("en", fieldType="text", analyzer="en")>]
id: string
Expand All @@ -156,6 +161,10 @@ let ``Recursive type serializes correctly``() =
"raw": {
"type": "keyword"
},
"integer": {
"ignore_malformed":true,
"type":"integer"
},
"ru": {
"type": "text",
"analyzer": "ru"
Expand All @@ -174,6 +183,10 @@ let ``Recursive type serializes correctly``() =
"raw": {
"type": "keyword"
},
"integer": {
"ignore_malformed":true,
"type":"integer"
},
"ru": {
"type": "text",
"analyzer": "ru"
Expand Down