cel*_*neu 3 java spring elasticsearch spring-data spring-data-elasticsearch
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.
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 **/
}
Run Code Online (Sandbox Code Playgroud)
elasticsearch_config.json contains both settings and mappings:
{
"settings": { /* some filters */},
"mappings": { /* some types' mappings*/ }
}
Run Code Online (Sandbox Code Playgroud)
I tried with curl, I get no problem indexing/searching.
My problem:
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"
Run Code Online (Sandbox Code Playgroud)
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...
I appreciate any help, thank you!
注意:我发现这个解决方案可能有效,但是:
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
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
cel*_*neu 10
终于发现了为什么它不起作用!
就像Val所说的,我将我的elasticsearch_config.json文件分解为settings.json和mappings.json。
我的专案/ src / main / ressources架构:
- mappings
+ mappings.json
- settings
+ settings.json
Run Code Online (Sandbox Code Playgroud)
和
- mappings
+ mappings.json
- settings
+ settings.json
Run Code Online (Sandbox Code Playgroud)
但是,在mappings.json中,我应该省略字段映射,并直接放置映射的内容。
代替:
{
"mappings": {
"Tweet": {
/* MAPPINGS CONFIGURATION ARE OMITTED */
}
}
}
Run Code Online (Sandbox Code Playgroud)
只写在mappings.json中:
{
"Tweet": {
/* MAPPINGS CONFIGURATION ARE OMITTED */
}
}
Run Code Online (Sandbox Code Playgroud)
应该对settings.json做同样的事情
该@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 **/
}
Run Code Online (Sandbox Code Playgroud)
然后,你需要存储settings.json在myproject/src/main/resources/settings/和mappings.json中myproject/src/main/resources/mappings/.
这应该工作.
| 归档时间: |
|
| 查看次数: |
4188 次 |
| 最近记录: |