1
所以我只是想通过java transport client api
在我的indice
中添加数据。我的代码运行没有任何例外。插入文档elasticsearch java API不起作用
- 它成功连接到我的服务器,我可以看到我的控制台上的连接细节。
- 我列出了服务器上所有可用的
indices
,它也显示了这一点。 - 然后我试图插入
docs
到我indice
称为logs
- 代码运行没有任何异常。
我迷失在这里,不知道我在想什么。数据未插入
indice
。任何帮助表示赞赏。
下面是我的代码:
public static void main(String[] args) {
try {
ElasticSearchMain es = new ElasticSearchMain();
ElasticSearchMain.configureClient();
JSONObject data = new JSONObject();
data.put("serverip", "bhavik");
data.put("classname", "bhavik");
data.put("methodname", "bhavik");
data.put("exception", "bhavik");
data.put("logexception", "4896681231231232");
data.put("timestamp", "1900-00-00");
es.setIndex(data.toString(), "logs");
} catch (Exception e) {
e.printStackTrace();
}
}
public IndexRequestBuilder setIndex(String data,String IndexName){
try{
return client.prepareIndex(IndexName, "event").setSource(data);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void configureClient(){
try{
if(client==null){
String address = "localhost";
int port = 9300;
BasicConfigurator.configure();
client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), port));
String[] indices = client.admin().indices().getIndex(new GetIndexRequest()).actionGet().getIndices();
for (String s : indices) {
System.out.println("indice ->> " + s);
}
}
}catch(Exception e){
e.printStackTrace();
EmgrLog.AppendExceptionToLog(e);
}
}
指数之映射:
PUT logs
{
"mappings": {
"event": {
"properties": {
"serverip": {
"type": "integer",
"index": "not_analyzed"
},
"classname": {
"type": "string",
"analyzer": "english"
},
"methodname": {
"type": "string",
"index": "not_analyzed"
},
"exception": {
"type": "string",
"index": "not_analyzed"
},
"logexception": {
"type": "string"
},
"timestamp":{
"type": "date"
}
}
}
}
}
aaah!我知道我错过了一些愚蠢的东西。谢谢你的解释。! –