如何强制NEST使用属性映射

时间:2016-08-25 22:45:01

标签: c# elasticsearch nest

好的,如果这是一个愚蠢的问题,我很抱歉,但我花了一天浏览文档并尝试了3种不同的NEST版本,最终结果是一样的。

基本上当我使用Elasticsearch的REST api来创建类型的映射时,我可以在映射上使用GET请求,并且我收到了我想要的内容:

"properties": {
   "date": {
      "type": "date",
      "format": "basic_date"
   },
   "id": {
      "type": "long",
      "index": "no"
   },
   "name": {
      "type": "string"
   },
   "slug": {
      "type": "string",
      "index": "no"
   }
}

但是,如果我从头开始,并在c#中使用以下类:

[Number(Index = NonStringIndexOption.No)]
public long Id { get; set; }
[String(Name = "name")]
public string Name { get; set; }
[String(Name = "slug", Index = FieldIndexOption.No)]
public string Slug { get; set; }
[Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
public DateTime Date { get; set; }

并创建并填充索引:

node = new Uri("http://localhost:9200");
settings = new ConnectionSettings(node);
settings.DefaultIndex("searchable-items");
//retrieve stuff from relational db
client.IndexMany(allItemsRetrievedFromRelDb);

我的类型默认为以下(基本上忽略所有属性值,除了Name =)

"date": {
   "type": "date",
   "format": "strict_date_optional_time||epoch_millis"
},
"id": {
   "type": "long"
},
"name": {
   "type": "string"
},
"slug": {
   "type": "string"
}

基本上我希望实现的是:

  1. “basic_date”类型的日期格式
  2. 只应分析“名称”并进行搜索
  3. 最终,“name”
  4. 的自定义分析器

    我的问题是 - 我做错了什么,为什么NEST无视我在属性中放置的内容?当前代码是v2.4.4,虽然我也尝试了5.0.0预发行版(那里的语法略有不同,但结果相同)。

1 个答案:

答案 0 :(得分:3)

为了使属性映射生效,您需要告诉Elasticsearch有关映射的信息,无论是在使用.CreateIndex()创建索引时,还是在创建索引之后以及索引任何文档之前,.Map()

以下是使用NEST 2.4.4进行演示的示例

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "searchable-items";
    var connectionSettings = new ConnectionSettings(pool)
            // specify the default index to use if
            // 1. no index is specified on the request
            // 2. no index can be inferred from the C# POCO type 
            .DefaultIndex(defaultIndex);;

    var client = new ElasticClient(connectionSettings);

    client.CreateIndex(defaultIndex, c => c
        .Mappings(m => m
            .Map<MyDocument>(mm => mm
                // map MyDocument, letting
                // NEST infer the mapping from the property types
                // and attributes applied to them
                .AutoMap()
            )
        )
    );

    var docs = new[] {
        new MyDocument
        {
            Id = 1,
            Name = "name 1",
            Date = new DateTime(2016,08,26),
            Slug = "/slug1"
        },
        new MyDocument
        {
            Id = 2,
            Name = "name 2",
            Date = new DateTime(2016,08,27),
            Slug = "/slug2"
        }
    };

    client.IndexMany(docs);
}

public class MyDocument
{
    [Number(Index = NonStringIndexOption.No)]
    public long Id { get; set; }
    [String(Name = "name")]
    public string Name { get; set; }
    [String(Name = "slug", Index = FieldIndexOption.No)]
    public string Slug { get; set; }
    [Date(Format = "dd-MM-yyyy", Index = NonStringIndexOption.No)]
    public DateTime Date { get; set; }
}

The Automapping documentation详细了解了如何使用the fluent APIthe visitor pattern (for applying conventions).来控制POCO类型的映射