2017-02-10 52 views
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" 
      } 
     } 
     } 
    } 
} 

回答

0

此代码:

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()在片断的到底是什么触发了电话。

+0

aaah!我知道我错过了一些愚蠢的东西。谢谢你的解释。! –