2

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);
    }
}
1
  • 1
    What version of NEST and ES do you use? May you share type which you are trying to index? Commented Feb 11, 2016 at 8:44

1 Answer 1

2

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I am using the latest NuGet as of two days ago. Looking at the version, I see Nest is 1.0 and so is Elasticsearch.Net. I tried your code example, neither I see it in Fiddler going over or see the "index-name" index created when I run localhost:9200/_cat/indices?v in the browser.
You won't see it going over fiddler unless you change localhost to ipv4.fiddler in the Uri. Can you update your question to show the code you are using? My example above executes in memory using InMemoryConnection so doesn't actually get sent to ES. Remove the InMemoryConnection from the constructor and it'll execute against localhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.