0

I am trying to use elasticsearch's official client in nodejs to create indices for chouchDB. Will really appreciate any help.

This is how I am creating an index:

esClient.indices.create({index: "users_index",
            body: {
                "type" : "couchdb",
                "couchdb" : {
                    "host" : "localhost",
                    "port" :"5984",
                    "db" :"users"
                },
                "index" : {
                    "index" : "users_index",
                    "type" : "users",
                    "bulk_size" : "100",
                    "bulk_timeout" : "10ms"
                }
            }}).then(function(x){
                console.log(x);
                callback(null);
            },
            function(err){
                console.log(err);
                callback(null);
            });

When I search for data like this in sense (GET users_index/users_index/_search), I get this without any data: { "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 0, "max_score": null, "hits": [] } }

I'll really appreciate any help.

Before creating index, I am also trying to create an index template so that all my indices get the same mapping. I am doing it like following. I have not gone that far to verify if its correct. Please let me know if there are any errors in this.

esClient.indices.putTemplate({
            name: "vw_index_template",
            body:{
                "template" : "*_index",
                "settings" : {
                    "number_of_shards" : 1
                },
                "mappings" : {
                    "id_prkey_template" : {
                        "properties" : {
                            "_id" : {"type" : "string", "index": "not_analyzed"},
                            "prkey" : {"type" : "string", "index": "analyzed"}
                        }
                    }
                }
            }
        }).then(function(res){
            console.log(res);
            callback(null);

        }, function(error){
            console.log(error);
            callback(null);
        });

Will really appreciate any help.

Many thanks.

Imad
  • 179
  • 3
  • 12

1 Answers1

0

You can't attach a document (body) to indices.create (see here). If you want to simply index new documents into a either existing or non-existing index you should use index, for example:

esClient.index({
  index: "users_index",
  type: "couchdb",
  body: {
    field1: 'test',
    field2: 123
  }
}).then(function (x) {
    console.log(x);
    callback(null);
  },
  function (err) {
    console.log(err);
    callback(null);
  });

Also, you shouldn't specify any mapping for the _id field since it's an internal ES field.

Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
  • I tried your suggestion but seems like it not working. What I want to do is to create an index that imports data from a couchdb (I am using coucdb river plugin for elasticsearch). – Imad Aug 18 '15 at 14:22
  • I tried the following code to do what I said in comment above. `curl -XPUT "http://localhost:9200/_river/user_idx/_meta" -d "{ "type" : "couchdb", "couchdb" : { "host" : "localhost", "port" : 5984, "db" : "users", "filter" : null }, "index" : { "index" : "user_idx", "type" : "users", "bulk_size" : "100", "bulk_timeout" : "10ms" }}" ' and it works fine. Now was trying to do the same in nodejs with elasticsearchclient. I tried your suggestion but that doesn't seem to do what I am looking for. – Imad Aug 18 '15 at 14:25
  • Regarding your suggestion for not using _id for mapping, can you please let me know how can I make _id of my couchdb database to be searchable ie., how can I then add that in index? thx. – Imad Aug 18 '15 at 14:30