【问题标题】:Spring Data REST does not seem to be working with elasticsearchSpring Data REST 似乎不适用于 elasticsearch
【发布时间】:2015-09-24 01:55:47
【问题描述】:

我正在尝试使用 Spring Data REST 进行弹性搜索。 POST 的内置 REST 控制器似乎无法正常工作:当我尝试发布文档时出现错误。这个问题很容易重现: 我创建了一个简单的实体:

@Document(indexName = "user", type = "user", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {

    @Id
    private String id;

    @Field(type = FieldType.String, store = true)
    private String firstName;

     @Field(type = FieldType.String, store = true)
    private String lastName;
    // getters and setters are skipped
}

存储库:

public interface UserRepository extends ElasticsearchRepository<User, String> {
}

当我尝试获取所有用户时,我得到了响应:

 curl -X GET "http://localhost:9000/users"
 {
  "_links" : {
   "self" : {
   "href" : "http://localhost:9000/users{?page,size,sort}",
   "templated" : true
},
"search" : {
  "href" : "http://localhost:9000/users/search"
}
},
 "page" : {
 "size" : 20,
 "totalElements" : 0,
 "totalPages" : 0,
 "number" : 0
 }
}

但是当我尝试添加用户时:

curl -i -X POST -H "Content-Type:application/json" http://localhost:9000/users -d '{"id":"4e9e62aa-7312-42ed-b8e4-24332d7973cd","firstName":"test","lastName":"test"}'

我收到一个错误:

{"cause":null,"message":"PersistentEntity must not be null!"}

似乎为此问题打开了一张没有任何 cmets 的 Jira 票: Jira Issue

我想知道是否可以避免为 Spring Data Elasticsearch 编写 CRUD REST 控制器?

【问题讨论】:

    标签: elasticsearch spring-data spring-data-rest spring-data-elasticsearch


    【解决方案1】:

    解决方法是添加

    @EnableElasticsearchRepositories(repositoryFactoryBeanClass = RestElasticsearchRepositoryFactoryBean.class)
    

    RestElasticsearchRepositoryFactoryBean 定义为的应用程序类的注释

    @SuppressWarnings("rawtypes")
    public  class RestElasticsearchRepositoryFactoryBean
        extends org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean {
        @SuppressWarnings("unchecked")
        @Override
        public void afterPropertiesSet() {
            setMappingContext(new org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext());
            super.afterPropertiesSet();
        }
    }
    

    【讨论】:

    • 可以使用的 spring-data-elasticsearch 的最高版本是多少?
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 2015-06-28
    • 2018-12-10
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2017-12-21
    • 2016-01-20
    相关资源
    最近更新 更多