zoukankan      html  css  js  c++  java
  • ElasticSearch添加mapping

    1.创建索引

    /**
    * 创建索引
    *
    * @param indexName
    */
    public static void createIndex(String indexName) {
    //插入前删除,以免报错
    boolean flag = client.admin().indices().exists(new IndicesExistsRequest().indices(new String[]{indexName})).actionGet().isExists();
    if (flag){
    client.admin().indices().prepareDelete(indexName).execute().actionGet();
    }
    client.admin().indices().prepareCreate(indexName).get();
    //client.admin().indices().prepareCreate(indexName)
    // .setSettings(Settings.builder()
    // .put("index.number_of_shards", 3)
    // .put("index.number_of_replicas", 2)
    // )
    // .get();
    }

    2.创建mapping

        /**
         * 创建mapping,根据需求修改
         *
         * @param indexName
         * @param typeName
         */
        public static void createMapping(String indexName, String typeName) {
            XContentBuilder mappingType = null;
            try {
                mappingType = jsonBuilder()
                        .startObject()
                        .startObject(typeName)
                        .startObject("properties")
                        .startObject("id").field("type", "integer").field("store", "yes").endObject()
                        .startObject("Name").field("type", "string").field("store", "yes").field("analyzer", "ik_smart").field("search_analyzer", "ik_smart").endObject()
                        .startObject("updatedate").field("type", "date").field("store", "yes").endObject()
                        .endObject()
                        .endObject()
                        .endObject();
            } catch (IOException e) {
                logger.error("build company mapping failed", e);
            }
            PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(typeName).source(mappingType);
            client.admin().indices().putMapping(mapping).actionGet();
            client.admin().indices().prepareRefresh().get();
            //client.close();
        }
  • 相关阅读:
    「JOISC 2020 Day3」收获
    $ ext{Min25}$筛
    [做题记录-图论] [NEERC2017]Journey from Petersburg to Moscow [关于处理路径前$k$大的一种方法]
    [复习笔记]一些有意思的解法技巧 (转 Dpair
    [比赛记录] CSP2021-S 题解
    [转]C++学习心得
    Sigmoid function in NN
    Kernel Regression from Nando's Deep Learning lecture 5
    Python codes
    php中mail()改用msmtp发送邮件
  • 原文地址:https://www.cnblogs.com/xmeo/p/6956241.html
Copyright © 2011-2022 走看看