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

Spring-Data-Elasticsearch settings: Spring can't find config file?

使用 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,没问题 indexing/searching。

我的问题:

我想使用 @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 在我项目的构建路径中...

感谢任何帮助,谢谢!

备注: 我找到了 this 可能有效的解决方案,但是:
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/settings/中存储settings.json,在myproject/src/main/resources/mappings/中存储mappings.json

应该可以。

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

正如 Val 所说,我将 elasticsearch_config.json 文件分解为 settings.json mappings.json.

我的 project/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并直接放置映射的内容。

代替:

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

只写入 mappings.json:

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

settings.json

也应该这样做