【问题标题】:Put mapping in ElasticSearch by JAVA API通过 JAVA API 将映射放入 ElasticSearch
【发布时间】:2020-03-13 09:12:12
【问题描述】:

我想通过 JAVA API 为字段映射但失败了。以下是详细信息:

我的数据结构是:

{
  "mje-test-execution-id": "464b66ea6c914ddda217659c84a3cb9d",
  "jvm-free-memory": 315245608,
  "jvm-total-memory": 361758720,
  "system-free-memory": 0,
  "jvm-max-memory": 7600078848,
  "system-total-memory": 34199306240,
  "memory-time-stamp": "2020-03-12T05:12:16.835Z",
  "mje-host-name": "CN-00015345",
  "mje-test-suite-name": "SCF Test no mje version",
  "mje-version": "1.8.7771-SNAPSHOT",
  "mje-test-artifact-id": "msran-regression-tests",
  "mje-test-version": "1.8.7771-SNAPSHOT",
  "stp-id": "vran-stp",
  "mje-test-location": {
    "lat": 58.41,
    "lon": 15.62
  }
}

我要做的是:将“mje-t​​est-location”类型设置为“geo_point”

我的代码 sn-p:

public void postMapping(String indexName, String field, String type) throws IOException {
        GetIndexRequest request = new GetIndexRequest(indexName);

        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        if (!exists) {
            LOGGER.info("index {} does not exist. Now to post mapping.", indexName);
            PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {

                builder.startObject("properties");
                {
                    builder.startObject(field);
                    {
                        builder.field("type", type);
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            putMappingRequest.source(builder);
            //

            AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest,
                    RequestOptions.DEFAULT);

            boolean acknowledged = putMappingResponse.isAcknowledged();
            if (acknowledged) {
                LOGGER.info("Succeed to put mapping: field:{}, type: {}", field, type);
            }
        }

        LOGGER.info("Fail to put mapping due to index {} already exist, ", indexName);
    }

错误信息:

15:59:54.397 [main] DEBUG org.elasticsearch.client.RestClient - request [PUT http://seliiuapp00269.lmera.ericsson.se:9208/mje-scf-v2-20200313-post/_mapping?master_timeout=30s&timeout=30s] returned [HTTP/1.1 400 Bad Request]
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: mapping type is missing;]

ElasticSearch JAVA API 版本:<elasticsearch.rest.high.level.client>7.0.0</elasticsearch.rest.high.level.client>

【问题讨论】:

  • 如果我理解的话,postMapping() 方法会为您文档的每个字段调用?
  • 是的。那么你对此有什么想法吗?
  • @Littlesun,为什么你要为每个字段调用这个,这会导致很多 n/w 调用,而不是你应该一次性创建一个包含所有字段的映射
  • 是的,你应该写一个方法postMapping(indexName),处理所有字段,例如一个json文件或另一个方法:elastic.co/guide/en/elasticsearch/client/java-rest/master/…
  • 可能你导入了错误的类型。你的类“PutMappingRequest”的全名是什么:org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest 或 org.elasticsearch.client.indices.PutMappingRequest?您应该使用第二个。

标签: java elasticsearch resthighlevelclient


【解决方案1】:

您需要像这样指定文档类型:

putMappingRequest.type("_doc");

并且还需要在这里指定字段的类型:

builder.field("type", type);

点赞:("type", "text")("type", "long")("type", "date") ......

你可以在here看到数据类型

【讨论】:

    【解决方案2】:

    我将发布@Mincong 的评论作为答案,因为我导入了从 github 搜索中发现的错误导入:

    你的类“PutMappingRequest”的全名是什么:org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest 或 org.elasticsearch.client.indices.PutMappingRequest?您应该使用第二个。

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 2014-07-23
      • 2021-06-10
      • 1970-01-01
      • 2016-09-27
      • 2021-09-13
      相关资源
      最近更新 更多