当我使用 JsonNetSerializer

问题描述

我在映射方面遇到了一些问题。我使用具有以下属性的 JsonNetSerializer 而不是认值:

var connectionSettings =
                new ConnectionSettings(pool,sourceSerializer: (builtin,settings) => new JsonNetSerializer(
                    builtin,settings,() => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,ReferenceLoopHandling = ReferenceLoopHandling.Ignore},resolver => resolver.NamingStrategy = new CamelCaseNamingStrategy()
                ))
                .BasicAuthentication(userName,password);

            client = new Elasticclient(connectionSettings);

我这样映射讲师:

private static CreateIndexDescriptor GetLecturerMap(string indexName)
        {
            CreateIndexDescriptor map = new CreateIndexDescriptor(indexName);
            map.Mappings(M => M
                .Map<Lecturer>(m => m
                    .Properties(prop => prop
                        .Text(s => s
                            .Name(n => n.FullName)
                        )
                        .Boolean(o => o
                            .Name(s => s.IsActive)
                         )
                        .Number(s => s
                            .Name(n => n.Id)
                            .Type(NumberType.Integer)
                        )

                        .Date(d => d
                            .Name(n => n.User.LastLogin)
                        )
                        .Object<User>(u=>u
                            .Name(n=>n.User)
                            .Properties(pr => pr
                                .Text(t=>t
                                    .Name(n=>n.SkypeContact)
                                    )
                                )
                            )
                    )
                )
             )
           ;
            return map;
        }

然后这样称呼它:

public int InitializeLecturers()
    {
        string lecturersIndexName = LECUTRERS_INDEX_NAME;
        client.Indices.Create(GetLecturerMap(lecturersIndexName));
        List<Lecturer> lecturers = GetLecturers();

        client.IndexMany(lecturers,lecturersIndexName);
        return lecturers.Count;
    }

当我使用以下方法数据库获取讲师时:

private List<Lecturer> GetLecturers() {
            using (Context context = new Context(connectionString))
            {
                return context.Lecturers
                    .ToList<Lecturer>();
            }
        }

程序创建以下映射:

{
  "lecturers" : {
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text","fields" : {
            "keyword" : {
              "type" : "keyword","ignore_above" : 256
            }
          }
        },"fullName" : {
          "type" : "text"
        },"id" : {
          "type" : "integer"
        },"isActive" : {
          "type" : "boolean"
        },"isLecturerHasGraduateStudents" : {
          "type" : "boolean"
        },"isNew" : {
          "type" : "boolean"
        },"isSecretary" : {
          "type" : "boolean"
        },"lastLogin" : {
          "type" : "date"
        },"lastName" : {
          "type" : "text","middleName" : {
          "type" : "text","skill" : {
          "type" : "text","user" : {
          "properties" : {
            "skypeContact" : {
              "type" : "text"
            }
          }
        }
      }
    }
  }
}

所以我不明白,为什么它会忽略我的映射并添加所有字段而不是一个 我选择?请告诉我如何修复它。可能我必须以不同的方式创建映射?

解决方法

可能发生的事情是

  1. 应用您在创建索引时定义的显式映射
  2. Elasticsearch 为它在 JSON 文档中看到的没有映射的属性添加新的字段映射,并为它们推断字段映射类型。

第 2 点是 Elasticsearch 的默认行为,但可以通过更改 dynamic property when creating the index and mapping.

根据问题的内容,您似乎使用的是 Elasticsearch 6.x,即

var client = new ElasticClient(settings);

client.CreateIndex("index_name",c => c
    .Mappings(m => m
        .Map<Lecturer>(m => m
            .Dynamic(false)
            .Properties(prop => prop
                .Text(s => s
                    .Name(n => n.FullName)
                )
                .Boolean(o => o
                    .Name(s => s.IsActive)
                 )
                .Number(s => s
                    .Name(n => n.Id)
                    .Type(NumberType.Integer)
                )

                .Date(d => d
                    .Name(n => n.User.LastLogin)
                )
                .Object<User>(u => u
                    .Name(n => n.User)
                    .Properties(pr => pr
                        .Text(t => t
                            .Name(n => n.SkypeContact)
                            )
                        )
                    )
            )
        )
    )
);

根据文档链接,dynamicfalse 值将忽略 字段并且不会创建新的字段映射或索引字段,但这些字段仍将在_source 文件。您可能还想在 [JsonIgnore] 的属性上设置 Lecturer 属性,这些属性应该被忽略,这样它们就不会被序列化并发送到 Elasticsearch。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...