0

Is there a way to use NEST's AutoMap functionality with the Object Initializer Syntax?

According to the tutorial here, the Object Initializer Syntax is "fully supported throughout the client." So, using an example from the tutorial, how would I perform this mapping using the Object Initializer Syntax?

.Nested<PackageDependency>(nn => nn
    .Name(pv => pv.Dependencies.First())
    .AutoMap()
)

I've tried it like this:

new NestedProperty
{
    Name = new PropertyName(nameof(PackageVersion.Dependencies))
}

But I can't find a property on the NestedProperty object that would corespond to AutoMap. The same goes for TextProperty, etc.

If it's not possible, I'll just use the Fluent API Syntax - no problem. But I want to make sure I'm not missing something obvious.

1 Answer 1

1

You've managed to find a method, AutoMap(), that has no directly corresponding counterpart in the Object Initializer syntax :) Because there is no generic type parameter to represent the CLR type available, it can't be modelled the same way.

You can however achieve the same effect with using the components that AutoMap uses

var nestedProperty = new NestedProperty
{
    Name = Nest.Infer.Property<PackageVersion>(pv => pv.Dependencies.First()),
    Properties = new Properties<PackageDependency>(
        new PropertyWalker(typeof(PackageDependency), null).GetProperties())
};

which will yield the following JSON assigned the field name resolved from Nest.Infer.Property<PackageVersion>(pv => pv.Dependencies.First()) (by default will be dependencies)

{
  "type": "nested",
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "version": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "framework": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

If you need to override any properties that will be automapped this way, you can either do so on the IProperties returned from .GetProperties() method call or using the indexer on Properties<T>, which is Properties<PackageDependency> in this example.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah... perfect. Thanks so much - that's exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.