elasticsearchelasticsearch-rest-client

Validation Failed: 1: source is missing;2: content type is missing; In ElasticSearch?


Hi I am trying to start an embedded elastic search server , then using a java high level rest client to insert a document in to an index. However i am getting the following error .

com.openmind.primecast.web.rest.PerformanceReportingIntTest  Time elapsed: 68.723 sec  <<< ERROR!
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: source is missing;2: content type is missing;
    at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:612)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1728)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1694)
    at org.elasticsearch.client.RestHighLevelClient.bulk(RestHighLevelClient.java:470)
    at com.openmind.primecast.web.rest.PerformanceReportingIntTest.startElasticServer(PerformanceReportingIntTest.java:75)

The following is my source code. in short i am having an index named cars and type car under it . I am trying to insert a document under car using java high level rest client.

package com.openmind.primecast.web.rest;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;

    import org.apache.commons.collections4.map.HashedMap;
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.json.simple.JSONObject;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    import com.openmind.primecast.AbstractCassandraTest;
    import com.openmind.primecast.PrimecastApp;

    import pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic;
    import pl.allegro.tech.embeddedelasticsearch.IndexSettings;
    import pl.allegro.tech.embeddedelasticsearch.PopularProperties;


        @RunWith(SpringRunner.class)
        @SpringBootTest(classes = PrimecastApp.class)
        public class PerformanceReportingIntTest extends AbstractCassandraTest {

            private static EmbeddedElastic embeddedElastic;

            private static RestHighLevelClient client;

            @BeforeClass
            public static void startElasticServer() throws FileNotFoundException, IOException, InterruptedException {

                embeddedElastic = EmbeddedElastic.builder().withElasticVersion("6.6.1")
                        .withSetting(PopularProperties.TRANSPORT_TCP_PORT, 9350)
                        .withSetting(PopularProperties.CLUSTER_NAME, "my_cluster").withStartTimeout(5, TimeUnit.MINUTES)
                        .withIndex("cars", IndexSettings.builder().withType("car", getSystemResourceAsStream()).build()).build()
                        .start();

                client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9350, "http")));

                BulkRequest request = new BulkRequest();
                Map<String, String> m1 = new HashedMap<>();
                m1.put("_id", "1");
                m1.put("manufacturer", "Benz");
                m1.put("model", "A Class");
                m1.put("description", "Latest Model");

                JSONObject jsonObj = new JSONObject(m1);

                request.add(new IndexRequest("cars", "car"), jsonObj);
                BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
            }

            private static InputStream getSystemResourceAsStream() throws FileNotFoundException {
                ClassLoader classloader = Thread.currentThread().getContextClassLoader();
                InputStream is = classloader.getResourceAsStream("config/elasticsearch/car-mapping.json");
                return is;
            }

            @Test
            public void test() {

            }

            @AfterClass
            public static void close() throws IOException {
                client.close();
                embeddedElastic.stop();
            }

        }

this is my car-mapping.json file

{
  "car": {
    "properties": {
      "manufacturer": {
        "type": "text",
        "index": "false"
      },
      "model": {
        "type": "text",
        "index": "true"
      },
      "description": {
        "type": "text"
      }
    }
  }
}

Really appreciate any help thank you


Solution

  • Why don't you try as the documentation suggests?

    BulkRequest request = new BulkRequest();
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("manufacturer", "Benz");
    jsonMap.put("model", "A Class");
    jsonMap.put("description", "Latest Model");
    IndexRequest indexRequest = new IndexRequest("posts").id("1").source(jsonMap);
    request.add(indexRequest);
    
    BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);