elasticsearch自定义mapping 步骤

2020-05-14  本文已影响0人  觉释

自定义mapping 的步骤:
1、写一条文档到es的临时索引中,获取es自动生成的mapping
2、修改第一步得到的mapping,自定义相关配置
3、使用第2步的mapping创建实际的索引

举例

一、假设我得到了需要存入es的文档,首先将文档写入临时的index中:

PUT test_index/doc/1
{
  "referre": "-",
  "response_code": "200",
  "remote_ip": "172.0.0.1",
  "method": "POST",
  "username": "-",
  "http_version": "1.0",
  "body_sent": {
    "bytes": "0"
  },
  "url": "/helloworld"
}

二、然后查看es自动生成的mapping:

GET test_index/_mapping

三、现在希望将bytes设置为整型,url设置为text类型,其他都使用keyword(将上一步的输出复制过来就好):

PUT product_index
{
    "mappings": {
      "doc": {
        "properties": {
          "body_sent": {
            "properties": {
              "bytes": {
                "type": "long"
              }
            }
          },
          "http_version": {
            "type": "keyword"
          },
          "method": {
            "type": "keyword"
          },
          "referre": {
            "type": "keyword"
          },
          "remote_ip": {
            "type": "keyword"
          },
          "response_code": {
            "type": "keyword"
          },
          "url": {
            "type": "text"
          },
          "username": {
            "type": "keyword"
          }
        }
      }
    }
  }

四、这样直接将测试index的mapping复制过来进行修改,不会遗漏字段,修改完成设置一个index的名称就行了。

然后就可以向实际的索引中写入文档了:

PUT product_index/doc/1
{
  "referre": "-",
  "response_code": "200",
  "remote_ip": "172.0.0.1",
  "method": "POST",
  "username": "-",
  "http_version": "1.1",
  "body_sent": {
    "bytes": "0"
  },
  "url": "/helloworld"
}

五、查看一下实际索引mapping:

GET product_index/_mapping
上一篇下一篇

猜你喜欢

热点阅读