2017-06-06 92 views
0

我陷入困境與巢。我在Elasticsearch 5.1.1上有一個全新的索引,我試圖用dotnet core定義一個類型映射。巢映射兩次相同的類型

我的類看起來像:

public class Review 
{ 
    public Guid Id { get; set; } 
    public User User { get; set; } 
    public Movie Movie { get; set; } 
    public int Grade { get; set; } 
    public string Title { get; set; } 
    public string Comment { get; set; } 
    public bool HasSpoiler { get; set; } 
    public bool BoughtInIngresso { get; set; } 
    public ReviewStatus Status { get; set; } 
    public DateTime Date { get; set; } 
} 

public class User 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
    } 


public class Movie 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
    } 

在我的申請,我試圖定義類型映射(只是爲了測試)的縮寫形式類似:

VAR池=新StaticConnectionPool(節點);

var settings = new ConnectionSettings(pool); 
    settings.DefaultIndex(elasticSettings.IndexName); 

    var client = new ElasticClient(settings); 

    client.Map<Review>(m => 
     m.Index("my_index") 
     .Type("reviews") 
     .Properties(ps=> 
      ps.Keyword(k=> 
        k.Name("title")) 
      .Text(t=> 
        t.Name("comment")) 
     ) 
    ); 

而最終的結果是這樣的。觀察正在創建的評論和評論映射。我只想要「評論」,而不是「評論」。

{ 
    "my_index": { 
    "mappings": { 
     "reviews": { 
     "properties": { 
      "comment": { 
      "type": "text" 
      }, 
      "title": { 
      "type": "keyword" 
      } 
     } 
     }, 
     "review": { 
     "properties": { 
      "boughtInSite": { 
      "type": "boolean" 
      }, 
      "comment": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "date": { 
      "type": "date" 
      }, 
      "grade": { 
      "type": "long" 
      }, 
      "hasSpoiler": { 
      "type": "boolean" 
      }, 
      "id": { 
      "type": "text", 
      "fields": { 
       "keyword": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "movie": { 
      "properties": { 
       "id": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       }, 
       "name": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       } 
      } 
      }, 
      "status": { 
      "type": "long" 
      }, 
      "title": { 
      "type": "keyword" 
      }, 
      "user": { 
      "properties": { 
       "id": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       }, 
       "name": { 
       "type": "text", 
       "fields": { 
        "keyword": { 
        "type": "keyword", 
        "ignore_above": 256 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我在做什麼錯了?如果我使用AutoMap(),也會發生這種情況。我不想映射屬性,因爲我想保留我的POCO類,但如果是唯一的方法,我可以做。

有幫助嗎?

回答

0

您尚未顯示如何爲Review類型建立索引,但我懷疑在索引時沒有將類型指定爲"reviews";在這種情況下,NEST將從C#POCO類型名稱中推斷出該類型。

NEST提供要使用InferMappingFor<T>() C#的POCO類型ReviewConnectionSettings

public class Review 
{ 
    public Guid Id { get; set; } 
    public User User { get; set; } 
    public Movie Movie { get; set; } 
    public int Grade { get; set; } 
    public string Title { get; set; } 
    public string Comment { get; set; } 
    public bool HasSpoiler { get; set; } 
    public bool BoughtInIngresso { get; set; } 
    public ReviewStatus Status { get; set; } 
    public DateTime Date { get; set; } 
} 

public enum ReviewStatus 
{ 
    Good, 
    Bad, 
    NotTooShabby 
} 

public class User 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
} 


public class Movie 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
} 

void Main() 
{ 
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); 
    var defaultIndex = "default-index"; 
    var connectionSettings = new ConnectionSettings(pool) 
     .InferMappingFor<Review>(m => m 
      .TypeName("reviews") 
     ) 
     .DefaultIndex(defaultIndex); 

    var client = new ElasticClient(connectionSettings); 

    client.CreateIndex("my_index", c => c 
     .Mappings(m => m 
      .Map<Review>(mm => mm 
       .Properties(ps => ps 
        .Keyword(k => k 
         .Name(n => n.Title) 
        ) 
        .Text(t => t 
         .Name(n => n.Comment) 
        ) 
       ) 
      ) 
     ) 
    ); 
} 

與此相關的方法爲Elasticsearch型"reviews"的方式,你現在沒有必要指定每個請求的類型,但如果需要,您可以根據請求覆蓋它。

With AutoMap(), you can let NEST infer the Elasticsearch field types from the C# POCO property typesthen use .Properties() to override any mappings you'd like.

相關問題