private TransportClient client = null;
//在所有的测验办法之前履行
public void init() throws Exception {
Settings settings = Settings.builder().put("cluster.name", "my-es").build();
client = new PreBuiltTransportClient(settings)
.addTransportAddresses(new TransportAddress(InetAddress.getByName("192.168.177.12"), 9300))
@Test
//获取Admin的API
//运用Admin API对索引进行操作
//预备创立索引
//装备索引参数
//参数装备器
.put("index.number_of_shards", 4)
.put("index.number_of_replicas", 2)
//真实履行
}
//跟索引增加mapping信息(给表增加schema信息)
public void putMapping() {
client.admin().indices().prepareCreate("twitter")
.addMapping("tweet",
" "tweet": {
" +
" "message": {
" +
" }
" +
" }
" +
.get();
* 你可以经过dynamic设置来操控这一行为,它可以承受以下的选项:
* false:疏忽新字段
* @throws IOException
@Test
//1:settings
settings_map = new HashMap
XContentBuilder builder = XContentFactory.jsonBuilder()
.field("dynamic", "true")
.startObject("properties")
.startObject("num")
.field("type", "integer")
.field("index", "not_analyzed")
.field("store", "yes")
//name特点
//string类型
//在文档中存储
//树立索引
//运用ik_smart进行分词
.endObject()
.endObject();
CreateIndexRequestBuilder prepareCreate = client.admin().indices().prepareCreate("user_info");
prepareCreate.setSettings(settings_map).addMapping("user", builder).get();
* XContentBuilder mapping = jsonBuilder()
.startObject("productIndex")
.startObject("title").field("type", "string").field("store", "yes").endObject()
.startObject("price").field("type", "double").endObject()
.startObject("type").field("type", "integer").endObject()
.endObject()
.endObject();
client.admin().indices().putMapping(mappingRequest).actionGet();
/**
* not_analyzed,建索引不分词
* expected [no], [not_analyzed] or [analyzed]
*/
@Test
//1:settings
settings_map = new HashMap
XContentBuilder builder = XContentFactory.jsonBuilder()
.field("dynamic", "true")
.startObject("id")
.field("store", "yes")
.startObject("name")
.field("index", "not_analyzed")
.startObject("age")
.endObject()
.field("type", "integer")
.startObject("team")
.field("index", "not_analyzed")
.startObject("position")
.field("index", "not_analyzed")
.startObject("description")
.field("store", "no")
.field("analyzer", "ik_smart")
.startObject("addr")
.field("store", "yes")
.field("analyzer", "ik_smart")
.endObject()
prepareCreate.setSettings(settings_map).addMapping("player", builder).get();
}
A8站源码交易平台