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

时间:2014-03-11 12:26:05

标签: elasticsearch javascript

我尝试使用官方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);
});

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

undefined { ok: true, acknowledged: true } 200

我做错了什么?

5 个答案:

答案 0 :(得分:23)

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

使用elastsearch on 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});

答案 1 :(得分:12)

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

foreach ($row as $columns => $rows) {
                if($rows instanceof \DateTime) {
                    echo "<td>" , $rows->format('Y-m-d H:i:s') , "</td>";
                }
                else
                {
                    echo "<td>$rows</td>";
                }           
        }

输出:

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

enter image description here

答案 2 :(得分: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);
});

答案 3 :(得分: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
            }
        }
    }
})

请注意,该类型已从身体中拉出,只有该类型下的子参数现在在体内。

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

答案 4 :(得分:0)

注意:这使用的是 client.indices.create() 而不是client.indices.putMapping()

我最近成功地使用如下自定义映射创建了索引:

client.indices.create({
  index: 'yourIndex',
  body: {
    yourIndex: {
      mappings: {
        yourType: {
          properties: {
            yourText: {
              type: 'string',
            }
          }
        }
      }
    }
  }
});

似乎您必须开始使用索引定义主体,然后是mappings关键字,然后是类型,依此类推。我使用了Elasticsearch软件包版本15.4.1和弹性版本6.5.4