【问题标题】:Validation Failed: 1: source is missing;2: content type is missing; In ElasticSearch?验证失败:1:缺少来源;2:缺少内容类型;在弹性搜索中?
【发布时间】:2019-12-25 12:41:11
【问题描述】:

您好,我正在尝试启动嵌入式弹性搜索服务器,然后使用 java 高级 rest 客户端将文档插入索引。但是我收到以下错误。

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 高级别的客户端在 car 下插入一个文档。

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"
      }
    }
  }
}

非常感谢任何帮助 谢谢

【问题讨论】:

    标签: elasticsearch elasticsearch-rest-client


    【解决方案1】:

    您为什么不按照documentation 的建议尝试一下?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2014-02-25
      • 2018-03-18
      • 2019-03-15
      相关资源
      最近更新 更多