zoukankan      html  css  js  c++  java
  • 使用spring-data-solr做solr客户端

            solr的客户端基本上只有一个,那就是solrj,spring-data-solr是在solrj的基础上做的封装,使统一成spring-data的风格

       官方网站:

        http://projects.spring.io/spring-data-solr/

       使用spring-data最简单的方式就是使用spring-boot,注意多个spring-data之间不能公用,由于之前我们同样使用了spring-data-jpa 做orm,

      这里要新创建一个项目进行使用spring-data-solr作为solr服务器的客户端,然后通过分布式进行协同使用。

      首先搭建spring-boot项目,具体参考另外一遍博客。

      添加依赖:

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-solr</artifactId>
            </dependency>

      由于spring-boot是集中性的配置,所以在application.properties文件中添加以下配置:

      

    # SOLR (SolrProperties)
    spring.data.solr.host=http://localhost:8983/solr/mark
    #spring.data.solr.zkHost=
    spring.data.solr.repositories.enabled=true

        这样就算是配置完成了

      

      使用spring-data-solr与使用spring-data-jpa几乎一毛一样:

      

    public interface MarkSolrRepository extends SolrCrudRepository<SearchMark, String> {
    
        @Query("value:?0")
        List<SearchMark> findAllByValue(String markey, Pageable pageable);
    
    }

           不过有一些区别:

      1.@Query中的是solr的语法,并且参数计数是从0开始的(spring-data-jpa是从1开始的)

      2.每个映射的实体类必须有@ID主键

      

    @Entity
    public class SearchMark implements Serializable {
    
        private static final long serialVersionUID = 1229830543809682342L;
        @org.springframework.data.annotation.Id 
        private String id;// 唯一主键
    
        @Field()
        private String value;// 标签的值
    
        @Field()
        private int type;// 类型
    
        @Field("datam_id")
        private int datamId;// 数据id
    
        @Field("create_time")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08")
        private Date createTime;// 创建时间

        注意@id是@org.springframework.data.annotation.Id

      

      其他用法参考:

      http://projects.spring.io/spring-data-solr/

      http://docs.spring.io/spring-data/solr/docs/1.0.0.RC1/reference/html/

      http://www.petrikainulainen.net/programming/solr/spring-data-solr-tutorial-crud-almost/?utm_source=tuicool&utm_medium=referral

      

  • 相关阅读:
    loadrunner -27778 https连接问题
    https调试
    Session Alerts
    Pause Web Sessions
    Customize Web Sessions List
    单例模式:Java单例模式的几种写法及它们的优缺点
    Activity: launchMode 和 Intent.FLAG_ACTIVITY_CLEAR_TOP
    TextView: android:ellipsize="marquee" 跑马灯效果无效的问题
    Socket通信(1):搭建开发环境
    linux: QT安装时出现段错误segmentation fault
  • 原文地址:https://www.cnblogs.com/lic309/p/5255057.html
Copyright © 2011-2022 走看看