Elasticsearch创建索引设置settings和mappings参数【20210607】

发表于 2021-05-21 18:22:02
阅读 1067

介绍

介绍

Elasticsearch的索引通过settings设置索引的基础参数,包括分词器、分片、慢查询等等。

Elasticsearch的索引通过mappings设置索引的字段参数,可以设置根字段类型、子级字段类型、孙级字段类型,还可以设置字段的格式等等。

今天福哥要教给大家在创建索引的时候设置settings和mappings参数的方法。

settings

全新索引

如果是全新的创建索引的话,没有什么需要注意的地方,直接通过PUT方法将settings参数提交给ES即可。

data.json

{
  "settings" : {
    "index" : {
      "number_of_shards" : "5",
      "number_of_replicas" : "1",
      "version" : { }
    }
  }
}

创建索引

curl -X PUT -H 'Content-Type: application/json' -d '@data.json' 'http://localhost:9200/tfapi_utf8'

导出索引

如果是导出索引的话就有一些门道了,我们先看看如何导出索引。

导出索引的settings参数

curl 'http://localhost:9200/tfapi_utf8/_settings?pretty=true'

home/topic/2021/0607/15/43161bc48c9551b0fc0819f295cfcd7d.png

去除自动创建字段

这里面有一些字段属于索引自动创建的,不能在创建新索引的时候带着,否则会报错。

包括:index.creation_date、index.uuid、index.version.created、index.version.upgraded、index.provided_name几个字段。

{
  "tfapi_utf8" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "version" : { }
      }
    }
  }
}

去除索引名称

最后我们需要把最外面的tfapi_utf8去掉,也就是说把settings放到最外层。

{
  "settings" : {
    "index" : {
      "number_of_shards" : "5",
      "number_of_replicas" : "1",
      "version" : { }
    }
  }
}

创建带settings的索引

创建索引通过PUT方法提交给ES,福哥下面给出了一个例子。

demo.json

{
  "settings" : {
    "index" : {
      "number_of_shards" : "5",
      "number_of_replicas" : "1",
      "version" : { }
    }
  }
}

创建索引

curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'

home/topic/2021/0607/15/0c45c646b5de6ae82618c8d7467cc0e1.png

mapping

全新索引

如果是全新的创建索引的话,没有什么需要注意的地方,直接通过PUT方法将mappings参数提交给ES即可。

data.json

{
  "mappings" : {
    "tfapi_utf8" : {
      "properties" : {
        "authorId" : {
          "type" : "long"
        },
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mCommentC" : {
          "type" : "long"
        },
        "mDatetime" : {
          "type" : "date"
        },
        "mId" : {
          "type" : "long"
        },
        "mLMDatetime" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mReadC" : {
          "type" : "long"
        },
        "mStat" : {
          "type" : "long"
        },
        "mTitle" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tagIds" : {
          "type" : "long"
        }
      }
    }
  }
}

创建索引

curl -X PUT -H 'Content-Type: application/json' -d '@data.json' 'http://localhost:9200/tfapi_utf8'

导出索引

导出索引和全新索引没有什么太大的区别,只需要进行一步操作处理一下即可。

导出索引的mappings参数

curl 'http://localhost:9200/tfapi_utf8/_mappings?pretty=true'

home/topic/2021/0607/15/9b73a39939bdf2949088135aecac88f5.png

去除索引名称

我们只需要把最外面的tfapi_utf8去掉,也就是说把mappings放到最外层。

{
  "mappings" : {
    "tfapi_utf8" : {
      "properties" : {
        "authorId" : {
          "type" : "long"
        },
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mCommentC" : {
          "type" : "long"
        },
        "mDatetime" : {
          "type" : "date"
        },
        "mId" : {
          "type" : "long"
        },
        "mReadC" : {
          "type" : "long"
        },
        "mStat" : {
          "type" : "long"
        },
        "mTitle" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tagIds" : {
          "type" : "long"
        }
      }
    }
  }
}

创建带mappings的索引

创建索引通过PUT方法提交给ES,福哥下面给出了一个例子。

demo.json

{
  "mappings" : {
    "tfapi_utf8" : {
      "properties" : {
        "authorId" : {
          "type" : "long"
        },
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mCommentC" : {
          "type" : "long"
        },
        "mDatetime" : {
          "type" : "date"
        },
        "mId" : {
          "type" : "long"
        },
        "mReadC" : {
          "type" : "long"
        },
        "mStat" : {
          "type" : "long"
        },
        "mTitle" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tagIds" : {
          "type" : "long"
        }
      }
    }
  }
}

创建索引

curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'

home/topic/2021/0607/15/0c45c646b5de6ae82618c8d7467cc0e1.png

settings和mappings

如果要在创建索引的时候同时设置settings和mappings也简单,只要把这两个JSON拼到一起就行了。

data.json

{
  "settings" : {
    "index" : {
      "number_of_shards" : "5",
      "number_of_replicas" : "1",
      "version" : { }
    }
  },
  "mappings" : {
    "tfapi_utf8" : {
      "properties" : {
        "authorId" : {
          "type" : "long"
        },
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mCommentC" : {
          "type" : "long"
        },
        "mDatetime" : {
          "type" : "date"
        },
        "mId" : {
          "type" : "long"
        },
        "mReadC" : {
          "type" : "long"
        },
        "mStat" : {
          "type" : "long"
        },
        "mTitle" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tagIds" : {
          "type" : "long"
        }
      }
    }
  }
}

创建索引

curl -X PUT -H 'Content-Type: application/json' -d '@demo.json' 'http://localhost:9200/demo?pretty=true'

home/topic/2021/0607/15/0c45c646b5de6ae82618c8d7467cc0e1.png

总结

福哥今天带着童鞋们学习了如何在创建Elasticsearch索引的时候设置settings和mappings参数的技巧,要发挥ES的最大性能就需要对ES索引的settings和mappings的设置有一个深入的研究才可以。