一、增加head插件
elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通过插件把它集成到es。或直接下载源码,在本地打开index.html运行它。该工具的git地址是: https://github.com/mobz/elasticsearch-head
1.elasticsearch/bin/plugin -install mobz/elasticsearch-head
2.运行es
3.打开http://localhost:9200/_plugin/head/
二、增加bigdesk插件
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等。项目git地址: https://github.com/lukas-vlcek/bigdesk。
和head一样,它也是个独立的网页程序,使用方式和head一样。
插件安装运行:
1.bin/plugin -install lukas-vlcek/bigdesk
2.运行es
3.打开http://localhost:9200/_plugin/bigdesk/
三、创建一个索引
#创建索引
curl -XPUT 'http://localhost:9200/orderbank_v1' -d '{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}';
#删除索引
curl -XDELETE 'http://localhost:9200/orderbank_v1';
四、字段的mapping
#创建mapping关系
curl -XPOST localhost:9200/orderbank_v1/ob/_mapping -d '{
"ob": {
"_all": {
"enable": true
},
"_source": {
"compress": true
},
"properties": {
"orderId": {
"type": "string",
"required": true
},
"price": {
"type": "float",
"required": true
},
"createTime": {
"type": "date",
"required": true
}
}
}
}'
五、java API使用
private TransportClient esClient = null;
public void init() {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "test.es").build();
esClient = new TransportClient(settings).addTransportAddress(
new InetSocketTransportAddress("192.168.146.62", 9300)).addTransportAddress(
new InetSocketTransportAddress("192.168.146.56", 9300));
}
public void close() {
if (esClient != null) {
esClient.close();
}
}
/**
*
* 创建索引
* @throws Exception
* @author zhangdongfang
*/
public void createIndex() throws Exception {
IndexResponse response = esClient
.prepareIndex(INDEX_NAME, TYPE_NAME, "569875")
.setSource(
XContentFactory.jsonBuilder().startObject().field("orderId", "569875")
.field("price", Float.valueOf(1212)).field("createTime", new Date()).endObject())
.setTTL(8000).execute().actionGet();
System.out.println(response.getId());
}
/**
*
* 通过id取值
* @throws Exception
* @author zhangdongfang
*/
public void getId() throws Exception {
GetResponse responseGet = esClient.prepareGet(INDEX_NAME, TYPE_NAME, "569875").execute().actionGet();
System.out.println(responseGet.getSourceAsString());
}
/**
*
* 查询数据
* @throws Exception
* @author zhangdongfang
*/
public void queryIndex() throws Exception {
SearchRequestBuilder builder = esClient.prepareSearch(INDEX_NAME)
.setQuery(QueryBuilders.termQuery("orderId", "569875")).setTypes(TYPE_NAME)
.setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100);
SearchResponse response = builder.execute().actionGet();
System.out.println(" " + response);
System.out.println(response.getHits().getTotalHits());
}
private static final String INDEX_NAME = "orderbank_v1";
private static final String TYPE_NAME = "ob";