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();
}
}
相关推荐
Java API是Elasticsearch官方提供的与Elasticsearch服务器进行交互的主要工具,它使得开发者能够用Java语言便捷地进行索引、搜索、聚合等多种操作。 ### 一、Elasticsearch核心概念 1. **节点(Node)**: Elastic...
11. **最佳实践**:了解Elasticsearch的数据接入最佳实践,如定期重新索引、使用别名(Aliases)进行读写分离、数据分片与复制策略等。 以上只是数据接入Elasticsearch可能涉及的部分核心知识点,具体PPT内容可能还会...
提供对多个索引的抽象,方便灵活的查询和操作,可以在不更改应用程序代码的情况下更改索引。 7. **性能优势**: - **倒排索引(Inverted Index)**:是Elasticsearch高效搜索的关键,允许快速查找包含特定单词的...
Elasticsearch 是一款强大的分布式搜索引擎,常用于大数据分析和实时搜索。本文将深入探讨 Elasticsearch 的基本使用,包括入门、索引管理和映射详解。 一、快速入门 1. 集群健康状况检查:通过访问 `...
在本压缩包“Laravel开发-elastic.zip”中,我们主要关注的是如何在Laravel框架中集成和使用Elasticsearch,这是一个强大的全文搜索引擎,广泛应用于数据检索和分析。以下是对这个主题的详细阐述: 一、Laravel框架...
在IT行业中,Elasticsearch(ES)是一种广泛使用的开源全文搜索引擎,它基于Lucene构建,提供了实时、分布式、可扩展的数据存储和搜索功能。当需要将数据从一个Elasticsearch集群迁移到另一个新集群时,`elasticdump...