【问题标题】:Store Key-Value structure in Elasticsearch via Spring-Data通过 Spring-Data 在 Elasticsearch 中存储键值结构
【发布时间】:2015-04-16 18:03:58
【问题描述】:

我需要有关如何使用包含地图的 Spring-Data-Elasticsearch @Document 注释以最佳方式存储文档 (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");

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

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

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

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

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

汤米·齐格勒

【问题讨论】:

    标签: java spring elasticsearch spring-data-elasticsearch


    【解决方案1】:

    您可以使用@Mapping annotation来配置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 {
    
        ...
    
    }
    

    【讨论】:

      【解决方案2】:

      你要做的就是创建一个另外的类并在那里添加additionalFieldList。

      类似的东西-

      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;
      
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-18
        • 2021-03-18
        • 1970-01-01
        • 2016-09-07
        • 2017-09-19
        • 2019-02-20
        • 2020-10-08
        • 2018-04-01
        • 1970-01-01
        相关资源
        最近更新 更多