1

こんにちは、組み込みのエラスティック検索サーバーを起動しようとしています。次に、Java の高レベルのレスト クライアントを使用してドキュメントをインデックスに挿入します。ただし、次のエラーが発生します。

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)

以下は私のソースコードです。つまり、 cars という名前のインデックスがあり、その下に car と入力しています。Java 高レベル レスト クライアントを使用して、車の下にドキュメントを挿入しようとしています。

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();
            }

        }

これは私の car-mapping.json ファイルです

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

助けてくれて本当にありがとう

4

1 に答える 1