传输客户端的类型映射在spring应用程序

时间:2017-12-14 23:35:37

标签: elasticsearch spring-data-elasticsearch

我在spring应用程序中有以下配置:

@Configuration
public class config {

    @Bean
    public TransportClient elasticsearchSecuredClient() throws Exception {
        // Based on https://github.com/elastic/found-shield-example/blob/master/src/main/java/org/elasticsearch/cloud/transport/example/TransportExample.java
        Settings settings = Settings.builder().build();

        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().
                startObject().
                startObject("mappings").
                    startObject("restaurant").
                        startObject("properties").
                            startObject("amount").field("type", "long").endObject().

                        endObject().
                        field("dynamic", "strict").
                    endObject().
                endObject().
                endObject();

        TransportClient localhost = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost", 9300)));
        localhost.admin().indices().prepareCreate("expenses").addMapping("restaurant",xContentBuilder).get();
        return localhost;
    }
}

这是我的实体:

@Data
@Document(indexName = "expenses",type = "restaurant")
public class ResturantExpenseEntity {

    //@Id
    //private String id;

    @Id
    private Long amount;


}

这是错误的 stacktrace

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为' hello':不满意的依赖项的bean时出错   通过现场表达' repo&#39 ;;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名称为' resturantExpensesRepo':无法解决的bean   引用bean' elasticsearchTemplate'设置bean属性时   ' elasticsearchOperations&#39 ;;嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为' elasticsearchTemplate'的bean时出错在课堂上定义   路径资源   [组织/ springframework的/引导/自动配置/数据/ elasticsearch / ElasticsearchDataAutoConfiguration.class]:   通过方法表达不满意的依赖性   ' elasticsearchTemplate'参数0;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为' elasticsearchSecuredClient'的bean在课堂上定义   path资源[hello / config.class]:通过工厂进行Bean实例化   方法失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[org.elasticsearch.client.transport.TransportClient]:   工厂方法' elasticsearchSecuredClient'抛出异常;嵌套   异常是MapperParsingException [无法解析映射   [restaurant]:根映射定义具有不受支持的参数:   [映射:{restaurant = {dynamic = strict,   属性= {量= {类型=长}}}}]];嵌套:   MapperParsingException [根映射定义不受支持   参数:[mappings:{restaurant = {dynamic = strict,   属性= {量= {类型=长}}}}]];在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:367)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1340)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)   〜[spring-beans-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)   〜[spring-context-5.0.1.RELEASE.jar:5.0.1.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)   〜[弹簧上下文5.0.1.RELEASE.jar:5.0.1.RELEASE]

或简而言之,该错误表明: 根映射定义包含不受支持的参数:[mappings:{restaurant = {dynamic = strict,properties = {amount = {type = long}}}}]];

我在做错了什么? 我也尝试使用@Mapping注释并为具有相同映射的json文件提供路径,我得到相同的结果!

请帮忙:) 感谢

1 个答案:

答案 0 :(得分:0)

在尝试了所有内容之后,我发现问题在于将所有内容置于“映射”对象之下。

应该没有它,因此就像这样:

XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().
                    startObject().
                        startObject("restaurant").
                            startObject("properties").
                                startObject("amount").field("type", "long").endObject().
                                startObject("id").field("type", "long").endObject().
                                startObject("name").field("type", "keyword").endObject().
                            endObject().
                            field("dynamic", "strict").
                        endObject().
                    endObject();