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

Mapping attribute in class DynamicTemplate does not work as expected #7234

Closed
matthewyang0807 opened this issue Feb 15, 2023 · 4 comments · Fixed by #7244 or #7337
Closed

Mapping attribute in class DynamicTemplate does not work as expected #7234

matthewyang0807 opened this issue Feb 15, 2023 · 4 comments · Fixed by #7244 or #7337
Labels
8.x Relates to 8.x client version
Milestone

Comments

@matthewyang0807
Copy link

Elastic.Clients.Elasticsearch version: 8.0.5

Elasticsearch version: 8.0

.NET runtime version: 6.0

Operating system version: Win11

Description of the problem including expected versus actual behavior:
A clear and concise description of what the bug is.

Steps to reproduce:

  1.  public static Union<IDictionary<string, DynamicTemplate>?, ICollection<IDictionary<string, DynamicTemplate>>?> 
      GetDynamicTemplates()
    {
        IDictionary<string, DynamicTemplate> templatesDic1 = new Dictionary<string, DynamicTemplate>();
        ICollection<IDictionary<string, DynamicTemplate>> templatesDicList = new List<IDictionary<string, DynamicTemplate>>();
    
        var template1 = new DynamicTemplate
        {
            PathMatch = "testPathMatch",
            Mapping = new Properties
            {
                **//The problem is here, I have tried many different types of property to add here, but will always lead the dynamic 
               //template creation failed**
                { "testType", new KeywordProperty() }
            }
        };
    
        templatesDic1.Add("testTemplateName", template1);
        templatesDicList.Add(templatesDic1);
        return new Union<IDictionary<string, DynamicTemplate>?, ICollection<IDictionary<string, DynamicTemplate>>?> 
         (templatesDicList);
    }
    
  2.   CreateIndexRequest cr = new CreateIndexRequest("matthew-es8-typeahead1");
       cr.Mappings.DynamicTemplates = GetDynamicTemplates();
       var indexCreateResponse = es8client.Indices.CreateAsync(cr);
       Console.WriteLine(indexCreateResponse.Result.Acknowledged);
       Console.WriteLine(indexCreateResponse.Result.ApiCallDetails.HttpStatusCode);
       Console.WriteLine(indexCreateResponse.Result.ApiCallDetails.OriginalException);
    
  3.  It will report the error 
    

image

I tried many times there is no property can be put here to make dynamic template created succsesfully.
I notice that old version in NEST6 the Mapping property type is IProperty not Properties, so can you provide me a correct way to generate the DynamicTemple?

Expected behavior
A clear and concise description of what you expected to happen.

I want to see my index "matthew-es8-typeahead1" with correct dynamic template in mapping part
"dynamic_templates": [
"testTemplateName": {
"path_match": "testPathMatch",
"mapping": {
"type": "keyword"}
}]

Provide DebugInformation (if relevant):
Elastic.Transport.TransportException: Request failed to execute. Call: Status code 400 from: PUT /matthew-es8-typeahead1. ServerError: Type: mapper_parsing_exception Reason: "Failed to parse mapping: dynamic template [testTemplateName] has invalid content [{"path_match":"testPathMatch","mapping":{"testType":{"type":"keyword"}}}], attempted to validate it with the following match_mapping_type: [object, string, long, double, boolean, date, binary]" CausedBy: "Type: illegal_argument_exception Reason: "dynamic template [testTemplateName] has invalid content [{"path_match":"testPathMatch","mapping":{"testType":{"type":"keyword"}}}], attempted to validate it with the following match_mapping_type: [object, string, long, double, boolean, date, binary]" CausedBy: "Type: mapper_parsing_exception Reason: "unknown parameter [testType] on mapper [__dynamic__testTemplateName] of type [null]"""
400

@matthewyang0807 matthewyang0807 added the 8.x Relates to 8.x client version label Feb 15, 2023
@stevejgordon
Copy link
Contributor

stevejgordon commented Feb 15, 2023

Thanks for raising this, @matthewyang0807. This is related to #7221. When we fix the generated code, I expect some changes to the types, which may resolve this as a by-product. If not, we can investigate this after a PR is merged, fixing the first issue.

@stevejgordon
Copy link
Contributor

@matthewyang0807 A fixed has been merged which will ship in the next patch release. Once that's available you can achieve your objective with the code along the lines of:

var myTemplate = new DynamicTemplate
{
   PathMatch = "testPathMatch",
   Mapping = new KeywordProperty()
};

var createIndexRequest = new CreateIndexRequest("testing")
{
   Mappings = new TypeMapping
   {
      DynamicTemplates = new[]
      {
         new Dictionary<string, DynamicTemplate>
         {
            { "testTemplateName", myTemplate }
         }
      }
   }
};

var indexCreateResponse = await client.Indices.CreateAsync(createIndexRequest);

@matthewyang0807
Copy link
Author

@stevejgordon
Is this fix included in version 8.0.6? I run this sample code in v8.0.6 will still get an dynamic template parsing error...or do I miss something?

Error Below:

Elastic.Transport.TransportException: Request failed to execute. Call: Status code 400 from: PUT /matthew-fix-dynamictemplate-test. ServerError: Type: mapper_parsing_exception Reason: "Failed to parse mapping: Dynamic template syntax error. An array of named objects is expected." CausedBy: "Type: mapper_parsing_exception Reason: "Dynamic template syntax error. An array of named objects is expected.""

@stevejgordon
Copy link
Contributor

@matthewyang0807 The original exception was caused by a malformed request. This new error is subtly different but also due to the request format not being excepted by the server. In this case, it appears to be an issue in the ES specification, which I'll review and fix before we code-gen this request again. I'll reopen to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.x Relates to 8.x client version
Projects
None yet
2 participants