简体   繁体   English

@CompletionField 注释不会在映射上创建字段

[英]@CompletionField annotation doesn't create field on mapping

I am trying to make autocomplete suggestion using Elasticsearch. Here is my entity:我正在尝试使用 Elasticsearch 提出自动完成建议。这是我的实体:

@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), (版本:4.4.2), (ver: 8.6.0) and there is not any additional configuration. (ver: 8.6.0) 并且没有任何额外的配置。

Am I missing some config or should I create index by myself?我是否缺少某些配置或者我应该自己创建索引?

The mapping of an index is only created by Spring Data Elasticsearch when you are using Spring Data repositories:当您使用 Spring 数据存储库时,索引的映射仅由 Spring 数据 Elasticsearch 创建:

during bootstrapping the repository support on application startup ( https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.repositories.autocreation ).在引导应用程序启动时存储库支持期间( 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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