如何从H2数据库导入数据到elasticsearch?

时间:2018-11-29 06:32:36

标签: java spring-boot elasticsearch h2

我正在尝试使用elasticsearch从H2数据库中搜索数据。 elasticsearch在我的项目中工作正常。我在外部添加了elasticsearch。当我使用构造函数添加数据时,它会添加到elasticsearch。但是如果尝试将H2数据库中的数据添加到elasticsearch中,则会出现此错误。
-在文件中定义名称为“ itemMigrator”的bean时出错。
-创建名称为'userJpaRepository'的bean时出错:调用init方法失败;
-没有找到属性UserAll的所有属性!

spring-boot-starter-parent 2.0.6.RELEASE

elasticsearch版本-5.6.12

我正在关注此示例https://www.hameister.org/SpringBootElasticsearch.html

http://techprimers.com/spring-data-elastic-search-example-3-using-spring-jpa/

User.java

android {
 compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_10
    targetCompatibility JavaVersion.VERSION_1_10
    }
}

SearchQueryBuilder.java

    @Entity
@Document(indexName = "user", type = "user", shards = 1)
@Table(name = "user_detail")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @NotBlank
    @Column(name = "first_name")
    private String firstName;
    @NotBlank
    @Column(name = "last_name")
    private String lastName;

ItemMigrator.java

@Component
    public class SearchQueryBuilder {

         private ElasticsearchTemplate elasticsearchTemplate;

            @Autowired
            public SearchQueryBuilder(ElasticsearchTemplate elasticsearchTemplate) {
                this.elasticsearchTemplate = elasticsearchTemplate;
            }

        public List<User> getAll(String text) {

            QueryBuilder query = QueryBuilders.boolQuery()
                    .should(
                            QueryBuilders.queryStringQuery(text)
                                    .lenient(true)
                                    .field("firstName")
                                    .field("role")
                    ).should(QueryBuilders.queryStringQuery("*" + text + "*")
                            .lenient(true)
                            .field("firstName")
                            .field("role"));

            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(query)
                    .build();

            List<User> userses = elasticsearchTemplate.queryForList(build, User.class);

            return userses;
        }
    }

UserJpaRepository.java

@Autowired
    ElasticsearchOperations operations;
    @Autowired
    UsersRepository UsersRepository;
    @Autowired
    UserJpaRepository jpaRepository;

    @Autowired
    public ItemMigrator(UserJpaRepository jpaRepository, ElasticsearchTemplate operations, UsersRepository UsersRepository) {
        this.jpaRepository = jpaRepository;
        this.operations = operations;
        this.UsersRepository = UsersRepository;
    }

    @PostConstruct
    @Transactional
    public void loadAll() {
        Iterable<User> items = jpaRepository.findAll();
        operations.putMapping(User.class);
        UsersRepository.saveAll(items);
    }

}

UsersRepository.java

    public interface UserJpaRepository extends JpaRepository<User, Long> 

ManualSearchResource.java

public interface UsersRepository extends ElasticsearchRepository<User, Long>{
        List<User> findByFirstName(String text);
        List<User> findByRole(String text);

Config.java

    @RestController
@RequestMapping("/rest/manual/search")
public class ManualSearchResource {

     @Autowired
        private SearchQueryBuilder searchQueryBuilder;

        @GetMapping(value = "/{text}")
        public List<User> getAll(@PathVariable final String text) {
            return searchQueryBuilder.getAll(text);
        }
}

这是我的堆栈跟踪https://www.dropbox.com/s/nxf2a3m961dx7a7/elastic_error.txt?dl=0

同时添加@Entity和@Document是否是同一类是正确的吗?请告诉我我在做什么错了。

0 个答案:

没有答案