0

我需要有关如何使用包含地图的 Spring-Data-Elasticsearch @Document Annotation 以最佳方式存储文档(Java POJO)的信息

@Document(indexName = "downloadclienterrors", type = "downloadclienterror")
public class DownloadClientErrorLogElasticsearch {

    @Id
    private Long id;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String host;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String shortMessage;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String fullMessage;

    @Field(type = FieldType.Date)
    private String clientTimestamp;

    private Integer level;

    private Map<String, String> additionalFieldList;

    ...
}

就像在第一类中创建的 POJO 一样,我可以通过我的存储库将它存储在弹性搜索实例中。

这就是我向其中添加数据的方式,我想要灵活地添加哪些 JSON 字段,因为这对于我的客户端软件来说是灵活的。

    additionalFieldList.put("url", "http://www.google.de");
    additionalFieldList.put("user_agent", "Browser/1.0.0 Windows");

我的问题是我还需要additionalFieldList 中标记为.not_analyzed 的字段。(fe additionalFieldList.url、additionalFieldList.user_agent)。我希望在我的 Map 上也具有与 String 上的 FieldIndex.not_analyzed 注释相同的行为,但当然仅适用于地图中的值。

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private Map<String, String> additionalFieldList;

但是当我尝试存储文档时这不起作用。我收到一个丑陋的异常。

如果有人知道一种方法,或者在 elasticsearch 中设计这样一个文档会更好,因为我在这个领域很新鲜,我很想听听一些评论。

之前的感谢和来自汉堡的灰色问候,

汤米齐格勒

4

2 回答 2

0

您可以使用@Mapping 注解来配置dynamic_templates

只需将映射文件放在类路径中并使用 @Mapping 注释您的 POJO

映射示例

JSON

{
    "downloadclienterrors": {
        "dynamic_templates": [
            {
                "additionalFieldList": {
                    "path_match": "additionalFieldList.*",
                    "mapping": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        ]

        ...

    }
}

POJO

@Mapping(mappingPath = "/downloadclienterrors.json")
@Document(indexName = "downloadclienterrors", type = "downloadclienterror")
public class DownloadClientErrorLogElasticsearch {

    ...

}
于 2015-05-10T00:17:45.803 回答
0

您需要做的是创建另一个附加类并在其中添加附加字段列表。

像这样的东西-

public class additional {

      private Map<String, String> additionalFieldList;

}

and then use this class in your pojo

@Document(indexName = "downloadclienterrors", type = "downloadclienterror")
public class DownloadClientErrorLogElasticsearch {

    @Id
    private Long id;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String host;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String shortMessage;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String fullMessage;

    @Field(type = FieldType.Date)
    private String clientTimestamp;

    private Integer level;

    @Field(type = FieldType.Nested)
    private additional additional;

    ...
}
于 2018-05-17T09:25:06.070 回答