已下载.90.6,已解压缩,已将Elastic Search移至/ usr / share / elasticsearch(在centosx64 6.4上使用chmod 777 -R权限),将群集重命名为 somethingstupid 并启动服务器。< / p>
已安装的插件ESHead和ESBrowser b / c我很新并且需要它(习惯了Solr的漂亮ui)。这样我知道服务器也在运行。
我可以通过curl创建索引:curl -XPOST 'http://localhost:9200/testindex'
并删除它:curl -XDELETE 'http://localhost:9200/testindex'
当我尝试创建新索引并索引类型文档的文档并通过Java API查看它时,eclipse运行代码,在控制台中显示基本日志记录,然后关闭而没有错误。此外,在我的日志中,最新一行显示我已启动弹性搜索,但没有别的。它就像代码甚至没有达到弹性搜索。运行java api后,没有索引或文章出现。我错过了什么?
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import static org.elasticsearch.node.NodeBuilder.*;
public class PostES {
public static void main (String args[]){
PostES p = new PostES();
p.postElasticSearch();
}
public static Map<String, Object> putJsonDocument(String title, String content, Date postDate, String author){
Map<String, Object> jsonDocument = new HashMap<String, Object>();
jsonDocument.put("title", title);
jsonDocument.put("conten", content);
jsonDocument.put("postDate", postDate);
jsonDocument.put("author", author);
return jsonDocument;
}
private void postElasticSearch(){
Node node = nodeBuilder().node();
Client client = node.client();
client.prepareIndex("testindex", "article")
.setSource(putJsonDocument("Example Title",
"This description is so important. You don't even know!",
new Date(),
"J.R."))
.execute().actionGet();
node.close();
}
}
我的来源:http://java.dzone.com/articles/elasticsearch-java-api。包括弹性文档在内的所有其他内容都以某种方式失败....(方法jsonBuilder()未定义为PostES类型。)
根据文件我应该能够做到这一点。但它也没有做任何事情:
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
public class TestPostMethod2 {
public static void main(String[] args) {
Node node = nodeBuilder().local(true).node();
Client client = node.client();
String json =
"{\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elastic Search\"}";
IndexResponse response = client.prepareIndex("testindex", "article")
.setSource(json)
.execute()
.actionGet();
}
}
答案 0 :(得分:1)
您需要在创建节点时指定群集名称并指定它是client node,或使用transport client。
在这两种情况下,您目前正在做的是启动一个新节点,该节点创建一个具有默认名称的新群集,而您希望有一个客户端节点来加入现有群集。
Node node = nodeBuilder().clusterName("somethingstupid").client(true).node();
答案 1 :(得分:0)
不确定这是否相关,但是当我最初尝试从eclipse连接到运行的ES集群时,我必须将其添加到我的tomcat的参数中:
-Delasticsearch -Des.path.home=/usr/local/opt/elasticsearch
其中elasticsearch是您在elasticsearch.yml中定义的名称,当然还有您安装ES的路径
答案 2 :(得分:0)
我可以让api工作的方法是不使用它:
import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
public class PostHttpClient {
public static void main(String[] args) {
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://localhost:9200/testindex/article");
StringEntity input = new StringEntity("{\"name\":\"ES JAVA API WorkAround\",\"category\":\"Garbage\"}");
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 3 :(得分:0)
为什么不将Transport Client与java.util.Map一起使用?
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import java.util.*;
public class Test {
public static Client getTransportClient(String host, int port) {
return new TransportClient()
.addTransportAddress(new InetSocketTransportAddress(host, port));
}
public static IndexResponse doIndex(Client client, String index, String type, String id, Map<String, Object> data) {
return client.prepareIndex(index, type, id)
.setSource(data)
.execute()
.actionGet();
}
public static void main(String[] args) {
Client client = getTransportClient("localhost", 9300);
String index = "twitter";
String type = "tweet";
String id = null; // set id here if you want to
Map<String, Object> data = new HashMap<String, Object>();
data.put("text", "Posted from Java @ " + System.currentTimeMillis());
data.put("user", "Igal");
data.put("date", new Date());
IndexResponse result = doIndex(client, index, type, id, data);
System.out.println((result.isCreated() ? "created" : "updated") + " document " + result.getId() );
client.close();
}
}
答案 4 :(得分:0)
我建议任何人都使用rest client(jest)代替transport client,请记住添加在link-1和link-2中指定的所有依赖项
替换 “ index_name” =带有您的索引名称
使用Java创建简单索引并插入数据,
public void setData(String data)
{
try
{
// Construct a new Jest client according to configuration via factory
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")
.multiThreaded(true)
.build());
JestClient client = factory.getObject();
client.execute(new CreateIndex.Builder("index_name").build());
String source = jsonBuilder()
.startObject()
.field("data", data)
//other data
.endObject().string();
System.out.println(source); // verify data
Index index = new Index.Builder(source).index("index_name").type("_type").build();
client.execute(index);
}
catch(IOException i)
{
i.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}