gpt4 book ai didi

java - 插入文档elasticsearch java API不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:47 26 4
gpt4 key购买 nike

所以我只是尝试通过 java Transport client api 在我的 indice 中添加数据。我的代码运行没有任何异常。

  • 它成功连接到我的服务器,我可以在控制台上看到连接详细信息。
  • 我列出了服务器上可用的所有索引,它也确实显示了这一点。
  • 然后我尝试将 docs 插入到名为 logsindice
  • 代码运行没有任何异常。

I am lost here and have no idea what I am missing. The data is not getting inserted in the indice. Any help is appreciated.

下面是我的代码:

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"
}
}
}
}
}

最佳答案

这段代码:

public IndexRequestBuilder setIndex(String data,String IndexName){
try{
return client.prepareIndex(IndexName, "event").setSource(data);
}catch(Exception e){
e.printStackTrace();
}
return null;
}

返回一个“请求构建器”,但该请求从未构建或执行,从逻辑上讲,不会产生任何结果。

要提交请求,您必须将其发送到elasticsearch实例。

Elasticsearch JavaAPI documentation建议这段代码:

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();

请注意,代码片段末尾的 get() 是触发调用的内容。

关于java - 插入文档elasticsearch java API不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42160151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com