Compass open source Java search engine framework

Source: Internet
Author: User
Document directory
  • 2.1 xml configuration
  • 4.1 hibernate Configuration
  • 4.2 spring Configuration
  • 4.3 web configuration

Compass

Author: Baron hell

1. Overview

Springside Official Website: http://www.springside.org.cn

Website: http://www.compassframework.org/

Compass is a first-class open-source Java search engine framework. It is more powerful for your application modifier and search engine semantics. Relying on the top-level Lucene search engine, compass combines popular frameworks such as Hibernate and sprin to provide your applications with the ability to synchronously change search capabilities from data models and data sources. in addition, two features are added, including thing management and fast update optimization. the goal of compass is to integrate Java applications into search engines. less encoding and easier data searching.

2. Object/search engine mapping-configure XML

Osem: Object Search Engine mapping. Through the xml configuration file, pojo's (plain old Java objects) is provided to the search engine.

2.1 xml configuration

Generally, the format is *. CPM. xml.

 

1) Package specifies the package name of the corresponding Java class,
2) class is the pojo class name. contract is the public part, and the subclass can be extends. Property attributes of the Java class.
3) Search Engine meta-data.
Note: ID is the class ID.

 <compass-core-mapping package="org.springside.bookstore.domain">  

 

<contract alias="product"> <id name="id"/> <property name="name"> <meta-data>${springside.name}</meta-data> </property> <property name="descn"> <meta-data>${springside.descn}</meta-data> </property> </contract>

<class name="Book" alias="${springside.book}" extends="product"> <property name="author"> <meta-data>${springside.author}</meta-data> </property>
<property name="publisher"> <meta-data>${springside.publisher}</meta-data> </property> </class>

</compass-core-mapping>

3. Common meta data

Define the compass configuration file (*. cfg. XML). Compass automatically replaces the value of the original data (common meta-data) Tag corresponding to osem. $ {...} is similar to ant.
It can be simply understood that alias corresponds to a table, and meta-data is column. For details, see compass reference (chapter 3. Search engine)

<compass-core-meta-data>  

<meta-data-group id="springside" displayName="SpringSide Book Meta Data">
<description>SpringSide Meta Data</description> <uri>http://compass/springside</uri>

<alias id="book" displayName="Book"> <description>Book alias</description> <uri>http://compass/springside/alias/book</uri> <name>book</name> </alias>

<meta-data id="name" displayName="Name"> <uri>http://compass/springside/name</uri> <name>descn</name> </meta-data>

<meta-data id="descn" displayName="Description"> <uri>http://compass/springside/descn</uri> <name>descn</name> </meta-data>

<meta-data id="author" displayName="Author"> <description>The author of a book</description> <uri>http://compass/springside/author</uri> <name>author</name> </meta-data>

<meta-data id="publisher" displayName="Publisher"> <description>The publisher of a book</description> <uri>http://compass/springside/publisher</uri> <name>publisher</name> </meta-data></meta-data-group>

</compass-core-meta-data> 

4. Integration with spring and hibernate: 4.1 hibernate Configuration

Configure the transparent pojo, and configure HBM. XML in hibernate for the class, which is not described here. For details, see the springside source code.

 

4.2 spring Configuration

The sessionfactory and transactionmanager in hiberante are easier to use than everyone else. I used it here (because it does not involve the issue of the draft fee? ^_^ ). compass has well encapsulated spring integration, making it easier to use. Instead of writing a line of code for compass, we can complete the search engine. next, let's talk about the concise configuration of compass in spring.

 

<beans>
<bean id="compass" class="org.compass.spring.LocalCompassBean"><property name="resourceLocations"><list><value>classpath:compass-springside.cmd.xml</value><value>classpath:compass-springside.cpm.xml</value></list>
</property><property name="compassSettings"><props><prop key="compass.engine.connection">file://${user.home}/springside/compass</prop><prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop></props></property>
<property name="transactionManager" ref="transactionManager"/></bean>

<bean id="hibernateGpsDevice" class="org.compass.spring.device.hibernate.SpringHibernate3GpsDevice">
<property name="name"> <value>hibernateDevice</value> </property> <property name="sessionFactory" ref="sessionFactory"/> </bean>

<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
<property name="compass" ref="compass"/> <property name="gpsDevices"> <list> <ref local="hibernateGpsDevice"/> </list> </property> </bean> </beans>

Resourcelocations: mount the configuration file *. cmd. XML, *. CPM. xml of compass.
Compass. Engine. connection: the path of the local file stored by the index. The search engine also queries the local file index.
Hibernategpsdevice: inject sessionfactory.

The Hibernate GPS device: Improved support with the hibernate 3 Event SystemReal time data processing ingThe data changed by hiberante is automatically reflected into the index.

4.3 web configuration
<bean id="indexBookController" class="org.compass.spring.web.mvc.CompassIndexController">
<property name="compassGps"><ref bean="compassGps"/></property>
<property name="indexView"><value>/admin/indexBook.jsp</value></property>
<property name="indexResultsView"><value>/admin/indexBook.jsp</value></property>
</bean>

<bean id="searchBookController" class="org.compass.spring.web.mvc.CompassSearchController">
<property name="compass"><ref bean="compass"/></property>
<property name="searchView"><value>/home/top.jsp</value></property>
<property name="searchResultsView"><value>/home/searchBook.jsp</value></property>
<property name="pageSize"><value>3</value></property>
</bean>

Call indexbookcontroller to rebuild the index. Call searchbookcontroller to complete the search.

Search Page:

<INPUT type="text" size="20" name="query" class="search" value=""> 

Search Results:

<Div class = "Left"> <C: If test = "$ {! Empty searchresults} ">
Time consumed: <C: Out value = "$ {searchresults. searchtime}"/> MS
<C: If test = "$ {empty searchresults. Hits}">
, No matching books found. Make sure that you have created an index in the management background.
</C: If>
<C: foreach Var = "hit" items = "$ {searchresults. Hits}">
<C: Choose>
<C: When test = "$ {hit. Alias = 'book'}">
<P> <
Href = "<C: URL value ="/bookstore. do? Action = bookdetail & ID =$ {hit. Data. ID} "/>">
<C: Out value = "$ {hit. Data. name}"/> </a> <br/>
Author: <C: Out value = "$ {hit. Data. Author}"/> & nbsp; Press: <C: Out
Value = "$ {hit. Data. Publisher}"/> </P>
</C: When>
</C: Choose>
</C: foreach>

...
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.