简体   繁体   English

Nest(C#的Elasticsearch客户端)批量索引

[英]Nest (Elasticsearch client for C#) Bulk Index

Noob at ElasticSearch and Nest here. Noob在ElasticSearch和Nest这里。 Not exactly sure what I am doing wrong here, but this code throws up 不确定我在这里做错了什么,但是这段代码抛出了

"Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_NUMBER]". “动作/元数据行[1]格式错误,预期是START_OBJECT或END_OBJECT,但找到了[VALUE_NUMBER]”。

I know ES is throwing this error because JSON is malformed . 我知道ES抛出此错误是因为JSON is malformed错误。 What I don't know is why Nest isn't generating the right JSON? 我不知道为什么Nest无法生成正确的JSON?

Note: I want to be able to do a bulk index operation while telling it which index and type this payload should go to. 注意:我希望能够执行批量索引操作,同时告诉它该有效负载应该进入哪个索引和类型。

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

You're using the low level ElasticsearchClient when I think you mean to use the high level ElasticClient . 当我认为要使用高级ElasticClient时,您正在使用低级ElasticsearchClient Based on the name of the low level client, I'm assuming that you're using NEST 1.x, probably the latest version 1.7.1. 基于低级客户端的名称,我假设您正在使用NEST 1.x,可能是最新版本1.7.1。 Note that NEST 1.x is only compatible with Elasticsearch 1.x and NEST 2.x in only compatible with Elasticsearch 2.x. 请注意,NEST 1.x仅与Elasticsearch 1.x和NEST 2.x兼容,仅与Elasticsearch 2.x兼容。

To bulk index using NEST 1.x specifying the index name and type name would be the following with the fluent API 要使用NEST 1.x批量编制索引,请使用fluent 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM