本文介绍了使用Spring数据Mongo和Spring数据Elasticearch时如何建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在项目中使用mongo和ElasticSearch,我也喜欢采用Spring Data Mongo和Spring Data ElasticSearch,但是两者都有其存储库和模型规范,如何一起使用?

I want to use mongo and ElasticSearch in my projects, and I also like adopt Spring Data Mongo and Spring Data ElasticSearch, but both has their Repository and model specs, how to use them together?

有一些选择:

  1. 对Mongo和ElasticSearch使用相同的模型类吗?

  1. Use the same model class for Mongo and ElasticSearch?

@Document//from Spring Data Mongo
@Document// from Spring Data ElasticSearch
public class Book{
    @Id//Spring Data Commons
    private String id;
}

但是Spring Data Mongo和Spring Data ElasticSearch中存在一些不匹配,例如Geo字段类型.

But there are some mismatch in Spring Data Mongo and Spring Data ElasticSearch, such as Geo field type.

为Mongo和ElasticSearch定义不同的模型,并从Mongo模型复制数据状态​​并在创建新模型时创建索引.

Define different model for Mongo and ElasticSearch, and copy data state from Mongo model and create index when create a new model.

这里有什么建议吗?

我想在项目中使用选项2.

  1. 正常保存mongo文档.
  2. 通过JMS/AMQP/Reactor触发事件以将数据同步到Elasticsearch,并为ElasticSearch文档中的每个字段选择索引策略.
  3. 所有搜索操作均基于ElasticSearch.

已于2016年5月15日更新

我创建了一个示例来演示这种方法.

I have created a sample to demo this approach.

示例代码

我使用Spring内置的ApplicationEvent演示了这种方法.

I used Spring builtin ApplicationEvent to demo this approach.

  1. 事件发布者方Mongo保存了帖子并发布了一个事件.

  1. The event publisher side, Mongo saved the post and publish an event.

@Component
public class Publisher implements ApplicationEventPublisherAware {

    private static final Logger LOG = LoggerFactory.getLogger(Publisher.class);

    @Autowired
    PostRepository repository;

    private ApplicationEventPublisher publisher;

    public Publisher() {
    }


    public void savePost(Post post) {
        Post saved = repository.save(post);
        this.publisher.publishEvent(saved);

        LOG.debug("saved post data in mongo@" + saved);
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

}

  • 事件接收器端接收数据并将其同步到ElasticSearch存储中.

  • The event receiver side, received the data and sync it into ElasticSearch storage.

        @Component
    public class Receiver {
    
        private static final Logger LOG = LoggerFactory.getLogger(Receiver.class);
    
        @Autowired
        ESPostRepository repository;
    
        @EventListener
        public void onPostSaved(Post savedPost) {
            LOG.debug("=================received post data============== @\r\n"+ savedPost);
    
            ESPost doc=new ESPost();
            doc.setId("1");
            doc.setTitle(savedPost.getTitle());
            doc.setContent(savedPost.getContent());
            repository.save(doc);
        }
    
    }
    

  • 在生产环境中,发布者和接收者可以通过JMA/AMQP而不是内置的ApplicationEvent放置在不同的应用程序中.

    In production environment, the publisher and receiver can be placed in different applications via JMA/AMQP instead of builtin ApplicationEvent.

    mongo用作主存储,ElasticSearch用作索引/搜索服务器.

    The mongo is used as main storage and ElasticSearch as index/search server.

    推荐答案

    您能为不同的文档注释使用完全限定的域名吗?

    can you just use the fully qualified domain name for the different Document annotations?

    这就是我们在这里要做的事情.

    thats what we are trying todo here.

      @Document(collection = "SPECTRUM")
      @org.springframework.data.elasticsearch.annotations.Document(indexName = "spectrum", `type` = "spectra", shards = 1, replicas = 0, refreshInterval = "-1")
      case class Spectrum(
                      ...
      )
    

    这篇关于使用Spring数据Mongo和Spring数据Elasticearch时如何建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-27 02:47