`
dragonzhu
  • 浏览: 14294 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Compass: Integrate Search into your apps

阅读更多
www.infoq.com/articles/compass-search-tutorial
终于把这面的文章大体翻译完毕,其中的几句不知如何翻译(特别是marshalling and unmarshalling ,知道可以告诉我,先谢了)
compass支持的映射是OSEM (Object/Search Engine Mapping)。它允许把对象域模型实例映射成搜索引擎

。下面是Author类的一个例子,关于使用了annotations的OSEM定义。
@Searchable
 public class Author {

    @SearchableId
   private Long id;

    @SearchableComponent
   private String Name;

    @SearchableReference
   private List books;

    @SearchableProperty(format = "yyyy-MM-dd")
   private Date birthdate;
 }

  // ...

 @Searchable
 public class Name {

    @SearchableProperty
   private String firstName;

    @SearchableProperty
   private String lastName;
 }
OSEM supports marshalling and unmarshalling an object hierarchy into a Resource. When saving the Author object, Compass will marshall it into a Resource, with the Name class marshalled into the same Resource that represents the Author (thanks to the component mapping), 和 作者的清单中每本书的一个引用(存储在其他 Resources). 结果集 resource 将被保存和索引进搜索引擎。
Compass位对象域模型实例映射成搜索引擎提供了一种非常灵活的机制,上面的例子仅仅是一个比较简单的例子。OSEM allows to specify custom converters, multiple meta-data (maps to Resource Property) per class property, analyzers, 'all' field participation, and many more.
下面是一个AUTHOR类如何被使用的:
// ...
 Author author = new Author(1, new Name("jack", "london"), new Date());
 session.save(author);
 // ...
 author = (Author) session.load(Author.class, 1);

在compass最后一种支持的搜索引擎映射是XSEM (Xml/Search Engine Mapping).这种映射允许把xml数据映射成为搜索引擎,它是建立在xpath的基础上的。The XSEM process goes through the same marshalling and unmarshalling process from and to Resources. Compass引入XmlObject (一种XML包装对象),它有不同的实现形式(dom4j, W3C Document),都可以实现xpath表达式赋值。我看一看下面的XML数据结构:
<xml-fragment>
    <author id="1">
      <firstName>Jack</firstName>
      <lastName>London</lastName>
    </author>
  </xml-fragment>
下面就是有可能的XSEM定义
<compass-core-mapping>
    <xml-object alias="author" xpath="/xml-fragment/author">
      <xml-id name="id" xpath="@id" />
      <xml-property xpath="firstName" />
      <xml-property xpath="lastName" />
      <xml-content name="content" />
    </xml-object>
  </compass-core-mapping>
使用Xpath表达式把XML数据结构映射成搜索引擎。在搜索引擎中使用xml-content映射,数据可以存储XML结构,所以它可以在loading/searching 数据时使用。在JSE 5中Compass支持几种XML dom库,dom4j (SAX and XPP) ,用户自定义也很容易。下面是一个它如何被使用的例子:
Reader reader = // construct an xml reader over raw xml content
 AliasedXmlObject xmlObj = RawAliasedXmlObject("author", reader);
 session.save(xmlObj);
 // ...
 Resource resource = session.loadResource("author", 1);
 // since we have xml-content, we can do the following as well
 XmlObject xmlObj = session.load("author", 1);
  Compass Gps是目标在于Compass和不同的数据源集成的模块。在Compass最通用的数据源集成是和不同的ORM(对象关系映射)工具集成。Compass支持JPA, Hibernate, OJB, JDO 和iBatis。
我们以Hibernate为例子,Compass引入了两种主要的操作:Indexing and Mirroring。使用Hibernate映射和Compass映射,Indexing 可以自动的索引数据库内容。拥有这两种映射的对象可以自动的使用Hibernate得到数据,保存到搜索引擎中。通过注册Hibernate的事件监听器以及Hibernatede API,Mirroring自动的镜像到搜索引擎。通过Hibernate API可以使索引随着数据的改变实时更新。下面是一个如何使用Compass Gps Hibernate集成的例子:
SessionFactory sessionFactory = // Hibernate Session Factory
 Compass compass = // set up a Compass instance
 CompassGps gps = new SingleCompassGps(compass);
 CompassGpsDevice device = new Hibernate3GpsDevice("hibernate", sessionFactory);
 gps.addDevice(device);
 // start the gps, mirroring any changes made through Hibernate API
 // to be mirrored to the search engine
 gps.start();

  // ....

  // this will cause the database to be indexed
 gps.index();
  // this will cause Hibernate to store the author in the database
  // and also index the author object through Compass
 hibernateSess.save(new Author(1, new Name("jack", "london"), new Date()));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics