howto:SpringRoo中solr插件的使用

 

  1. roo> solr setup

    向工程添加solr支持

  2. ~.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<Person> people = new ArrayList<Person>();        people.add(person);        indexPeople(people);    }  //请求solr服务器索引一个Person集合        public static void Person.indexPeople(Collection<Person> people) {        List<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();        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;    }    }
  3. 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:

<simple-entity-name>.<field-name>_<field-type>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.





posted @ 2012-01-19 17:02  HelloCoding  阅读(464)  评论(0编辑  收藏  举报