Elasticsearch Java API 之Index type mapping json创建修改

转:https://blog.csdn.net/u014297722/article/details/53470281

  Elasticsearch 关于java的api网上有不少资料了,最后面是一个很完整的博客讲解。我这篇主要补充一下如何使用java 对于elasticsearch搜索引擎的index创建一个新的mapping。不过再上代码之前有几点总结说一下哈,我觉得是有不少新手不清楚的:

    1.索引index索引可能有多个类型type,每个类型有自己的映射mapping,不同类型的文档可以存放在同一个索引中

    2.类型type包含一个名字typename——例如 user —— 和一个 mapping映射

   

package es.java.api_test;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.elasticsearch.client.Client;
 
public class Createmapping {
    public static void main(String[] args) throws IOException {
        Client client = ConnectES.getInstance().getTransportClient();
        // 索引可能有多个类型,每个有自己的映射,不同类型的文档可以存放在同一个索引中
        File article = new File("D:/java:eclipse/article_mapping2.txt");
        FileReader fr = new FileReader(article);
        BufferedReader bfr = new BufferedReader(fr);
        String mapping_json = null;
 
        if ((mapping_json = bfr.readLine()) != null) {
            // create indexname:if not exit index
            // client.admin().indices().prepareCreate("index_name").execute().actionGet();
            // notice: typename==mapping_json.name
            client.admin().indices().preparePutMapping("index_name")
                    .setType("index_type").setSource(mapping_json)
                    .execute().actionGet();
             System.out.println(mapping_json);
        }
        fr.close();
    }
}
解释:  mapping_json 你将要修改或者创建的mapping结构,用字符串保存下来   

      

{"index_type":{"properties":{"content":{"type":"string"},"id":{"type":"long"},"posttime":{"type":"date","format":"dateOptionalTime"},"title":{"type":"string"},"example":{"type":"string","analyzer":"ik"}}}}
             index_name 已经创建好、尚未创建的索引名称

             index_type 索引的类型名称,同一个索引可以拥有多个不同type(已测试);同时提醒:注意在mapping_json起始位置的类型名称和 setType("index_type")名字相同,否则会报错。

     上面用到了client连接,我用单例模式保护client不被多次创建(据说java 反射模式可以避开懒汉单例模式,java 不熟,新手,后面去了解) ___es 1.X版本没有builder()函数

      

package es.java.api_test;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
 
public class ConnectES {
    
    private TransportClient transportClient;
    //****************************http://blog.csdn.net/jason0539/article/details/23297037/
    //***************************其他几种单例模式
    private static ConnectES clientInstance; // 类对象唯一,懒汉单例模式————初始化类为private
 
    @SuppressWarnings("resource")
    private ConnectES() {         //初始化,自动调用
 
        // Settings settings =                               //设置集群使用
        // ImmutableSettings.settingsBuilder().put("cluster.name",
        // "elasticsearch").build();
 
        transportClient = new TransportClient()
                .addTransportAddress(new InetSocketTransportAddress(
                        "192.168.3.4", 9300));
    }
 
    // 懒汉单例模式,只有调用类 Instaance函数才会初始化这个单例
    public static ConnectES getInstance() {
        if (clientInstance == null) {
            clientInstance = new ConnectES();                 //初始化单例得到ES客户端
        }
        return clientInstance;
    }
 
    public TransportClient getTransportClient() {
        return transportClient;
    }
 
}

其它elasticsearch java api下面这个博客说的很好了,我不多赘述了。

CSDN博客-ES Java API
--------------------- 
作者:cuixuange 
来源:CSDN 
原文:https://blog.csdn.net/u014297722/article/details/53470281 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值