I'm trying to work with Spring Data Elasticsearch 4.0.1 and trying to figure out how and when the Elasticsearch mappings are being created by Spring or if they are being created by Spring.
If I have an entity like so:
@Document(indexName = "companies")
public class CompanyEntity {
@Id
private final String id;
@MultiField(
mainField = @Field(type = Text),
otherFields = {
@InnerField(suffix = "raw", type = Keyword)
}
)
private final String companyName;
@PersistenceConstructor
public CompanyEntity(String id, String companyName) {
this.id = id;
this.companyName = companyName;
}
public String getId() {
return id;
}
public String getCompanyName() {
return companyName;
}
}
I was under the impression that Spring would implicitly create the mapping for this index but I seem to be mistaken. Elasticsearch still creates the mapping for this index.
{
"companies": {
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"companyName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
Clearly from the above, the suffix in the InnerField annotation is not used and the ignore_above value is not used as well since it defaults to -1, and Elasticsearch would remove this field altogether if trying to set ignore_above to -1.
The only way I was able to get the mapping for the above annotations was to explicitly set the mapping myself.
@Autowired private ElasticsearchOperations operations;
Document mapping = operations.indexOps(CompanyEntity.class).createMapping();
operations.indexOps(CompanyEntity.class).putMapping(mapping);
Which yields the expected mapping:
{
"companies": {
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"companyName": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
Which is fine, but feels a bit odd to me as I could not find any details of this approach in the official Spring Data Elasticsearch docs. And the JavaDocs are kinda void of any details.
Is this the right approach to installing mappings into Elasticsearch from Spring Data?