简体   繁体   中英

Disable indexing fields for all fields in the document in Elasticsearch

  • am using elasticsearch in my Java Spring application, for working with elasticsearch Spring JPA is used. I have a document and corresponding class in java with all fields that should not be indexed (I search through them for exact match using termFilter statement in java api) In my case I have to annotate each field

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)

and I get something like this

@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;


}

Is there a possibility to put annotation on class in order not to duplicate it above all fields?

You can achive your goal without @Field annotations using raw mappings + dynamic templates
Specify the path to your mappings in json file using @Mapping annotation

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

Then in mappings.json define your mapping like this:

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

Note: I didn't test it, so please check for typos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM