代码之家  ›  专栏  ›  技术社区  ›  Fish-Guts

将更改直接填充到弹性搜索

  •  0
  • Fish-Guts  · 技术社区  · 6 年前

    我正在编写一个小的演示应用程序,并使用jhipster生成堆栈。由于我对web开发还很陌生,我遇到了一些问题,特别是弹性搜索。

    每当我在应用程序中更改实体数据时,更改的数据不会反映在使用弹性搜索执行的搜索中。

    我的理解是,如果实体更改保存在相应的搜索存储库中,那么它应该反映在es索引中。我搞错了吗?

    一个活生生的例子:

    @Override
    public Client save(Client client) {
        log.debug("Request to save Client : {}", client);
        Client result = clientRepository.save(client);
        clientSearchRepository.save(result);
        return result;
    }
    

    这把更新的信息存储在我的mysql数据库中,到目前为止还不错。但是,假设我编辑了用户的出生日期并保存了它。每当我搜索包含该客户机的内容时,“旧”出生日期仍会显示,直到我对es存储库执行重新索引。

    是否可以使用某种实体监听器(或其他任何东西)直接填充更改的数据?

    谢谢你的指点。

    1 回复  |  直到 6 年前
        1
  •  0
  •   D00de    6 年前

    您应该在更新方法上使用相同的searchrepository。上面的代码看起来不错,如果您的elasticsearch配置正确,那么应该在创建/更新实体时更新索引。

    使用jhipster实体生成器并启用elasticsearch,它应该是开箱即用的。

    您将在ClientResource中获得这样的内容,用于创建新客户端:

    @PostMapping("/clients")
    @Timed
    public ResponseEntity<Client> createClient(@Valid @RequestBody Client client) throws URISyntaxException {
    
        Client result = clientRepository.save(client);
        clientSearchRepository.save(result);
    

    像这样编辑你的客户:

        @PutMapping("/clients")
        @Timed
        public ResponseEntity<Client> updateClient(@Valid @RequestBody Client client) throws URISyntaxException {
    
        Client result = clientRepository.save(client);
        clientSearchRepository.save(result);
    

    尝试生成新的空jhipster项目并包含elasticsearch广告,您将看到它的工作原理与您最初的设想一样。