0

我编写了一个 api,通过使用 Java High Level Rest Client 指定自定义设置和映射来创建索引。Elastic Search的版本是7.索引创建成功。api的信息在下面指定。

public IndexResponse createIndexWithCustomSettingAndMappings(String indexName, String settings, String fieldsMapping)

在此之后,我使用 localhost:9200/indexName 进行了测试

{"testing":{"aliases":{},"mappings":{"properties":{"category_title_en":{"type":"text"}}},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"1","provided_name":"testing"}}}}

在此之后,我还编写了一个 api 来将文档添加到索引中,但我没有指定任何映射,因为我想使用动态映射并希望弹性搜索来找出类型本身。

public CreateAndUpdateResponse createDocumentById(@RequestParam String indexName)

这是控制器方法。该文档基本上是一个 Java 类,我将值提供给它,并在服务层中通过将 Java POJO 转换为 String 将其作为 String 参数传递。服务方法信息如下。

public CreateAndUpdateResponse createDocumentById(String indexName, String jsonData).

这里的 jsonData 是上面提到的通过转换 POJO 准备的字符串。

在运行此 api 时,文档被插入到给定的索引中,但我没有看到已插入的字段的任何映射。弹性搜索应该使用动态映射。

{"demo":{"aliases":{},"mappings":{"properties":{"{\"index_name\":\"demo\",\"status\":\"success\",\"updated_at\":\"Wed Sep 29 14:12:54 IST 2021\",\"created_at\":\"Wed Sep 29 14:12:54 IST 2021\",\"created_at_epoch\":0}":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"settings":{"index":{"routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},"number_of_shards":"1","provided_name":"demo","creation_date":"1632904975248","number_of_replicas":"1","uuid":"1vdUxgafQDeub6GHp2C-IA","version":{"created":"7140099"}}}}}

在这个响应中,您可以看到文档被插入到 index demo 中,但我没有看到所有变量的类型。此响应中还有一个以下面给出的类型命名的字段,但此值看起来是通用的。如果弹性搜索使用动态映射,那么我们可以在哪里看到它赋予文档字段的数据类型。

{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}
4

0 回答 0