设为首页 收藏本站
查看: 1250|回复: 0

[经验分享] howto:SpringRoo中solr插件的使用

[复制链接]

尚未签到

发表于 2015-7-17 11:51:14 | 显示全部楼层 |阅读模式





  • roo> solr setup
      向工程添加solr支持


  • ~.Person roo> solr add
      通过AOP为Person类添加支持全文索引的方法

    privileged aspect Person_Roo_SolrSearch {        @Autowired    transient SolrServer Person.solrServer;//简单查询        public static QueryResponse Person.search(String queryString) {        return search(new SolrQuery("person.solrsummary_t:" + queryString.toLowerCase()));    }//高级查询      public static QueryResponse Person.search(SolrQuery query) {        try {            QueryResponse rsp = solrServer().query(query);            return rsp;        } catch (Exception e) {            e.printStackTrace();        }        return new QueryResponse();    }  //请求solr服务器索引一个Person对象      public static void Person.indexPerson(Person person) {        List people = new ArrayList();        people.add(person);        indexPeople(people);    }  //请求solr服务器索引一个Person集合        public static void Person.indexPeople(Collection people) {        List documents = new ArrayList();        for (Person person : people) {            SolrInputDocument sid = new SolrInputDocument();            sid.addField("id", "person." + person.getId());            sid.addField("person.birthday_dt", person.getBirthDay());            sid.addField("person.id_l", person.getId());            sid.addField("person.name_s", person.getName());            //add summary field to allow searching documents for objects of this type            sid.addField("person.solrsummary_t", new StringBuilder().append(                          person.getBirthDay()).append(" ").append(                          person.getId()).append(" ").append(person.getName()));            documents.add(sid);        }        try {            SolrServer solrServer = solrServer();            solrServer.add(documents);            solrServer.commit();        } catch (Exception e) {            e.printStackTrace();        }    }    //根据Person的id 从索引中删除一个Person对象    public static void Person.deleteIndex(Person person) {        SolrServer solrServer = solrServer();        try {            solrServer.deleteById("person." + person.getId());            solrServer.commit();        } catch (Exception e) {            e.printStackTrace();        }    }    //根据JPA生命周期自动索引新创建的Person对象        @PostUpdate    @PostPersist    private void Person.postPersistOrUpdate() {        indexPerson(this);    }    //根据JPA生命周期自动从索引中删除Person对象        @PreRemove    private void Person.preRemove() {        deleteIndex(this);    }        public static final SolrServer Person.solrServer() {        SolrServer _solrServer = new Person().solrServer;        if (_solrServer == null) throw new IllegalStateException("Entity manager \                                     has not been injected (is the Spring Aspects JAR \                                    configured as an AJC/AJDT aspects library?)");        return _solrServer;    }    }

  • roo> solr all
      为工程中所有的实体添加支持全文索引的方法,效果如上。

  









15.3. The @RooSolrSearchable Annotation
  @RooSolrSearchable 注解允许你更改方法名,如果方法名留空,就不会生成相应方法。
  The @RooSolrSearchable annotation allows you to change all method names through their respective attributes in the annotation. Marking a method name with an empty String will instruct the Roo Solr add-on to not generate that method (i.e. @RooSolrSearchable(preRemoveMethod="")).
  默认所有实体属性被索引为动态字段,定义在schema.xml中
  By default all fields in a domain entity are indexed as dynamic fields (defined in the default schema.xml which Solr ships with). The default format of a field name is as follows:

._person.birthday_dt
通过以上的声明格式来确保field声明的全局唯一,也可以在实体类的属性上添加@Field注解,来自定义field名称。
  This ensures each field is uniquely mapped across your domain model by prepending the entity name followed by the field name and field type (which is used to trigger the dynamic field mapping). You can change field names by adding a @Field annotation to a field in the domain object (i.e. Person) which contains your own field (you need to provide a field definition in the Solr schema for it as well):

@Field("my:field:name:birthday")@Temporal(TemporalType.TIMESTAMP)@DateTimeFormat(style = "M-")private Date birthDay;
  索引一张数据表中的所有记录。
  To index existing DB entity tables each entity exposes a convenience method (example for Person entity):

Person.indexPeople(Person.findAllPeople());
solr服务器路径在 src/main/resources/META-INF/spring/solr.properties 中配置
  The URL of the solr server location can be changed in the project src/main/resources/META-INF/spring/solr.properties config file.
  solr插件的前端(控制器和视图),正在开发当中,ajax-solr项目计划提供一个开箱即用的插件集成(只要solr all 为所有entity添加全文索引,从前端到后端都生成好),在不太长的时间里。
  Front-end (controller and MVC/JSP views) are currently a work-in-progress. However, the following Ajax Library offers a neat front-end for those who want to take this a step further: http://github.com/evolvingweb/ajax-solr It is planned to provide a out of the box integration with the Ajax-Solr front-end through this addon in the medium term.
  



通过 ODC 发布  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87639-1-1.html 上篇帖子: Lucene vs Solr 下篇帖子: Nutch 快速入门(Nutch 2.2.1+Hbase+Solr)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表