【问题标题】:How to Map dynamically the properties of a JObject in C# using Elasticsearch Nest如何使用 Elasticsearch Nest 在 C# 中动态映射 JObject 的属性
【发布时间】:2020-03-30 08:35:12
【问题描述】:

我想使用 Nest 在 C# 中动态映射 JObject 中的属性。 目标是将对象的每个字符串字段映射为 SearchAsYouType。 我想了 3 种方法,但没有奏效:

  1. 使用AutoMap,直接在C#类中声明属性
public class Forfait
    {
        public long Id { get; set; }
        [SearchAsYouType()] 
        public string Data { get; set; }
    }
public class  Act
    {
        public JObject Entity;
    }
Forfait forfait = new Forfait()
            {
                Data = "data",
                Id = 99
            };
            Act act = new Act()
            {
                Entity = JObject.FromObject(forfait)
            };

            await client.Indices.CreateAsync("index", o => o
                .Map<Act>(m => m
                .AutoMap()

2.使用DynamicTemplates,但在Mapping中找不到SearchAsYouType,好像在Nest 7.4.1中还不存在

await client.Indices.CreateAsync("index", o => o
                .Map<Act>(m => m
                .DynamicTemplates(d =>d
                    .DynamicTemplate("stringassearch",dt => dt
                        .Match("entity.*")
                        .MatchMappingType("string")
                        .Mapping(ma =>ma
                            .)))));

3.使用Visitor强制每个String都是SearchAsYouType

public class EveryStringIsASearchAsYouTypePropertyVisitor : NoopPropertyVisitor
    {
        public override IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
        {
            if (propertyInfo.PropertyType == typeof(String))
                return new SearchAsYouTypeProperty();
            return null;
        }
    }
await client.Indices.CreateAsync("index", o => o
                .Map<Act>(m => m
                .AutoMap(new EveryStringIsASearchAsYouTypePropertyVisitor(),2)

一切都失败了

我觉得解决方案是在 NEST.JsonNetSerializer 中以某种方式使映射中使用的设置在 JObject 中应用,但我找不到任何有用的东西

【问题讨论】:

    标签: c# elasticsearch nest elasticsearch-net elasticsearch.net


    【解决方案1】:

    2.使用DynamicTemplates但在Mapping中找不到SearchAsYouType,好像Nest 7.4.1中还没有

    你说得对,看起来 SingleMappingSelector 中缺少它,但你可以使用这个扩展类轻松解决它,它将添加对 search_as_you_type 类型的支持。

    static class MappingExtension
    {
        public static IProperty SearchAsYouType<T>(this SingleMappingSelector<T> mappingSelector, 
            Func<SearchAsYouTypePropertyDescriptor<T>, ISearchAsYouTypeProperty> selector) where T : class
        => selector?.Invoke(new SearchAsYouTypePropertyDescriptor<T>());
    } 
    

    然后您可以创建动态模板,如关注

    var createIndexResponse = await client.Indices.CreateAsync("index", o => o
        .Map<Act>(m => m
            .AutoMap<Act>()
            .DynamicTemplates(d => d
                .DynamicTemplate("stringassearch", dt => dt
                    .PathMatch("entity.*")
                    .MatchMappingType("string")
                    .Mapping(ma => ma.SearchAsYouType(s => s))))));
    

    注意,我已将 Match(..) 更改为 PathMatch(..) - 我认为这是您需要的。另外,我必须将Act 定义更改为

    public class  Act
    {
        public object Entity;
    }
    

    索引示例文档后,此索引映射已创建

    {
      "index": {
        "mappings": {
          "dynamic_templates": [
            {
              "stringassearch": {
                "path_match": "entity.*",
                "match_mapping_type": "string",
                "mapping": {
                  "type": "search_as_you_type"
                }
              }
            }
          ],
          "properties": {
            "entity": {
              "properties": {
                "data": {
                  "type": "search_as_you_type",
                  "max_shingle_size": 3
                },
                "id": {
                  "type": "long"
                }
              }
            }
          }
        }
      }
    }
    

    Here 是 GH 问题。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多