简体   繁体   中英

Nest (Elasticsearch client for C#) Bulk Index

Noob at ElasticSearch and Nest here. 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]".

I know ES is throwing this error because JSON is malformed . What I don't know is why Nest isn't generating the right 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 . 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. Note that NEST 1.x is only compatible with Elasticsearch 1.x and NEST 2.x in only compatible with 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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