Convert the dynamic long datatype field to string in Elasticsearch .NET 8.x client

199 views Asked by At

My data is dynamic, some fileds have incorrect values, when i index/insert first time data below

   // other attributes....

      "my_key-sp1": [
            {
                "locale": null,
                "scope": null,
                "data": "60"
            }
        ],
  
// other attributes....

data is parsed as long datatype other product may have incorrect value like below


// other attributes....

  "my_key-sp1": [
        {
            "locale": null,
            "scope": null,
            "data": "60/ep"
        }
    ],

// other attributes....


now as you know this is incorrect value to store in the index

i tried to diable numeric detection, maybe let me store all long as text/string

`

var createIndexResponse = client.Indices.Create("my_index", c => c
    .Map(m => m
        .NumericDetection(false)
    )
);

` and i think this is the right solution

var createIndexResponse = client.Indices.Create("my_index", c => c
    .Map(m => m
        .DynamicTemplates(dt => dt
            .DynamicTemplate("longs_as_strings", d => d
                .MatchMappingType("string")
                .Match("long_*")
                .Unmatch("*_text")
                .Mapping(mm => mm
                    .Number(n => n
                        .Type(NumberType.Long)
                    )
                )
            )
        )
    )
);

check the image below Map function is not there in my ES client

I think with the new client i can do this by following

var createIndexResponse = elasticsearchClient.Indices.Create("my_index",c=>c.Mappings(m=>m.DynamicTemplates(HOW_To_USE_PARAMS_HERE))but not sure how to use it

1

There are 1 answers

0
Alap On

Got a solution guys, sharing so it can help others, so 1 way around is I can add template for my index where i can set this rule query

 POST _template/my_templatename_here
  {
  "index_patterns": [
    "my_temp_name_index_*"
  ],
  "aliases": {},
  "mappings": {
    "date_detection": true,
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match": "*",
          "match_mapping_type": "long",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}```