ElasticSearch java篇学习笔记(二)- 创建索引(Index)

创建索引

     es中创建创建Index提供了四中方法:

     ①:JSON

     ②:MAP

     ③:bean

     ④:ES自己提供的工具

下面逐个练习:

下面用到的工具类:

/**
 * 搜索平台-ELASTIC-INDEX:
 * 

* 索引信息 * * @author 骆林 2018-06-21 */ public class Index { /** * 索引 */ private String index; /** * 类型 */ private String type; /** * ID */ private String id; public String getIndex() { return index; } public void setIndex(String index) { this.index = index; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getId() { return id; } public void setId(String id) { this.id = id; } }

单元测试类:

/**
 * 搜索平台-ELASTIC-INDEX:
 * 

* index工具类 * * @author 骆林 2018-06-21 */ public class IndexTest { public static TransportClient client = null; public static Index index = new Index(); @Before public void initClient(){ client = ESClient.getClient(); index.setIndex("staff"); index.setType("user"); } @Test public void indexString(){ String json = "{" + "\"username\":\"luolin\"," + "\"password\":\"123456\"," + "\"realname\":\"骆林\"," + "\"age\":\"27\"," + "\"sex\":\"man\"" + "}"; ESIndex esIndex = new ESIndex(); index.setId("1"); Boolean result = esIndex.StringCreate(client,json,index); client.close(); } @Test public void indexMap(){ Map,Object> map = new HashMap,Object>(); map.put("username","hello"); map.put("password","123456"); map.put("realname","AA"); map.put("age","25"); map.put("sex","woman"); ESIndex esIndex = new ESIndex(); index.setId("2"); Boolean result = esIndex.mapCreate(client,map,index); client.close(); } @Test public void indexBean() throws JsonProcessingException{ User user = new User(); user.setUsername("阿三"); user.setPassword("123456789"); user.setRealname("刘能"); user.setAge(60); user.setSex("man"); ESIndex esindex = new ESIndex(); index.setId("3"); Boolean result = esindex.beanCreate(client,user,index); client.close(); } @Test public void indexEsHelp() throws IOException{ ESIndex esIndex = new ESIndex(); index.setId("4"); Boolean result = esIndex.EsHelpCreate(client,index); client.close(); } }



由于这个比较简单:所以直接上代码

一:用JSON的方式创建Index

/**
* JSON创建索引
*
* @param  client ES客户端
* @param  index json文档
* @return java.lang.Boolean
* @author [luo.lin] [ 2018/6/21 16:37]
*/
public Boolean StringCreate(TransportClient client, String json, Index index){

    IndexResponse response = client.prepareIndex(index.getIndex(),index.getType(),index.getId())
                                    .setSource(index, XContentType.JSON)
                                    .get();

    RestStatus status = response.status();

    if(response.getResult().equals("created")){
        return true;
    }
    return false;
}

测试方法:上面测试类中的方法

@Test
public void indexString(){

    String json = "{" +
                  "\"username\":\"luolin\"," +
                  "\"password\":\"123456\"," +
                  "\"realname\":\"骆林\"," +
                  "\"age\":\"27\"," +
                  "\"sex\":\"man\"" +
                  "}";
    ESIndex esIndex = new ESIndex();
    index.setId("1");
    Boolean result = esIndex.StringCreate(client,json,index);
    client.close();

}

二:使用MAP的方式创建Index

/**
 * map创建索引
 *
 * @param  client ES客户端
 * @param  mapEntry map文档对象
 * @return java.lang.Boolean
 * @author [luo.lin] [ 2018/6/21 16:37]
 */
public Boolean mapCreate(TransportClient client,Map,Object> mapEntry,Index index){

    IndexResponse response = client.prepareIndex(index.getIndex(),index.getType(),index.getId())
                                   .setSource(mapEntry,XContentType.JSON)
                                   .get();

    if(response.getResult().equals("created")){
        return true;
    }
    return false;

}
测试方法:上面测试类中的方法

@Test
public void indexMap(){

    Map,Object> map = new HashMap,Object>();
    map.put("username","hello");
    map.put("password","123456");
    map.put("realname","AA");
    map.put("age","25");
    map.put("sex","woman");

    ESIndex esIndex = new ESIndex();
    index.setId("2");
    Boolean result = esIndex.mapCreate(client,map,index);
    client.close();

}

三:使用实例bean的方式创建Index

/**
 * bean创建索引
 *
 * @param  client ES客户端
 * @param  user bean实例
 * @return java.lang.Boolean
 * @author [luo.lin] [ 2018/6/21 16:37]
 */
public Boolean beanCreate(TransportClient client, User user,Index index) throws JsonProcessingException{

    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(user);

    IndexResponse response = client.prepareIndex(index.getIndex(),index.getType(),index.getId())
                                   .setSource(json,XContentType.JSON)
                                   .get();
    if(response.getResult().equals("created")){
        return true;
    }
    return  false;

}

测试方法:上面测试类中的方法

/**
 * bean创建索引
 *
 * @param  client ES客户端
 * @param  user bean实例
 * @return java.lang.Boolean
 * @author [luo.lin] [ 2018/6/21 16:37]
 */
public Boolean beanCreate(TransportClient client, User user,Index index) throws JsonProcessingException{

    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(user);

    IndexResponse response = client.prepareIndex(index.getIndex(),index.getType(),index.getId())
                                   .setSource(json,XContentType.JSON)
                                   .get();
    if(response.getResult().equals("created")){
        return true;
    }
    return  false;

}

四:使用ES提供的工具创建Index

/**
* 方法EsHelpCreate的功能描述
*
* @param  client ES客户端
 *@param index 索引对象
* @return boolean
* @author [luo.lin] [ 2018/7/4 20:39]
*/
public boolean EsHelpCreate(TransportClient client,Index index) throws IOException{

    XContentBuilder builder = jsonBuilder()
                              .startObject()
                              .field("username","zhaosi")
                              .field("password","9876554321")
                              .field("realname","赵四")
                              .field("age",65)
                              .field("sex","man")
                              .endObject();

    String json = builder.string();

    IndexResponse response = client.prepareIndex(index.getIndex(),index.getType(),index.getId())
                                   .setSource(json,XContentType.JSON)
                                   .get();

    if(response.getResult().toString().equals("created")){
        return true;
    }
    return false;

}

测试方法:上面测试类中的方法

@Test
public void indexEsHelp() throws IOException{
    ESIndex esIndex = new ESIndex();
    index.setId("4");
    Boolean result = esIndex.EsHelpCreate(client,index);
    client.close();
}

注:如果使用ES自带的工具类创建Index,则需要导入jackson


    com.fasterxml.jackson.core
    jackson-databind
    2.9.5

总结:

       ES通过提供prepareIndex方法来创建创建Index,并且用IndexResponse 对象来封装创建的结构信息,

      IndexResponse的构造方法为:

public IndexResponse(ShardId shardId, String type, String id, long seqNo, long primaryTerm, long version, boolean created) {
    super(shardId, type, id, seqNo, primaryTerm, version, created ? Result.CREATED : Result.UPDATED);
}

其中包含了当前索引的类型、id、版本号、操作结果等相关信息


你可能感兴趣的:(ElasticSearch java篇学习笔记(二)- 创建索引(Index))