【问题标题】:Disable indexing fields for all fields in the document in Elasticsearch在 Elasticsearch 中禁用文档中所有字段的索引字段
【发布时间】:2016-11-15 15:24:49
【问题描述】:
  • 我在我的 Java Spring 应用程序中使用弹性搜索,用于使用弹性搜索 Spring JPA。 我在 java 中有一个文档和相应的类,其中包含不应索引的所有字段(我使用 java api 中的 termFilter 语句搜索它们以查找完全匹配) 就我而言,我必须注释每个字段

    @Field(type = FieldType.String, index = 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;


}

是否有可能在类上添加注释以免在所有字段之上重复?

【问题讨论】:

    标签: java spring elasticsearch spring-data-jpa spring-data-elasticsearch


    【解决方案1】:

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

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

    然后在mappings.json 中定义您的映射,如下所示:

    {
      "mappings": {
        "message": {
            "dynamic_templates": [
                { "notanalyzed": {
                      "match":              "*", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":        "string",
                          "index":       "not_analyzed"
                      }
                   }
                }
              ]
           }
       }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 2016-09-14
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多