Chuanzhi podcast-Compass (Tang sunshine)

Source: Internet
Author: User

Today, instructor Tang teaches full-text retrieval technology. Today, he is studying compass. This framework is an encapsulation of Lucene. However, it is similar to hibernate.
However, there are many similarities. So today, the teacher gave us a review of hibernate APIs and usage, and explained compass.
In this case, we will make it easier to understand and deepen our memory. We learned from Lucene yesterday and understood full-text search. Today's compass
It is easy to learn. Using compass for full-text retrieval can greatly improve our development time.

1. Hibernate and compass are compared in general.

Hibernate:

It is an ORM framework and object-oriented operations. It provides slave objects
To tables, from tables to object services. It is a kind of encapsulation of JDBC.
Provides save ()/delete ()/update ()/get through the session.
/Load ()/hql/QBC and other methods.
Hibernate. cfg. xml configures the database link information, declares the ing file, and other configurations.
. HBM. xml
Relationship between objects and tables and between attributes and columns.
Compass:

Yes
Osem/compass. From object to document, from document to object. It is an encapsulation of Lucene API.
Pass
Compasssession. Provides save ()/delete ()/update ()/get/load ()/querystring/query, etc.
Method.
Compass. cfg. xml: configure the link information (index directory), declare the ing file, and other configurations.
. Pom. xml
Object To document, the correspondence between attribute to field.

2.
In use.

Hibernate:

Hibernate. cfg. xml

Configuration
① Programming method:
Cfg.
= New configuation ();

Cfg. setpropxxx ();

Cfg. setpropxxx ();

...


Configuration File
CFG = new
Configration (). Configure ();

Cfg. Configure ("/hibernate. cfg. xml ");

③ HBM. xml
Enable
Use the. HBM. xml file (XML file)
Annotation
Compass:

Master
Configuration
Compassconfiguration
① Programming Method
CFG = new compassconfiguration ()

Cfg. Set ("key", "value ");

...


Configuration File
Cfg. Configure ();

// Cfg. Configure ("/compass. cfg. xml ");


Ing file:
XML file
Annotation

3. API comparison

Hibernate:

Fg = new configuration (). Configure ();

SF
= Cfg. buildsessionfactory ();


Session
= SF. opensession ();

Tx = session. begintransaction ();


// Do something

// Session. Save/update/delete /..... ()


TX. Commit ();

Session. Close ();

Compass:

CFG = new compassconfiguration (). Configure ();

Compass
= Cfg. buildcompass ();


Compasssession
Session = compass. opensession ();

Compasstransaction Tx =
Session. begintransaction ();


// Do
Something

// Session. Save/update/delete /.... ()


TX. Commit ();

Session. Close ();

4. API:

Configuration
--> Compassconfiguration
Sessionfactory --> compass
Session
--> Compasssession
Transaction --> compasstransaction

5. Session method:

Warranty
Save () --> Create ()
Delete: delete () --> Delete ()
Update ()
--> Save ()
Saveorupdate () --> Save ()
GET/load () -->
GET/load ()

6. Compass is a process of use

1) Import
Jar package

2) configure the ing file. Annotations are used here.
Method.

@ Searchable
Comment before the object class.
@ Searchableid
Comment on the ID attribute of the object.
@ Searchableproperty (store
= Store. Yes, Index = index. analyzed, boost = 2.0f)
Comment on the common attributes of an object.
Store
= Store. Yes whether to store
Index = index. Whether analyzed creates an index
Boost = 2.0f plays a role in sorting.
3) load related configuration files

Private compass;


Public void Init (){


Compassconfiguration CFG = new compassconfiguration ();


Cfg. setconnection ("./indexpath/"); // connection information


Cfg. addclass (article. Class); // declares the ing information.

//
Cfg. setsetting ("key", "value"); // other configurations

Compass =
Cfg. buildcompass ();

}

4) Create an index

Compasssession session = compass. opensession ();


Compasstransaction Tx = session. begintransaction ();


Session. Create (Article );

TX. Commit ();


Session. Close ();

5) Search
Suo


List <Article> List = new arraylist <Article> ();


Compasssession session = compass. opensession ();


Compasstransaction Tx = session. begintransaction ();


Compasshits hits =
Session. Find (querystring );

For (INT I = 0; I <
Hits. Length (); I ++ ){

Article article = (Article)
Hits. Data (I );

List. Add (Article );


}

TX. Commit ();


Session. Close ();


Summary:

By writing the above instance-line program, you can learn from yesterday's
Luncene makes a clear comparison. However, the teacher said, just like learning hibernate, JDBC has not learned hibernate well.
It is impossible to learn well. Similarly, luncene does not have a good foundation, compass
It is impossible to learn well, even if it is used, it cannot be understood. It cannot reach a certain height. Therefore, it is necessary to learn more deeply.

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.