Spring-Data-Elasticsearch 设置:Spring 找不到配置文件? [英] Spring-Data-Elasticsearch settings: Spring can't find config file?
问题描述
通过 Spring-Data-Elasticsearch,我尝试使用 elasticsearch_config.json 中定义的分析器和映射.
此 JSON 文件位于 /src/main/resources 文件夹中.
With Spring-Data-Elasticsearch, I am trying to use analyzers and mappings defined in elasticsearch_config.json.
This JSON file is in /src/main/resources folder.
我的 JAVA 模型看起来像:
My JAVA model looks like:
@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,我没有遇到索引/搜索问题.
I tried with curl, I get no problem indexing/searching.
我的问题:
我想使用 @Setting 来配置我的映射(不是 curl),但是 @Setting 注释似乎不起作用.使用 @Setting,当我使用 curl 检查我的映射时,我没有得到我在 elasticsearch_config.json 中定义的所有映射:
I want to use @Setting to configure my mappings (not curl), but @Setting annotation doesn't seem to work. With @Setting, when I use curl to check my mapping, I don't get all the mappings I defined in elasticsearch_config.json :
curl -X GET "localhost:9200/test/_mapping?pretty=true"
我的猜测是 Spring 无法正确找到 elasticsearch_config.json.但是我已经检查过 myproject/src/main/resources 是否在我的项目的构建路径中...
My guess is that Spring can't find the elasticsearch_config.json properly. However I have already checked that myproject/src/main/resources is in my project's build path...
感谢您的帮助,谢谢!
注意事项:我发现这个解决方案可能有效,但是:
1.我不明白nodeBuilder从哪里来
2. 我可能是错的,但不是只有使用 @Setting 的解决方案吗?
Notes:
I have found this solution that may work, but:
1. I don't understand where nodeBuilder comes from
2. I may be wrong but, isn't there a solution only using @Setting?
更新:我的映射我将分析器与映射分开.mappings.json 看起来像这样:
UPDATES: My mappings I separeted analyzers from mappings. mappings.json looks like this:
{
"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
}
}
}
}
}
推荐答案
终于找到不工作的原因了!!
Finally found out why it was not working!!
正如 Val 所说,我将我的 elasticsearch_config.json 文件分解为 settings.json 和 mappings.json.
Like Val said, I decomposed my elasticsearch_config.json file into settings.json and mappings.json.
我的项目/src/main/ressources 架构:
My project/src/main/ressources architecture:
- mappings
+ mappings.json
- settings
+ settings.json
还有
@Document(indexName = "test", type="SentimentTweet")
@Setting(settingPath = "/settings/settings.json")
@Mapping(mappingPath = "/mappings/mappings.json")
但是,在 mappings.json 中,我应该省略字段 mappings 并直接放置映射的内容.
However, in mappings.json, I should omit the field mappings and DIRECTLY put the content of the mapping.
代替:
{
"mappings": {
"Tweet": {
/* MAPPINGS CONFIGURATION ARE OMITTED */
}
}
}
只写入 mappings.json:
Only writes in mappings.json:
{
"Tweet": {
/* MAPPINGS CONFIGURATION ARE OMITTED */
}
}
settings.json 也应该这样做
The same should be done for settings.json
这篇关于Spring-Data-Elasticsearch 设置:Spring 找不到配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!