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

时间:2021-03-21 00:10:59

      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


后面我打算用ES 解析mysql慢查询日志的分析一个实例,也很简单,正好和我前面logstash结合对比一下。在后面的话,打算es搜索服务lucene框架、开源druid jdbc深入了解一下