我正在从 Java 对象创建弹性索引。
public class JeuDeDonnees {
private String titre;
private String slug;
private String acronyme;
private String url;
private OrganisationId organisationId;
private String organisation;
private String description;
private String frequence;
[... Getters and setters ...]
}
以下单元测试成功完成。
它的方法运行
co.elastic.clients.elasticsearch.ElasticsearchClient
的 8.11.3
版本,并在插入时为对象定义适当的索引。
之后,它会列出在映射中设置的字段。
void insertionCatalogue() {
assertTrue(this.catalogDatagouvElastic.createIndex(), "L'index cible n'a pas été créé");
// Il faut qu'une entrée ait été placée pour que le mappings soit constitué.
List<JeuDeDonnees> jeux = this.jeuxDeDonneesDataset.catalogueDataset().limit(100).collectAsList();
JeuxDeDonnees jeuxDeDonnees = new JeuxDeDonnees(jeux);
assertDoesNotThrow(() -> this.catalogDatagouvElastic.insert(jeuxDeDonnees), "L'insertion de jeux de données du catalogue dans Elastic a échoué");
// Relire puis modifier l'index
TypeMapping mappings = this.catalogDatagouvElastic.getIndex().mappings();
Set<String> champsEnFrancais = Set.of("titre", "description", "organisation");
Map<String, Property> properties = mappings.properties();
properties.forEach((nomChamp, property) -> {
if (champsEnFrancais.contains(nomChamp)) {
LOGGER.info("{}: {}", nomChamp, property.toString());
}
});
}
LOGGER.info(...)
列出了我想要更改的映射:
test : titre: Property: {"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
test : description: Property: {"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
test : organisation: Property: {"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
我想在其属性中添加一个
"analyzer": "french"
条目,"titre": {
"type": "text",
"fields": {
keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"analyzer": "french"
}
但是 co.elastic.clients.elasticsearch._types.mapping.Property 类
描述了我的每个Java对象字段成员
似乎没有办法添加
analyzer
声明。
如何添加?
你好👋🏼
您不能对属性本身执行此操作,但需要在创建索引时定义映射。 这可以通过代码来完成,也可以使用 API 或直接从 Kibana 来完成。
直接的代码示例如下:
client.indices().create(cir -> cir.index("create-mapping"));
client.indices().putMapping(pmr -> pmr.index("create-mapping").properties("foo", p -> p.text(tp -> tp)));
请注意,某些框架(例如 hibernate search 或 spring data)正在提供注释,并可能会自动为您创建映射。