【java操作Elasticsearch之mapping和aliases】

Elasticsearch之创建mapping--字段类型映射,以及别名

package estest.cn.com.esdemo;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.common.xcontent.XContentFactory;

/**

<dependency>

    <groupId>org.elasticsearch</groupId>

    <artifactId>elasticsearch</artifactId>

    <version>2.2.0</version>

</dependency>

    <!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->

<dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.5.2</version>

</dependency>

    

    <dependency>

  <groupId>net.sf.json-lib</groupId>

  <artifactId>json-lib</artifactId>

  <version>2.4</version>

  <classifier>jdk15</classifier>

</dependency>

 * 

 */

@SuppressWarnings("deprecation")

public class App {

public static void main(String[] args) throws Exception {

System.out.println("Hello World!");

String url ="http://192.168.1.111:9200/";

String json =  createMapping();

doPost(url+"index_users3", JSONObject.fromObject( json));

//

String alias =  addAlias("index_users3","ttest");

doPost(url+"_aliases", alias);

}

/**

* post请求

* @param url

* @param json

* @return

*/

public static JSONObject doPost(String url, JSONObject json) {

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(url);

JSONObject response = null;

try {

StringEntity s = new StringEntity(json.toString(),HTTP.UTF_8);

s.setContentEncoding("UTF-8");

s.setContentType("application/json;charset=UTF-8");// 发送json数据需要设置contentType

post.setEntity(s);

HttpResponse res = client.execute(post);

if (res.getStatusLine().getStatusCode() == 200) {

HttpEntity entity = res.getEntity();

String result = EntityUtils.toString(entity,"UTF-8");// 返回json格式:

System.out.println(result);

}

} catch (Exception e) {

throw new RuntimeException(e);

}

return response;

}

public static JSONObject doPost(String url, String str) {

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(url);

JSONObject response = null;

try {

StringEntity s = new StringEntity(str,HTTP.UTF_8);

post.addHeader("Content-Type", "application/json; charset=UTF-8"); 

post.setEntity(s);

HttpResponse res = client.execute(post);

if (res.getStatusLine().getStatusCode() == 200) {

HttpEntity entity = res.getEntity();

String result = EntityUtils.toString(res.getEntity(),"UTF-8");// 返回json格式:

System.out.println(result);

}

} catch (Exception e) {

throw new RuntimeException(e);

}

return response;

}

public static String  createMapping() throws Exception {

XContentBuilder builder = XContentFactory.jsonBuilder()

.startObject()

.startObject("settings")

.startObject("index")

.field("number_of_shards").value(2)

.field("number_of_replicas").value(1)

.endObject()

.endObject()

.startObject("mappings")

   .startObject("userinfo")

.startObject("properties")

.startObject("name").field("type", "string").field("store", "yes").endObject()

.startObject("address").field("type", "string").field("index", "not_analyzed").endObject()

.startObject("sal").field("type", "double").endObject()

.startObject("sex").field("type", "boolean").endObject()

.startObject("age").field("type", "integer").endObject()

.startObject("createDate").field("type", "date").endObject()

.endObject() 

.endObject();

System.out.println(builder.string());

return builder.string();

}

//{"actions":[{"add":{"index":"index_users3","alias":"5"}}]}:

public static String addAlias(String index,String alias) throws Exception {

XContentBuilder builder = XContentFactory.jsonBuilder()

.startObject()

.startArray("actions")

.startObject()

.startObject("add")

.field("alias").value(alias).

 field("index").value(index)

.endObject().endObject()

.endArray().endObject();

System.out.println(builder.string());

return builder.string();

}

}

 

 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2300212