Spring Boot + Spring Data JPA + Spring Data ElasticSearch:elastic 不返回任何结果 [英] Spring Boot + Spring Data JPA + Spring Data ElasticSearch: elastic doesn't return any results

查看:91
本文介绍了Spring Boot + Spring Data JPA + Spring Data ElasticSearch:elastic 不返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始将嵌入式 Elasticsearch 设置到我的 Spring Boot 应用程序中,其中 Spring Data JPA 是使用 Postgres 数据库设置的.

I am new to setting up embedded Elasticsearch into my Spring Boot application where Spring Data JPA is setup with Postgres database.

现在我还添加了对 Elastic Search Spring Data 存储库的支持(或者我是这么认为的).问题是 ES 搜索不返回任何内容(JSON 数组为空),而 JPA 搜索正常工作.

Now I've also added support for Elastic Search Spring Data repositories (or so I thought). The problem is that ES searches do not return anything (JSON array is empty), whilst JPA ones work correctly.

我读到人们需要一个时不时运行的索引工具,但我在 Spring Data Elastic Search 文档中找不到与此相关的任何内容.

I read that people need an index tool that runs every now and then, but I couldn't find anything related to this in the Spring Data Elastic Search documents.

我是否正确理解您需要不断为数据库中的搜索建立索引?本主题中是否提供了答案Batch通过 Spring Data ElasticSearch 将 Spring Data JPA 条目索引到 Elastic 唯一的解决方案:

Do I understand correctly that you need to index the searches from the database constantly? Is the answer provided in this topic Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch the only solution:

这是 Application 类:

@SpringBootApplication
@EnableJpaRepositories(basePackages = "eu.deniss.repository")
@ComponentScan
public class SpringDataElasticsearchDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataElasticsearchDemoApplication.class, args);
    }
}

Person Entity 类:

@Entity
@Document(indexName = "person", type = "person")
public class Person {
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    private String gender;
    private String ipAddress;

    @Id
    @org.springframework.data.annotation.Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
...
}

PersonSearchRepository 类:

public interface PersonSearchRepository extends ElasticsearchRepository<Person, Long> {
}

PersonServiceImpl 类:

@Service
public class PersonServiceImpl implements PersonService {

    private final PersonRepository personRepository;
    private final PersonSearchRepository personSearchRepository;
    private final PersonMapper personMapper;

    private static final Logger log = Logger.getLogger(PersonServiceImpl.class);

    public PersonServiceImpl(PersonRepository personRepository, PersonSearchRepository personSearchRepository, PersonMapper personMapper) {
        this.personRepository = personRepository;
        this.personSearchRepository = personSearchRepository;
        this.personMapper = personMapper;
    }

...

    @Override
    @Transactional(readOnly = true)
    public Page<PersonDTO> search(String query, Pageable pageable) {
        log.info("Request to search for a page of People with a query " + query);
        Page<Person> result = personSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(person -> personMapper.personToPersonDTO(person));
    }
}

PersonController 类:

@RestController()
@RequestMapping("/api")
public class PersonController {

    private final PersonService personService;
    private final Logger log = LoggerFactory.getLogger(PersonController.class);
    private static final String ENTITY_NAME = "person";

    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    @GetMapping("/_search/people")
    public ResponseEntity<List<PersonDTO>> searchPeople(@RequestParam String query, Pageable pageable) throws URISyntaxException {
        log.info("REST request to search for a page of Appointments for query {} ", query);
        Page<PersonDTO> page = personService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/people");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }
}

推荐答案

答案很简单.

我必须执行从数据库中查询当前实体并将它们保存到 Elastic Search 的批处理作业

I had to do a batch job of querying current entities from a database and saving them to Elastic Search by using

personSearchRepository.save(person);

这篇关于Spring Boot + Spring Data JPA + Spring Data ElasticSearch:elastic 不返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆