簡體   English   中英

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

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

Noob在ElasticSearch和Nest這里。 不確定我在這里做錯了什么,但是這段代碼拋出了

“動作/元數據行[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);
    }
}

當我認為要使用高級ElasticClient時,您正在使用低級ElasticsearchClient 基於低級客戶端的名稱,我假設您正在使用NEST 1.x,可能是最新版本1.7.1。 請注意,NEST 1.x僅與Elasticsearch 1.x和NEST 2.x兼容,僅與Elasticsearch 2.x兼容。

要使用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