java - 在 Elasticsearch 中禁用文档中所有字段的索引字段

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

  • 我在我的Java Spring应用程序中使用elasticsearch,为了使用elasticsearch,使用了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;


}

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

最佳答案

您可以使用原始映射 + 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"
                  }
               }
            }
          ]
       }
   }
}

注意:我没有测试过,所以请检查是否有拼写错误。

关于java - 在 Elasticsearch 中禁用文档中所有字段的索引字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40613586/

相关文章:

java - 线程生命周期如何在线程池中工作?

java - 测试时在 spring boot 中禁用 Redis AutoConfig

Spring 、JUnit 和 EasyMock : EntityNotFoundException

java - Spring Data ElasticSearch TransportClient Java 配置

php - PHP-Elasticsearch不返回所有结果,例如Kibana

java - Web 服务的身份验证

java - 哪个布局管理器可以在 Java 中进行此布局?

java - 有什么办法可以阻止 java 破坏对象

java - Spring MVC : Get Unnamed Request Parameter

ruby - 将 Logstash 中的时间戳时区转换为输出索引名称