Last active
May 7, 2021 19:48
-
-
Save mjebrahimi/548d84b173293fed3f79a91f4de21b0f to your computer and use it in GitHub Desktop.
An idea to use strongly typed `Suffix` method in elasticsearch NEST library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Field postField = elasticClient.Infer.Field<Blog>(p => p.Post); //Field.Name: "post" | |
Field tagsField = postField.Suffix<Post>(p => p.Tags); //Field.Name: "post.tags" | |
Field tagsIdField = tagsField.Suffix<Tag>(p => p.Id); //Field.Name: "post.tags.id" | |
public class FieldInfer : Field | |
{ | |
public Inferrer Infer { get; set; } | |
public FieldInfer(Inferrer inferrer, string name) : base(name) | |
{ | |
Infer = inferrer; | |
} | |
} | |
public static class FieldExtensions | |
{ | |
public static FieldInfer Field<T>(this Inferrer inferrer, Expression<Func<T, object>> expression) where T : class | |
{ | |
var field = Infer.Field(expression); | |
var name = inferrer.Field(field); | |
return new(inferrer, name); | |
} | |
public static FieldInfer Suffix<T>(this FieldInfer fieldInfer, Expression<Func<T, object>> expression) where T : class | |
{ | |
var field = Infer.Field(expression); | |
var name = fieldInfer.Infer.Field(field); | |
var suffix = fieldInfer.Name + "." + name; | |
return new(fieldInfer.Infer, suffix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment