Use compass to quickly add a search function to your website

Source: Internet
Author: User

Use compass to quickly add a search function to your website

If your website architecture uses spring + hibernate. With the popular open-source search engine framework compass, you can quickly add powerful search functions to your website. From hundreds of thousands of data records, you can search for the desired data in just a few milliseconds. Now I only want to quickly build the search function into your system. As for the configuration details, the point may be reached. Hope you can forgive me. Let's get started. Step 1: Create an index for the table you want to search. We did not directly create indexes through tables in relational databases. Instead, index the database table indirectly through the intermediate bridge hibernate. We know that hibernate mainly maps objects to database tables. We create indexes based on objects. Assume that our database table has a table named video. Fields, ID (primary key, unique ID, increment), c_name (Chinese name), e_name (English name), alias (alias), genre (type), Director (Director), create_time, update_time .... this table should correspond to an object, also known as pojo. video. javapackage COM. jack. videopublic class video {private integer ID;
Private string cname;
Private string ename;
Private string alias;
Private string Genre;
Pivate string director;
Private date createtime; private date updatetime;
Public video () {} public integer GETID (){
Return this. ID;
} Public void setid (integer ID ){
This. ID = ID;
} Public String getcname (){
Return this. cname;
} Public void setcname (string cname ){
This. cname = cname;
} Public String getename (){
Return this. ename;
} Public void setename (string ename ){
This. ename = ename;
} Public String getalias (){
Return this. Alias;
} Public void setalias (string alias ){
This. Alias = alias;
} Public String getgenre (){
Return this. Genre
} Public void setgenre (string genre ){
This. Genre = Genre;
} Public String getdirector (){
Return this. Director;
} Public void setdirector (string Director ){
This. Director = Director;
} Public date getcreatetime (){
Return this. ceatetime;
} Public void setcreatetime (date ceatetime ){
This. ceatetime = ceatetime;
} Public date getupdatetime (){
Return this. updatetime;
} Public void setupdatetime (date updatetime ){
This. updatetime = updatetime;
}
} There is nothing to talk about in the simple pojo above. You will be familiar with it at a glance. Next we will create an index for this pojo, that is, a CPM file. The specific configuration is as follows: video. CPM. xml <! Doctype Compass-core-mapping public
"-// Compass/compass core mapping DTD 1.0 // en"
Http://www.opensymphony.com/compass/dtd/compass-core-mapping.dtd>
<Compass-core-mapping package = "com. Jack. Video" <class name = "video" alias = "video"
Index = "video-index">
<ID name = "ID"/>
<Property name = "cname">
<Meta-data index = "tokenized"> cname </meta-data>
</Property>
<Property name = "ename">
<Meta-data index = "tokenized"> ename </meta-data>
</Property>
<Property name = "alias">
<Meta-data index = "tokenized"> alias </meta-data>
</Property>
<Property name = "genre">
<Meta-data index = "un_tokenized"> genre </meta-data>
</Property>
<Property name = "director">
<Meta-data index = "tokenized"> Director </meta-data>
</Property>
<Property name = "createtime">
<Meta-data index = "no"> truecreatetime </meta-data>
</Property>
<Property name = "updatetime">
<Meta-data index = "no"> updatetime </meta-data>
</Property>
</Class>
</Compass-core-mapping> the package attribute of this file is the package that guides pojo (video. Java. <Class name = "video" alias = "video"
Index = "video-index">. Name is the class name. Alias is an alias for this class. Index refers to the name of the folder where the index is generated. You will understand later. <Property name = "cname">
<Meta-data index = "tokenized"> cname </meta-data>
</Property> the name in the tag is the property names in video. java. <Meta-data index = "tokenized"> This attribute specifies the index policy for this field. The index has three values. The default value is tokenized, which indicates that the index is segmented first, no indicates neither word segmentation nor indexing. un_tokenized indicates no word segmentation but indexing. There is also an attribute store that indicates whether to store the field. It has two values: Yes/No. The default value is yes. It indicates that the field is stored. This is a commonly used attribute. There are many other attributes, but they are not commonly used. These two are enough. Other attributes are not configured here. They are generally configured in another configuration file. Each CPM file can be used. Step 2: configure the compass file. <! -- Compass master configuration -->
<Bean id = "Compass" class = "org. Compass. Spring. localcompassbean">
<Property name = "compasssettings">
<Props>
<Prop key = "compass. Engine. Connection"> E:/video </prop>
<Prop key = "compass. transaction. Factory">
Org. Compass. Spring. transaction. springsynctransactionfactory
</Prop>

<Prop key = "compass. Engine. analyzer. mmanalyzer. customanalyzer"> jeasy. analysis. mmanalyzer </prop>
<Prop
Key = "compass. Engine. highlighter. Default. formatter. Simple. pre">
<! [CDATA [<font color = "red"> <B>]>
</Prop>
<Prop
Key = "compass. Engine. highlighter. Default. formatter. Simple. Post">
<! [CDATA [</B> </font>]>
</Prop>
</Props>
</Property>
<Property name = "transactionmanager">
<Ref local = "transactionmanager"/>
</Property>
<Property name = "resourcelocations">
<List>
<Value>
Classpath: COM/Jack/Video/video. CPM. xml
</Value>
</List>
</Property>
</Bean>
<! -- Synchronously update indexes -->
<Bean id = "hibernategps"
Class = "org. Compass. GPS. impl. singlecompassgps" init-method = "start"
Destroy-method = "stop">
<Property name = "Compass">
<Ref bean = "Compass"/>
</Property>
<Property name = "gpsdevices">
<List>
<Bean
Class = "org. Compass. Spring. device. springsynctransactiongpsdevicewrapper">
<Property name = "gpsdevice" ref = "hibernategpsdevice"/>
</Bean>
</List>
</Property>
</Bean>
<! -- Hibernate driver links: compass and hibernate -->
<Bean id = "hibernategpsdevice"
Class = "org. Compass. Spring. device. hibernate. Dep. springhibernate+psdevice">
<Property name = "name">
<Value> hibernatedevice </value>
</Property>
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean> I have not configured the file compass. cfg. xml here. I configured all the configurations in it in the above file.

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.