Elastic Search number of object passed must be even

elastic search the number of object passed must be even but was (1)
elasticsearch updaterequest
org.elasticsearch.action.actionrequestvalidationexception: validation failed: 1: type is missing;
indexrequest elasticsearch
org elasticsearch action index indexrequest
org elasticsearch action index indexrequest ifseqno j
org elasticsearch action index indexrequest seqno
indexresponse elasticsearch

I am learning about elastic search and I am following the next tutorial, but I get the next error

Exception in thread "main" java.lang.IllegalArgumentException: The number of object passed must be even but was [1]
at  org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:451)
at elastic.elasti.App.lambda$0(App.java:55)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at elastic.elasti.App.indexExampleData(App.java:53)
at elastic.elasti.App.main(App.java:45)

Could you help me to fix it please?

public class App 
{
    public static void main( String[] args ) throws TwitterException, UnknownHostException
    {
    System.out.println( "Hello World!" );
    List tweetJsonList = searchForTweets();

    Client client = TransportClient.builder().build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    String index = "tweets_juan";
    client.admin().indices()
                    .create(new CreateIndexRequest(index))
                    .actionGet();
    indexExampleData(client, tweetJsonList, index);
    searchExample(client);
}
public static void indexExampleData(Client client, List tweetJsonList, String index) {


    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

    tweetJsonList.forEach((jsonTweet) -> {
        bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan")
                .source(jsonTweet));
    });

    BulkResponse bulkItemResponses = bulkRequestBuilder.get();
}




public static void searchExample(Client client) {
    BoolQueryBuilder queryBuilder = QueryBuilders
            .boolQuery()
            .must(termsQuery("text", "españa"));

    SearchResponse searchResponse = client.prepareSearch("tweets_juan")
            .setQuery(queryBuilder)
            .setSize(25)
            .execute()
            .actionGet();
     }

public static List searchForTweets() throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    Query query = new Query("mundial baloncesto");
    List tweetList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        QueryResult queryResult = twitter.search(query);
        tweetList.addAll(queryResult.getTweets());
        if (!queryResult.hasNext()) {
            break;
        }
        query = queryResult.nextQuery();
    }
    Gson gson = new Gson();

    return (List) tweetList.stream().map(gson::toJson).collect(Collectors.toList());
    }
}

I know it's late but the simple answer to this is adding XContentType.JSON along with source which is available in ElasticSearch library package org.elasticsearch.common.xcontent

public static void indexExampleData(Client client, List tweetJsonList, String index) {


    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

    tweetJsonList.forEach((jsonTweet) -> {
        bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan")
                .source(jsonTweet,XContentType.JSON));
    });

    BulkResponse bulkItemResponses = bulkRequestBuilder.get();
}

Error in BulkRequest java API in Elasticsearch : "The number of , Ok I got the fix : Use Map<String, Object> instead of Map <String,String> Map<​String,Object> jsonMap = new HashMap<String,Object>(); jsonMap.put("name",  Number of object passed must be even. Elasticsearch. May 23, 2015, 5:05am #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();

Summary:

  1. Json object cannot be used as a source for indexing
  2. Either Stringify your json by using something like Jackson or set the source as Map

Jackson:

String stringifiedJson = objectMapper.writeValueAsString(jsonObject)

Number of object passed must be even - Elasticsearch, 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. Contribute to elastic/elasticsearch development by creating an account on GitHub. (" The number of object passed must be even but was

If you use the org.json library to manage JSON content and encounter this error, you can solve it this way (setting the source as Map as skgemini proposed in his answer):

JSONObject dataAsJson = new JSONObject(dataAsJsonFormattedString);
HashMap<String, Object> dataAsMap = new HashMap<String, Object>(dataAsJson.toMap());
bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan").source(dataAsMap, XContentType.JSON));

Elastic Search number of object passed must be even, Exception in thread "main" java.lang.IllegalArgumentException: The number of object passed must be even but was [1] at org.elasticsearch.action.index. BulkRequestBuilder throws this exception: java.lang.IllegalArgumentException: The number of object passed must be even but was [1] when invoking it via:

Elasticsearch Exception:The number of object passed must be even , Licensed to Elasticsearch under one or more contributor. * license <b>Note: the number of objects passed to this method must be an even. * number. Also the​  Out of the above, the search_type, request_cache and the allow_partial_search_results settings must be passed as query-string parameters. The rest of the search request should be passed within the body itself. The body content can also be passed as a REST parameter named source.

elasticsearch/IndexRequest.java at master · elastic/elasticsearch , Licensed to Elasticsearch under one or more contributor throw new IllegalArgumentException("The number of object passed must be even but was [" +  Returns search hits that match the query defined in the request. Allows you to execute a search query and get back search hits that match the query. The query can either be provided using a simple query string as a parameter, or using a request body. To ensure fast responses, the search API will respond with partial results if one or more

IndexRequest.java, When java implements elasticsearch to insert data, I encountered the following exception: IllegalArgumentException:The number of object passed must be even​  The number of version conflicts that the update by query hit. noops. The number of documents that were ignored because the script used for the update by query returned a noop value for ctx.op. retries. The number of retries attempted by update by query. bulk is the number of bulk actions retried, and search is the number of search actions retried.

Comments
  • this should be the selected answer