Number of object passed must be even


(Marc) #1

Hello,

Iam new on elasticsearch, and I want to play a little.

I used a simple Java-Code to insert one document:

  Node node = NodeBuilder.nodeBuilder().clusterName("Suche")
                .node();
        Client client = node.client();
        JSONObject obj = new JSONObject();
        try {
            obj.put("id", "1");
            obj.put("adresse", "USA");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        IndexRequest indexRequest = new IndexRequest("Test", "Adresses");
        indexRequest.source(obj);
        IndexResponse response = client.index(indexRequest).actionGet();
        node.close(); 

But well, I got an exception

The number of object passed must be even but was [1]

Can anyone help? Thanks.


(Adrien Grand) #2

This is because JSONObject is not allowed as a request source. Alternatively, you could use a map, or simply do

indexrequest.source("id", "1", "adresse", "USA");

(Marc) #3

Thanks for the fast response.


(Matty) #4

I have the same issue

BulkRequestBuilder bulkRequest = client.prepareBulk();

	  for (int i = 0; i < jsonDataList.length(); i++) { //
	 
		  System.out.println(jsonDataList.optJSONObject(i)); 
		  JSONObject object = jsonDataList.optJSONObject(i);
	  
	  Map<String, String> map = new HashMap<String, String>(); 
		  Iterator keys = object.keys();
	  
	  try { while (keys.hasNext()) { 
		  String key = (String) keys.next();
		  map.put(key, object.get(key).toString()); 
		  } 
	  System.out.println(map);
	  

	   bulkRequest.add(client.prepareIndex(INDEX_NAME, INDEX_TYPE,object.get("id").toString())
			   .setSource(map)); //JSONObject is not allowed as a request source 
	   } catch (JSONException e) { 
		   // TODO Auto-generated catch block e.printStackTrace(); 
		   } 
	  } // ::End of for
	  // brb.execute().actionGet();
	  
	  BulkResponse bulkResponse = bulkRequest.execute().actionGet();

anyone can help ?


(Becjit) #5

use Map<String, Object> instead of Map <String,String>. see Stackoverflow Question


(Harshit Shah) #6

I have same issue Regarding this but i pass
ArrayList<ArrayList> batch = new ArrayList<ArrayList>();

Is there any solution ?????????????

`BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {

		public void beforeBulk(long arg0, BulkRequest arg1) {
			// TODO Auto-generated method stub

		}

		public void afterBulk(long arg0, BulkRequest arg1, Throwable arg2) {
			// TODO Auto-generated method stub

		}

		public void afterBulk(long arg0, BulkRequest arg1, BulkResponse arg2) {
			// TODO Auto-generated method stub

		}
	}).setBulkActions(1000).build();
	bulkProcessor.add(new IndexRequest(index, type).source(batch));
}`

(system) #7