elasticsearch:使用javascript创建带映射的索引

San*_*man 30 javascript elasticsearch

我正在尝试使用官方javascript客户端创建一个带有映射的elasticsearch索引.

我的代码如下:

client.indices.create({
    index: "aName",
    "mappings": {
        "aType": {
            "properties": {
                "aProp1": { "type": "string", "index": "not_analyzed" },
                "aProp2": { "type": "string", "index": "not_analyzed" },
                "aProp3": { "type": "string", "index": "not_analyzed" },
                "aProp4": { "type": "string", "index": "not_analyzed" }
            }
        }
    }
}, function(err,resp,respcode){
    console.log(err,resp,respcode);
});
Run Code Online (Sandbox Code Playgroud)

但是...索引已创建但没有映射....输出为:

undefined { ok: true, acknowledged: true } 200

我究竟做错了什么?

Tim*_*Tim 25

完善Sander Spilleman上述评论中的答案."映射"属性需要位于"body"属性中.我也在使用Javascript客户端1.3.0,文档仍然没有更新示例.

使用elastsearch在NPM 1.3.0上提供的javascript API添加一个对我有用的示例

var body = {
    tweet:{
        properties:{
            tag         : {"type" : "string", "index" : "not_analyzed"},
            type        : {"type" : "string", "index" : "not_analyzed"},
            namespace   : {"type" : "string", "index" : "not_analyzed"},
            tid         : {"type" : "string", "index" : "not_analyzed"}
        }
    }
}

client.indices.putMapping({index:"tweets", type:"tweet", body:body});
Run Code Online (Sandbox Code Playgroud)

  • 您是否介意提交拉取请求或填写您认为可以从文档中受益的方法的问题?我很乐意添加它们 (4认同)
  • @tim我认为在放映之前我们必须调用client.indices.create({index:"aName",type:"tweet",body:body,},function(err,resp,respcode){console.log(err, resp,respcode);}); 否则他们会得到错误indexnotexistexception (2认同)

uza*_*y95 13

我尝试了相同但从索引名称得到错误.aName无效,错误是关于使用小写索引名称.然后用映射创建.

it.only('putMapping', function (done) {
    client.indices.create({
        index: "aname",
        body: {
            "mappings": {
                "aType": {
                    "properties": {
                        "aProp1": {"type": "string", "index": "not_analyzed"},
                        "aProp2": {"type": "string", "index": "not_analyzed"},
                        "aProp3": {"type": "string", "index": "not_analyzed"},
                        "aProp4": {"type": "string", "index": "not_analyzed"}
                    }
                }
            }
        }
    }, function (err, resp, respcode) {
        console.log(err, resp, respcode);
    });
})
Run Code Online (Sandbox Code Playgroud)

输出:

Elasticsearch DEBUG: 2015-08-08T15:23:09Z
  starting request { method: 'POST',
    path: '/aname',
    body: { mappings: { aType: [Object] } },
    query: {} }


Elasticsearch TRACE: 2015-08-08T15:23:10Z
  -> POST http://localhost:9200/aname
  {
    "mappings": {
      "aType": {
        "properties": {
          "aProp1": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp2": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp3": {
            "type": "string",
            "index": "not_analyzed"
          },
          "aProp4": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
  <- 200
  {
    "acknowledged": true
  }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 10

只需在映射周围添加body:

client.indices.create({
    index: "aName",
    body: {
        "mappings": {
            "aType": {
                "properties": {
                    "aProp1": { "type": "string", "index": "not_analyzed" },
                    "aProp2": { "type": "string", "index": "not_analyzed" },
                    "aProp3": { "type": "string", "index": "not_analyzed" },
                    "aProp4": { "type": "string", "index": "not_analyzed" }
                }
            }
        }
    }
}, function (err, resp, respcode) {
    console.log(err, resp, respcode);
});
Run Code Online (Sandbox Code Playgroud)


Bra*_*don 7

这些示例都不适用于ElasticSearch 5.3 API.

这是一个适用于5.3的示例.

elasticClient.indices.putMapping({
    index: indexName,
    type: "document",
    body: {
        properties: {
            title: { type: "string" },
            content: { type: "string" },
            suggest: {
                type: "completion",
                analyzer: "simple",
                search_analyzer: "simple",
                payloads: true
            }
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

请注意,该类型已被拉出体外,只有该类型下的子参数现在在体内.

资料来源:https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/

  • 如何创建具有多种类型的索引 (2认同)