• Elasticsearch索引mapping的写入、查看与修改(转)


    mapping的写入与查看

    首先创建一个索引:

    curl -XPOST "http://127.0.0.1:9200/productindex"
    {"acknowledged":true}  
    

      

    现在只创建了一个索引,并没有设置mapping,查看一下索引mapping的内容:

    curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty" 
    {
      "productindex" : {
        "mappings" : { }
      }
    }
    

      

    可以看到mapping为空,我们只创建了一个索引,并没有进行mapping配置,mapping自然为空。 
    下面给productindex这个索引加一个type,type name为product,并设置mapping:

    curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d ' 
    {
        "product": {
                "properties": {
                    "title": {
                        "type": "string",
                        "store": "yes"
                    },
                    "description": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "price": {
                        "type": "double"
                    },
                    "onSale": {
                        "type": "boolean"
                    },
                    "type": {
                        "type": "integer"
                    },
                    "createDate": {
                        "type": "date"
                    }
                }
            }
      }
    '
     
    {
      "acknowledged" : true
    }
    

      

    上面的操作中,我们给productindex加了一个type,并写入了product的mapping信息,再次查看:

    curl -XGET "http://127.0.0.1:9200/productindex/_mapping?pretty"
    {
      "productindex" : {
        "mappings" : {
          "product" : {
            "properties" : {
              "createDate" : {
                "type" : "date",
                "format" : "strict_date_optional_time||epoch_millis"
              },
              "description" : {
                "type" : "string",
                "index" : "not_analyzed"
              },
              "onSale" : {
                "type" : "boolean"
              },
              "price" : {
                "type" : "double"
              },
              "title" : {
                "type" : "string",
                "store" : true
              },
              "type" : {
                "type" : "integer"
              }
            }
          }
        }
      }
    }
    

      

    修改mapping

    如果想给product新增一个字段,那么需要修改mapping,尝试一下:

    curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
         "product": {
                    "properties": {
                         "amount":{
                            "type":"integer"
                       }
                    }
                }
        }'
    {
      "acknowledged" : true
    }
    

      

    新增成功。 
    如果要修改一个字段的类型呢,比如onSale字段的类型为boolean,现在想要修改为string类型,尝试一下:

     curl -XPOST "http://127.0.0.1:9200/productindex/product/_mapping?pretty" -d '{
         "product": {
                    "properties": {
                     "onSale":{
                        "type":"string" 
                   }
                }
            }
    }'
    

      

    返回错误:

    {
      "error" : {
        "root_cause" : [ {
          "type" : "illegal_argument_exception",
          "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
        } ],
        "type" : "illegal_argument_exception",
        "reason" : "mapper [onSale] of different type, current_type [boolean], merged_type [string]"
      },
      "status" : 400
    }
    

      

    为什么不能修改一个字段的type?原因是一个字段的类型修改以后,那么该字段的所有数据都需要重新索引。Elasticsearch底层使用的是lucene库,字段类型修改以后索引和搜索要涉及分词方式等操作,不允许修改类型在我看来是符合lucene机制的。 
    这里有一篇关于修改mapping字段的博客,叙述的比较清楚:Elasticsearch 的坑爹事——记录一次mapping field修改过程,可以参考.

  • 相关阅读:
    【linux]】lighttpd的日志格式
    【vi】awk为指定行的指定字段添加一个单词
    【Android】命令行操作-启动应用程序
    CCS设置第一个li的元素与其他li样式不同
    nginx+tomcat 下POST响应参数过大无法显示完整及文件下载服务遇到过大文件无法下载解决办法
    有重复行,查询时只保留最新一行的sql
    Android定时执行和停止某任务
    MySQL每天自动增加分区
    <html:option获取文本值
    easyui datagrid 增删改查示例
  • 原文地址:https://www.cnblogs.com/sandea/p/10557125.html
Copyright © 2020-2023  润新知