java - Elasticsearch Java API : Object mapping for [eventDefinitions] tried to parse field [null] as object, 但找到了具体值?

标签 java elasticsearch elasticsearch-mapping

我正在尝试为以下文档工作进行映射:

{
  "eventDatabase": "abc",
  "usageLibraryEventType": "ABC",
  "name": "Prionti",
  "namespace": "Prionti's namespace",
  "latestBuildTimestamp": 1581348323634,
  "flattenedEventProperties": [
    "User Id"
  ],
  "eventDefinitions": [
    {
      "buildInfo": {
        "baseVersion": "1",
        "branch": "master",
        "buildName": "something.com",
        "orgName": "Prionti's org",
        "repoName": "myrepo",
        "buildTimestamp": 1581348323634,
        "packageName": "myrepo",
        "packagePath": "",
        "resolvedVersion": "1.2920",
        "rootModuleName": "repo",
        "rootPackagePath": ""
      },
      "eventKey": "myEvent",
      "eventDefinition": {
        "name": "myName",
        "namespace": "myNamespace",
        "meta": {
          "description": "No description available",
          "database": "myDatabase",
          "owner": null,
          "codeOwners": [
            "Prionti Nasir"
          ],
          "imgSrc": null,
          "isPublic": null,
          "yamlSrc": {
            "packageName": "my-package",
            "packageVersion": "static-1.2920",
            "relativePath": "something.yaml"
          }
        },
        "properties": {
          "userId": {
            "type": "number",
            "options": null,
            "isOptional": false,
            "description": null
          }
        },
        "class": "interaction"
      }
    }
  ]
}

我将排除 buildInfo 和其他一些字段,因此我相应地创建了一个映射:


{
  "settings": {
    "index": {
      "number_of_replicas": "2",
      "number_of_shards": "25",
      "analysis": {
        "analyzer": {
          "autocomplete": {
            "filter": [
              "lowercase",
              "autocomplete_filter"
            ],
            "tokenizer": "standard",
            "type": "custom"
          },
          "prefixMatch": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "standard",
            "type": "custom"
          }
        },
        "filter": {
          "autocomplete_filter": {
            "min_gram": "3",
            "max_gram": "10",
            "type": "edge_ngram"
          }
        }
      }
    }
  },
  "mappings": {
    "usageLibraryEventType": {
      "dynamic": false,
      "properties": {
        "eventDatabase": {
          "properties": {
            "name": {
              "type": "string"
            }
          },
          "enabled": "false"
        },
        "eventType": {
          "type": "string",
          "analyzer": "autocomplete"
        },
        "name": {
          "type": "string",
          "analyzer": "autocomplete"
        },
        "namespace": {
          "type": "string",
          "analyzer": "autocomplete"
        },
        "latestBuildTimestamp": {
          "type": "long"
        },
        "flattenedEventProperties": {
          "type": "string"
        },
        "eventDefinitions": {
          "properties": {
            "eventKey": {
              "type": "string",
              "index": "not_analyzed"
            },
            "eventDefinition": {
              "properties": {
                "name": {
                  "type": "string",
                  "index": "no"
                },
                "namespace": {
                  "type": "string",
                  "index": "no"
                },
                "meta": {
                  "properties": {
                    "description": {
                      "type": "string",
                      "analyzer": "prefixMatch"
                    },
                    "owner": {
                      "type": "string",
                      "index": "not_analyzed",
                      "null_value" : "N/A"
                    },
                    "codeOwners": {
                      "type": "string",
                      "index": "not_analyzed",
                      "null_value" : "N/A"
                    }
                  }
                },
                "class": {
                  "type": "string",
                  "index": "not_analyzed"
                }
              }
            }
          }
        }
      }
    }
  }
}

这给了我一个 MapperParsingException。 eventDefinitions 应该是一个 json 对象列表,每个对象都包含 buildInfoeventKeyeventDefinition。如您所见,eventDefinition 还包含 json 对象。

@POST
  public UsageLibraryEventType indexEventType(UsageLibraryEventType usageLibraryEventType) {
    elasticsearchIndexerClient.addDocumentRequest(
        new IndexRequest(config.getUsageLibrarySourceTopic(), TYPE)
            .source(
                "eventDatabase", usageLibraryEventType.getEventDatabase(),
                "eventType", usageLibraryEventType.getUsageLibraryEventType(),
                "name", usageLibraryEventType.getName(),
                "namespace", usageLibraryEventType.getNamespace(),
                // fix these field names
                "latestBuildTimestamp", usageLibraryEventType.getLatestBuildTimestamp(),
                "flattenedEventProperties", usageLibraryEventType.getFlattenedEventProperties(),
                "eventDefinitions", usageLibraryEventType.getEventDefinitions()),
        config.getUsageLibrarySourceTopic())
        .join();
    return usageLibraryEventType;
  }

(eventDefinitionsEventDefinitionWithBuildInfo 的列表,每个 EventDefinitionWithBuildInfo 包含 buildInfoeventKeyeventDefinitionEventDefinition 还包含一些字段和一个名为 meta 的对象。虽然我已在映射中映射了所有这些内容,但我没有显式地将每个字段的值移交给树中的最后一个分支。当然,我无法分别输入每个 eventDefinitionWithBuildInfo 中的每个字段,然后分别输入 eventDefinition 中的每个字段,因此我必须为其提供列表,因此它不会一直映射到最后一个单元。我该怎么办?我应该定义名为 EventDefinitionWithBuildInfoEventDefinition 的新类型吗?

最佳答案

@IanGabes 是一位救世主。我整个周末都在哭泣,并试图让这一切顺利进行。但我的对象是如此嵌套、如此分层,我期望 ES 能够自行检测内部字段。我向很多人展示了这个,但他们一无所知。然后,当我绝望地看着瑞克和莫蒂,芯片从我嘴里掉下来时,@IanGabes 像天使一样过来,要求我简单地使用 Jackson 将我的对象转换为 Json,并且它不会像我这样做的方式反序列化到最后一个单元。谢谢。你摇滚,我烂。

关于java - Elasticsearch Java API : Object mapping for [eventDefinitions] tried to parse field [null] as object, 但找到了具体值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60358254/

相关文章:

for 循环内的 Java If 语句,返回多个 int 值。

java - ZK 我们可以保存 Windows 组件状态吗?

node.js - NodeJS 使用 Elasticsearch/Mongoosastic 不返回任何结果

elasticsearch - 有什么方法可以在文档中添加该字段,但对_source隐藏该字段,还应该对文档进行分析和搜索

elasticsearch - Elasticsearch创建映射问题

java - 更改 apache Camel 的 http 响应和正文响应

java - IE9意外重启

c# - Elasticsearch NEST向现有索引添加属性

elasticsearch - Elasticsearch 中的子字段聚合分组

elasticsearch - 为存在索引启用_size