Nhibbench. Search full-text index based on Lucene. net

Source: Internet
Author: User

Nhib.pdf. search is a non-released project under nhiberante contrilb, also from hibernate. the search is transplanted to nhib.pdf and Lucene.. NET is integrated, and the orm persistence object is stored in the database, Lucene. NET provides index and query support.
The actual use of nhibbench. Search is as follows:
Because this project has not yet been released, many of its features have been changing, so now it is only subject to the version I downloaded. If you want to try this project, please download the followingCodeWhich includes the self-compiled nhibbench. Search and the latest release e.net-related DLL.

1. Configuration
1. Add the configuration information of nhibbench. search to the location where your nhibbench configuration information is modified.

 <  Configsections  > 
< Section Name = "Hibernate-configuration" Type = "Nhibler. cfg. configurationsectionhandler, Nhibernate" />
< Section Name = "Maid" Type = "Nhib.pdf. Search. cfg. configurationsectionhandler, nhib.pdf. Search"
Requirepermission = "False" />
< Sectiongroup Name = "System. Web. Extensions" Type = "System. Web. configuration. systemwebextensionssectiongroup,
System. Web. Extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" >
.....
</ Configsections >

Note that in versions earlier than nhibbench. Search, the configuration information of nhibbench. Search was put together with that of nhibbench. Next, let's look at the specific configuration blocks:

  < Hibernate - Configuration   Xmlns = "Urn: nhibernate-configuration-2.2"  > 
< Bytecode - Provider Type = "LCG" />
< Reflection - Optimizer Use = "True" />
< Session - Factory Name = "Nhibernatesearch. Demo" >
< Property Name = "Connection. provider" > Nhib.pdf. Connection. driverconnectionprovider, nhib.pdf</ Property >
< Property Name = "Connection. connection_string" >
Data Source = | datadirectory | demo. db3; version = 3; compress = false; synchronous = off;
</ Property >
< Property Name = "Show_ SQL" > True </ Property >
< Property Name = "Dialect" > Nhib.pdf. dialect. sqlitedialect </ Property >
< Property Name = "Connection. driver_class" > Nhib.pdf. Driver. sqlite20driver </ Property >
< Property Name = "Prepare_ SQL" > True </ Property >
< Property Name = "Cache. provider_class" > Nhib.pdf. cache. hashtablecacheprovider, nhib.pdf </ Property >
< Property Name = "Cache. use_query_cache" > True </ Property >
< Mapping Assembly = "Nhibernatesearch. Demo. Model" />
</ Session -Factory >
</ Hibernate -Configuration >
< Healthcare - Configuration Xmlns = 'Urn: nhs-configuration-1.0' >
< Search - Factory Sessionfactoryname = "Nhibernatesearch. Demo" >
< Property Name ='Hibernate. Search. Default. directory_provider' > Nhibbench. Search. Store. fsdirectoryprovider,
Nhibbench. Search </ Property >
< Property Name = 'Hibernate. Search. Default. indexbase' > ~ /Index </ Property >
< Property Name = 'Hibernate. Search. Default. indexbase. create' > True </ Property >
</ Search -Factory >
</ Healthcare -Configuration >

The above is the configuration information of nhib.pdf.ProgramThe SQLite database is used, so this demo is also a case of SQLite + nhib.pdf, and pay attention to the SQLite connection string data source = | datadirectory | demo. db3, in this case, you only need to put the data file under app_data to run, instead of entering the absolute path of the database file.
The following is the configuration of nhib.pdf. Search. We use full-text retrieval based on the file directory, and the index file is placed under the index folder under the root directory.
In addition, we need to set the event listening for Nhibernate (if we do not set it, we need to call it manually to enable Nhibernate to process the full-text index at the same time when the object is persisted ), currently, this event configuration does not support the following configuration directly in nhib.pdf:

  
< Listener Class = 'Nhibbench. Search. event. fulltextindexeventlistener, nhibbench. search' Type = 'Post-insert' />


< Listener Class = 'Nhibbench. Search. event. fulltextindexeventlistener, nhibbench. search' Type = 'Post-Update' />


< Listener Class ='Nhibbench. Search. event. fulltextindexeventlistener, nhibbench. search' Type = 'Post-delete' />

Now we can only manually add the configuration information before creating sessionfactory:

Configuration configuration =NewConfiguration ();
Configuration. setlistener (nhibener. event. listenertype. postupdate,NewFulltextindexeventlistener ());
Configuration. setlistener (nhibener. event. listenertype. postinsert,NewFulltextindexeventlistener ());
Configuration. setlistener (nhibener. event. listenertype. postdelete,NewFulltextindexeventlistener ());
Configuration. Configure ();
Sessionfactory = configuration. buildsessionfactory ();

We have configured full-text index event listening for updates, deletions, and additions. This ensures that the data in the index file is consistent with that in the actual database, so don't worry, this performance is also considered. Here we can support the maximum number of submissions, rather than changing the index file for each operation.

Ii. Use
First, we need to add some attributes about the full-text index of nhib.pdf. Search in the nhib.pdf object. I don't know if it will support xml configuration later, rather than modifying the original object code.

[Indexed (Index =" Book ")]
Public Class Book
{
[Entid]
Public Virtual String Bookid
{
Get ;
Set ;
}
[Field (index. tokenized, store = store. Yes)]
Public Virtual String Title
{
Get ;
Set ;
}
[Field (index. tokenized, store = store. Yes)]
Public Virtual String Authors
{
Get ;
Set ;
}
[Field (index. tokenized, store = store. Yes)]
Public Virtual String Publisher
{
Get ;
Set ;
}
[Field (index. tokenized, store = store. Yes)]
Public Virtual String Summary
{
Get ;
Set ;
}
}

Lucene is used. net friends may find that, in fact, these identifiers are directly targeting fields without using nhib.pdf. but now it is only for the attributes of a business object. in this case, indexes are automatically performed when data is added or changed. However, indexes are automatically updated when data is deleted without manual intervention.
The above is an object book, which contains the name, author, publisher, and introduction. if we want to perform full-text search for these attributes, we can only spell a few like attributes at the same time. If we are targeting databases and database data, the performance will certainly not be good, however, the index file is different. first, let's take a look at how to implement search:

 Public     Static Ilist <booksearchresult> findbooks ( String Query)
{
Ilist <booksearchresult> Results = New List <booksearchresult> ();
Analyzer analyzer = New Lucene. net. analysis. Standard. standardanalyzer ();
Multifieldqueryparser parser = New Multifieldqueryparser ( New String [] {" Title "," Summary "," Authors "," Publisher "
}, Analyzer );
Query queryobj;
Try
{
Queryobj = parser. parse (query );
}
Catch (Parseexception)
{
Return Results;
}
Ifulltextsession session = search. createfulltextsession (nhibernatehelper. getcurrentsession ());

/// 1
System. Type targettype = Typeof (Book );
IQUERY nhquery = session. createfulltextquery (queryobj, New Type [] {targettype });
Ilist <book> books = nhquery. List <book> ();

Nhib.pdf. cfg. Configuration cf = New Configuration (). Configure ();
Searchfactoryimpl searchfactory = searchfactoryimpl. getsearchfactory (CF );
Idirectoryprovider provider = searchfactory. getdirectoryproviders (targettype) [0];

Workspace = New Workspace (searchfactory );
Indexreader = workspace. getindexreader (provider, targettype );
Query simplifiedquery = queryobj. Rewrite (indexreader );
Simplehtmlformatter formatter = New Simplehtmlformatter (" <B class = 'terminal'> "," </B> ");

Highlighter htitle = gethighlighter (simplifiedquery, formatter ," Title ", 100 );
Highlighter hsummary = gethighlighter (simplifiedquery, formatter ," Summary ", 200 );
Highlighter hauthors = gethighlighter (simplifiedquery, formatter ," Authors ", 100 );
Highlighter hpublisher = gethighlighter (simplifiedquery, formatter ," Publisher ", 100 );

Foreach (Book In Books)
{
Booksearchresult result = New Booksearchresult (book );

Tokenstream tstitle = analyzer. tokenstream (" Title ",New System. Io. stringreader (book. Title ?? String . Empty ));
Result. highlightedtitle = htitle. getbestfragment (tstitle, Book. Title );

Tokenstream tsauthors = analyzer. tokenstream (" Authors ", New System. Io. stringreader (book. authors ?? String . Empty ));
Result. highlightedauthors = hauthors. getbestfragment (tsauthors, Book. Authors );

Tokenstream tspublisher = analyzer. tokenstream (" Publisher ",New System. Io. stringreader (book. publisher ??
String . Empty ));
Result. highlightedpublisher = hpublisher. getbestfragment (tspublisher, Book. publisher );

Tokenstream tssummary = analyzer. tokenstream (" Summary ", New System. Io. stringreader (book. Summary ??
String . Empty ));
Result. highlightedsummary = hsummary. getbestfragments (tssummary, Book. Summary, 3 ," ... <Br/>... ");

Results. Add (result );
}
Return Results;
}

I believe Lucene has been used. net friends are not difficult to understand the above Code. All operations using ifulltextsession will perform full-text indexing. in addition, the technology of multi-field parsing and Word Segmentation is not described in detail. the query keyword is highlighted. The booksearchresult entity only packages the book and processes the display. for details, seeSource code.
You can add some operations on the book at // 1 mentioned above to check whether the index file is updated at the same time.

Ii. Results
Download and run the entire project directly, and enter the "program" keyword. You will find that the matching records are listed in a short period of time, and the matched words are displayed in black (as for the comparison with the search performance without indexing, leave it for later ):

 

Iii. Materials and references
1. nhibloud. Search Using Lucene. Net full text index http://blogs.intesoft.net/post/2008/03/NHibernateSearch-using-Lucene-NET-Full-Text-Index-Part1.aspx
2. nhibbench search http://darioquintana.com.ar/blogging? P = 21
3. Lucene. NET and nhib.pdf. Search on medium trust http://www.klopfenstein.net/lorenz.aspx/lucene-net-and-nhibernate-search-on-medium-trust
4. Using nhib.pdf. Search with activerecord http://using.castleproject.org/display/AR/Using+NHibernate.Search+with+ActiveRecord

Download this project file (nhib.pdf + SQLite + Lucene. Net). (For vs 2008)

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.