2016-02-11 69 views
1

菜鳥在ElasticSearch和在這裏築巢。不完全知道我在做什麼錯在這裏,但是這個代碼拋出了
巢(Elasticsearch客戶端C#)散貨指數

「格式不正確動作/元數據線[1],預計START_OBJECT或END_OBJECT卻發現[VALUE_NUMBER]」。

我知道ES正在拋出此錯誤,因爲JSON is malformed。我不知道爲什麼Nest不生成正確的JSON?

注:我希望能夠做批量指數操作,同時告訴它哪個指數,並輸入該有效載荷應該去。

public class Test 
{ 
    private static Uri _node; 
    private ElasticsearchClient _client; 

    static Test() 
    { 
     _node = new Uri("http://localhost:9200"); 
    } 

    public Test() 
    { 
     _client = new ElasticsearchClient(new ConnectionSettings(_node)); 
    } 

    public void Bulk<T>(List<T> data, string index, string type) where T : class 
    { 
     _client.Bulk(index, type, data); 
    } 
} 
+1

你用什麼版本NEST和ES的下面?你可以分享你想要索引的類型嗎? – Rob

回答

0

您使用的是低級別ElasticsearchClient當我想你的意思是使用高級別ElasticClient。根據低級客戶端的名稱,我假設您使用的是NEST 1.x,可能是最新版本1.7.1。請注意,NEST 1.x僅與Elasticsearch 1.x和NEST 2.x兼容,僅與Elasticsearch 2.x兼容。

要使用NEST 1.x中指定索引名和類型名散裝指數將與流暢的API

void Main() 
{ 
    var settings = new ConnectionSettings(new Uri("http://localhost:9200")); 

    // use NEST *ElasticClient* 
    var client = new ElasticClient(settings, connection: new InMemoryConnection()); 

    var docs = new List<Doc> 
    { 
     new Doc(), 
     new Doc(), 
     new Doc(), 
     new Doc(), 
     new Doc() 
    }; 

    var indexResponse = client.CreateIndex("docs", c => c 
     .AddMapping<Doc>(m => m.MapFromAttributes()) 
    ); 

    var bulkResponse = client.Bulk(b => b 
     .IndexMany(docs, (d, doc) => d.Document(doc).Index("index-name").Type("type-name")) 
    ); 
} 

public class Doc 
{ 
    public Doc() 
    { 
     Id = Guid.NewGuid(); 
    } 

    public Guid Id { get; set; } 
} 
+0

是的,我在兩天前使用最新的NuGet。看這個版本,我看到Nest是1.0,Elasticsearch.Net也是。我試過了你的代碼示例,我沒有看到Fiddler會在瀏覽器中運行http:// localhost:9200/_cat/indices?v時看到創建的「index-name」索引。 – codelove

+0

除非您在Uri中將'localhost'更改爲'ipv4.fiddler',否則您不會看到它超過提琴手。你能否更新你的問題以顯示你正在使用的代碼?我上面的例子使用'InMemoryConnection'在內存中執行,所以實際上並沒有發送到ES。從構造函數中刪除'InMemoryConnection',它將針對本地主機執行。 –