import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
public class ESHelper { // 创建私有对象
private static TransportClient client;
static {
try {
int port = 9300;
Map<String, String> m = new HashMap<String, String>();
// 设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中,
Settings settings = Settings.settingsBuilder().put(m).put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
client = TransportClient.builder().settings(settings).build();
String ip = "";
InetAddress host = InetAddress.getLocalHost();
client.addTransportAddress(new InetSocketTransportAddress(host, port));
} catch (Exception e) {
e.printStackTrace();
}
}
// 取得实例
public static synchronized TransportClient getTransportClient() {
return client;
}
/**
* 创建一个索引
*
* @param indexName 索引名
*/
public static void createIndex(String indexName) {
try {
boolean indexExists = client.admin().indices().prepareExists(indexName).execute().actionGet().isExists();
if (indexExists) {
client.admin().indices().prepareDelete(indexName).execute().actionGet();
}
CreateIndexResponse indexResponse = getTransportClient().admin().indices().prepareCreate(indexName).get();
System.out.println(indexResponse.isAcknowledged());
// true表示创建成功
} catch (ElasticsearchException e) {
e.printStackTrace();
}
}
public static XContentBuilder createMapping(String indexName, String type) {
XContentBuilder mapping = null;
try {
mapping = jsonBuilder()
.startObject() // 索引里面的字段
.startObject("properties")
.startObject("id")
.field("type", "long")
.field("store", "yes")
.field("index", "not_analyzed")
.field("include_in_all", "false")
.endObject()
.startObject("thumb").field("type", "string").field("store", "yes").field("index", "not_analyzed").field("include_in_all", "false").endObject().endObject().endObject();
System.out.println(mapping.string());
PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).source(mapping).type(type);
client.admin().indices().putMapping(mappingRequest).actionGet();
} catch (IOException e) {
e.printStackTrace();
}
return mapping;
}
public static Integer addIndex(String indexName, String indexType) {
Client esClient = getTransportClient();
BulkRequestBuilder bulkRequest = esClient.prepareBulk();
for (int i = 1; i < 5; i++) {
try {
bulkRequest.add(esClient.prepareIndex(indexName, indexType, "" + i)
.setSource(jsonBuilder().startObject().field("id", "" + i)
.field("thumb", "http://www.chepoo.com/imges/" + i + ".jpg")
.endObject()));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bulkRequest.add(esClient.prepareIndex(indexName, indexType, "" + 5)
.setSource(jsonBuilder()
.startObject()
.field("id", "" + 5)
.field("thumb", "http://www.chepoo.com/imges/" + 5 + ".png")
.endObject()));
} catch (Exception e) {
e.printStackTrace();
}
bulkRequest.execute().actionGet();
return bulkRequest.numberOfActions();
}
public static void get(String index, String type) {
client.admin().indices().flush(new FlushRequest(index).force(true)).actionGet();
SearchResponse response = client.prepareSearch(index).setTypes(type) //查询图片为jpg或者png
.setQuery(new QueryStringQueryBuilder("jpg|png").field("thumb"))
.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
System.out.println(response);
}
}
Elasticsearch Java API测试
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近有个日志收集监控的项目采用的技术栈是ELK+JAVA+Spring,客户端语言使用的是Java,以后有机会的话...
- Elasticsearch Java API 客户端连接 一个是TransportClient,一个是NodeCl...
- 官网API地址 https://www.elastic.co/guide/en/elasticsearch/cli...