2017-06-06 11 views
0

私はネストに問題があります。私はElasticsearch 5.1.1に全く新しいインデックスを持っており、私はdotnetコアでタイプマッピングを定義しようとしています。同じタイプを2回マッピングするネスト

私のクラスはそのようになります。私のアプリケーションで

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は、このアプローチではConnectionSettings

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) 
        ) 
       ) 
      ) 
     ) 
    ); 
} 

InferMappingFor<T>()を使用してC#POCOタイプReviewに関連付けられる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.

関連する問題