• 正在我的Java Spring应用程序中使用elasticsearch,用于与elasticsearch一起使用Spring JPA。
    我在Java中有一个文档和相应的类,其中的所有字段都不应编入索引(我使用Java api中的termFilter语句通过它们搜索精确匹配)
    就我而言,我必须注释每个字段

    @Field(类型= FieldType.String,索引= FieldIndex.not_analyzed)

  • 我得到这样的东西
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    @Document(indexName = "message", type = "message")
    public class Message implements Serializable {
    
        @Id
        @NotNull
        @JsonProperty("id")
        private String id;
    
        @JsonProperty("userName")
        @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
        private String userName;
    
    
        @NotNull
        @JsonProperty("topic")
        @Field(index = FieldIndex.not_analyzed, type = FieldType.String)
        private String topic;
    
        @NotNull
        @JsonProperty("address")
        @Field(index = FieldIndex.not_analyzed, type = FieldType.String)
        private String address;
    
        @NotNull
        @JsonProperty("recipient")
        @Field(index = FieldIndex.not_analyzed, type = FieldType.String)
        private String recipient;
    
    
    }
    

    是否可以在类上放置注释,以免在所有字段上重复注释?

    最佳答案

    您可以使用原始映射+ dynamic templates在没有@Field批注的情况下实现目标
    使用@Mapping注释指定json文件中映射的路径

    @Mapping(mappingPath = "/mappings.json")
    

    然后在mappings.json中定义您的映射,如下所示:
    {
      "mappings": {
        "message": {
            "dynamic_templates": [
                { "notanalyzed": {
                      "match":              "*",
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":        "string",
                          "index":       "not_analyzed"
                      }
                   }
                }
              ]
           }
       }
    }
    

    注意:我没有测试过,所以请检查错别字。

    10-08 13:56
    查看更多