简体   繁体   中英

adding null_value through spring-data-elasticsearch annotation

I want to create user index like below using spring-data-elasticsearch-2.1.0. annotation. I am not able to find any annotation to add "null_value": "NULL" . This is required because our sorting order is failing.

"user": {
    "properties": {
      "firstName": {
        "type": "string"
      },
      "lastName": {
        "type": "string"
      },
      "displayName": {
        "type": "string",
        "analyzer": "word_analyzer",
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "null_value": "NULL"
          }
        }
      }
    }
  }

Domain class

 private String firstName;
 private String lastName;
 @MultiField(
         mainField = @Field(type = FieldType.String, analyzer = "word_analyzer"),
         otherFields = {
               @InnerField(suffix = "raw", type = FieldType.String, index = FieldIndex.not_analyzed)
                }
)
private String displayName;

How to add "null_value": "NULL" through spring-data-elasticsearch annotation in InnerField? I do not want to creating index mapping externally.

At now it's only possible through @Mapping annotation. Create JSON file with mapping definition:

{
  "type": "string",
  "index": "analyzed",
  "analyzer": "word_analyzer",
  "fields": {
    "raw": {
      "type": "string",
      "index": "not_analyzed",
      "null_value": "NULL"
    }
  }
}

And save it in your resources folder. In this example I save it in resources/elastic/document_display_name_mapping.json .

Annotate field with @Mapping annotation

@Mapping(mappingPath = "elastic/document_display_name_mapping.json")
private String displayName;

Referring to https://jira.spring.io/browse/DATAES-312

This is an open issue(Fixed but not merged). This can be handled by adding "missing" : "_last"/"_first" and "unmapped_type" in sorting options..

Ref:- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_ignoring_unmapped_fields

These options("missing", "unmapped_type", "mode") are not available.

Putting "null_value": "NULL" will not have correct sorting order in string field.

"null_value": "0" can satisfy sorting order for integer field.

is it possible to do something in settings itself to achive one usecase usecase -- if sort direction is ASC then "missing" : "_first" and if sort direction is DESC then "missing" : "_last" .. which can be applied on raw field.

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