Ale*_*dra 5 java rest elasticsearch
我目前正在构建一个elasticsearch plugin公开REST端点(从这篇文章开始)
我可以这样调用我的端点curl:
curl -X POST 'http://my-es:9200/lt-dev_terminology_v1/english/_terminology?pretty=-d '{
"segment": "database",
"analyzer": "en_analyzer"
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何java使用传输客户端调用相同的端点?你能指点我一些教程吗?
我建议你看看这里。这对您来说应该是一个很好的起点。
让我总结一下:
考虑以下参数:
String clustername = "...";
String clientTransportHost = "...";
Integer clientTransportPort= "...";
String clientIndex = "...";
String indexType = "...";
Run Code Online (Sandbox Code Playgroud)
当然,您可以用您想要使用的设置替换这些点。
然后定义您的集群Settings:
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", clustername).build();
Run Code Online (Sandbox Code Playgroud)
您实例化 TransportClient 对象:
TransportClient client = new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(clientTransportHost, clientTransportPort));
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法验证您的连接:
private void verifyConnection(TransportClient client) {
ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
if (nodes.isEmpty()) {
throw new ElasticsearchException(
"No nodes available. Verify ES is running!");
} else {
log.info("connected to nodes: " + nodes.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
PS:要使用 log.info() 方法,您必须实例化一个 Logger。
现在你可以使用验证方法:
verifyConnection(client);
Run Code Online (Sandbox Code Playgroud)
完成所有操作后,您现在可以按照示例准备搜索:
SearchResponse response = client.prepareSearch(clientIndex)
.setTypes(indexType)
.addFields("...", "...")
.setSearchType(SearchType.DEFAULT)
.execute()
.actionGet();
Run Code Online (Sandbox Code Playgroud)
PS:在 Elasticsearch 1.3 上测试
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |