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)

以下是我的源代码。简而言之,我有一个名为汽车的索引,并在其下键入汽车。我正在尝试使用 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

2

您为什么不按照文档的建议尝试?

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);
于 2019-08-20T12:01:58.407 回答