Spring-Data-Elasticsearch设置:Spring无法找到配置文件?

使用Spring-Data-Elasticsearch,我试图使用elasticsearch_config.json中定义的分析器和映射。
此JSON文件位于/ src / main / resources文件夹中。

我的JAVA模型看起来像:

@Document(indexName = "test", type="Tweet") @Setting(settingPath = "/elasticsearch_config.json") public class Tweet { @Id private String idStr; /** other fields, getters and setters are omitted **/ } 

elasticsearch_config.json包含设置和映射:

 { "settings": { /* some filters */}, "mappings": { /* some types' mappings*/ } } 

我尝试使用curl,我没有问题索引/搜索。

我的问题

我想使用@Setting来配置我的映射(而不是curl),但@Setting注释似乎不起作用。 使用@Setting ,当我使用curl检查映射时,我没有得到我在elasticsearch_config.json中定义的所有映射:

 curl -X GET "localhost:9200/test/_mapping?pretty=true" 

我的猜测是Spring无法正确找到elasticsearch_config.json 。 但是我已经检查过myproject / src / main / resources是否在我项目的构建路径中…

我感谢任何帮助,谢谢!

注意 :我发现这个解决方案可能有效,但是:
1.我不明白nodeBuilder的来源
2.我可能错了,但是,只有使用@Setting才有解决方案吗?

更新:我的映射我从映射中分离了分析器。 mappings.json看起来像这样:

 { "mappings": { "Tweet": { "_id": { "path":"idStr", "properties": { "idStr": { "type":"string", "index":"not_analyzed", "store":true } } }, "numeric_detection" : true, "dynamic_templates": [ { "id_fields": { "match":"userId*", "match_mapping_type": "string", "mapping": { "type":"string", "index":"no", "store":true } } }, { "name_fields": { "match":"*Name", "match_mapping_type": "string", "mapping": { "type":"string", "index":"no", "store":true } } } ], "properties": { "text": { "type": "string", "index":"analyzed", "analyzer":"custom_analyzer", "store": true }, "createdAt": { "type": "date", "format": "yyyy-MM-dd", "index":"analyzed", "store": true }, "testVersion": { "type": "integer", "index":"not_analyzed", "store":false } } } } } 

@Setting注释应指向仅包含settings部分的文件。 如果您还想指定自定义映射,则需要使用@Mapping批注并为其指定映射文件的路径。 它是这样的:

 @Document(indexName = "test", type="Tweet") @Setting(settingPath = "/settings/settings.json") @Mapping(mappingPath = "/mappings/mappings.json") public class Tweet { @Id private String idStr; /** other fields, getters and setters are omitted **/ } 

然后你需要在myproject/src/main/resources/mappings/中的myproject/src/main/resources/settings/mappings.json中存储settings.json

这应该工作。

终于找到了为什么它不工作!!

就像Val说的那样,我将elasticsearch_config.json文件分解为settings.jsonmappings.json

我的项目/ src / main / ressources架构:

 - mappings + mappings.json - settings + settings.json 

 @Document(indexName = "test", type="SentimentTweet") @Setting(settingPath = "/settings/settings.json") @Mapping(mappingPath = "/mappings/mappings.json") 

但是,在mappings.json中 ,我应该省略字段映射并直接放置映射的内容。

代替:

 { "mappings": { "Tweet": { /* MAPPINGS CONFIGURATION ARE OMITTED */ } } } 

只写在mappings.json中:

 { "Tweet": { /* MAPPINGS CONFIGURATION ARE OMITTED */ } } 

对settings.json也应该这样做