2016-02-11 64 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',它将针对本地主机执行。 –