使用elasticssearch客户端创建索引

时间:2015-08-17 20:38:36

标签: node.js templates indexing elasticsearch couchdb

我试图在nodejs中使用elasticsearch的官方客户端为chouchDB创建索引。真的很感激任何帮助。

这就是我创建索引的方式:

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

当我在意义上搜索这样的数据时(GET users_index / users_index / _search),我得到的没有任何数据:      {      “拿”:1,      “timed_out”:false,      “_shards”:{       “总数”:1,       “成功”:1,       “失败”:0      },      “命中”:{       “总数”:0,       “max_score”:null,       “点击”:[]      }     }

我真的很感激任何帮助。

在创建索引之前,我还尝试创建索引模板,以便所有索引都获得相同的映射。我这样做就像以下一样。我没有走那么远,以验证它是否正确。如果有任何错误,请告诉我。

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

非常感谢任何帮助。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您无法将文档(正文)附加到indices.createsee here)。如果您只想将新文档编入现有或不存在的索引,则应使用index,例如:

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

此外,您不应为_id字段指定任何映射,因为它是内部ES字段。

相关问题