Home > Enterprise >  @CompletionField annotation doesn't create field on mapping
@CompletionField annotation doesn't create field on mapping

Time:01-21

I am trying to make autocomplete suggestion using Elasticsearch. Here is my entity:

@Document(indexName = ESUtil.INDEX_NAME)
public class Person {
    
    @Id
    private String id;
    
    @Field(type = FieldType.Text, name = "name")
    private String name;

    @Field(type = FieldType.Text, name = "surname")
    private String surname;
    
    @Field(type = FieldType.Integer, name = "age")
    private int age;

    @Field(type = FieldType.Text, name = "job")
    private String job;

    @CompletionField
    private Completion suggest;

    // getters-setters...
}

I am creating documents with this method:

    @Autowired
    private ElasticsearchRestTemplate elastic;

    public String createOrUpdateDocument(Person person) {
        IndexQuery query = new IndexQueryBuilder().withId(person.getId()).withObject(person).build();
        
        return elastic.index(query, IndexCoordinates.of(ESUtil.INDEX_NAME));
    }

And it is creating the index automatically with these config after create a document:

{
    "dummy": {
        "aliases": {},
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "age": {
                    "type": "long"
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "job": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "surname": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "dummy",
                "creation_date": "1674206424438",
                "number_of_replicas": "1",
                "uuid": "a_XWdCzxRUieyDZrioIaSg",
                "version": {
                    "created": "8060099"
                }
            }
        }
    }
}

So my question is, where is my "suggest" field with "completion" type? I am using (ver: 4.4.2), (ver: 8.6.0) and there is not any additional configuration.

Am I missing some config or should I create index by myself?

CodePudding user response:

The mapping of an index is only created by Spring Data Elasticsearch when you are using Spring Data repositories:

during bootstrapping the repository support on application startup (https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.repositories.autocreation).

So if you don't use repository access for your entities, or the index already exists, the mapping will not be written automatically.

You always can do that by your self:

elastic.indexOps(Person.class).putMapping(Person.class)
  • Related