Sorting, weight setting, optimization, and distributed search of e.net advanced applications

Source: Internet
Author: User

The following content is reproduced in version 1.9. The advanced version may already have a better solution.

1. Basic Applications

Using system; using system. collections. generic; using system. text; using Lucene. net; using Lucene. net. analysis; using Lucene. net. analysis. standard; using Lucene. net. events; using Lucene. net. index; using Lucene. net. queryparsers; using Lucene. net. search; using Lucene. net. store; using Lucene. net. util; namespace consoleapplication1.lucene {public class lucenetest {private const string fieldname = "name"; priva Te const string fieldvalue = "value"; private directory = new ramdirectory (); Private analyzer = new standardanalyzer (); Public deleetest () {} private void index () {indexwriter writer = new indexwriter (directory, analyzer, true); writer. maxfieldlength = 1000; For (INT I = 1; I <= 100; I ++) {document = new document (); document. add (new field (fieldname, "name" + I, fie LD. Store. Yes, field. Index. un_tokenized); document. Add (new field (fieldvalue, "Hello, world! ", Field. store. yes, field. index. tokenized); writer. adddocument (document);} writer. optimize (); writer. close ();} private void search () {query = queryparser. parse ("name *", fieldname, analyzer); indexsearcher searcher = new indexsearcher (directory); hits = searcher. search (query); console. writeline ("qualified records: {0}; Total number of index database records: {1}", hits. length (), searcher. reader. numdocs (); For (INT I = 0; I 

In addition to ramdirectory, you can also use fsdirectory. (Note that when the create parameter of fsdirectory. getdirectory is set to true, the existing index library files will be deleted. You can use indexreader. indexexists () to determine the value .)

Open an existing index library from the specified directory.

Private directory = fsdirectory. getdirectory ("C: \ Index", false );


Load the index library into the memory to increase the search speed.

Private directory = new ramdirectory (fsdirectory. getdirectory (@ "C: \ Index", false ));
// Or
// Private directory = new ramdirectory (C: \ Index ");


2. Multi-field search

You can use multifieldqueryparser to specify multiple search fields.

Query query = multifieldqueryparser. parse ("name *", new string [] {fieldname, fieldvalue}, analyzer );

Indexreader reader = indexreader. Open (directory );
Indexsearcher searcher = new indexsearcher (Reader );
Hits hits = searcher. Search (query );


3. Multi-condition search

In addition to using queryparser. parse to break down complex search syntaxes, you can also combine multiple queries.

Query query1 = new termquery (new term (fieldvalue, "name1"); // word search
Query query2 = new wildcardquery (new term (fieldname, "name *"); // wildcard
// Query query3 = new prefixquery (new term (fieldname, "name1"); // field search field: keyword, automatically added at the end *
// Query query4 = new rangequery (new term (fieldnumber, numbertools. longtostring (11l), new term (fieldnumber, numbertools. longtostring (13l), true); // range search
// Query query5 = new filteredquery (query, filter); // search with filter conditions

Booleanquery query = new booleanquery ();
Query. Add (query1, booleanclause. occur. Must );
Query. Add (query2, booleanclause. occur. Must );

Indexsearcher searcher = new indexsearcher (Reader );
Hits hits = searcher. Search (query );


4. Set Weights

You can add weights (boost) to documents and fields so that they rank top in search results. By default, the search result is sorted by document. Score. The larger the value, the higher the ranking. The default boost value is 1.

Score = score * boost


Using the formula above, we can set different weights to influence the ranking.

In the following example, different weights are set based on the VIP level.

Document document = new Document();switch (vip){  case VIP.Gold: document.SetBoost(2F); break;  case VIP.Argentine: document.SetBoost(1.5F); break;}


As long as boost is large enough, a hit result will always rank first, which is the "billing ranking" service of Baidu and other websites.

5. Sort

With the construction parameters of sortfield, we can set sorting fields, sorting conditions, and inverted sorting.

Sort sort = new Sort(new SortField(FieldName, SortField.DOC, false));IndexSearcher searcher = new IndexSearcher(reader);Hits hits = searcher.Search(query, sort);


Sorting has a great impact on search speed. Try not to use multiple sorting conditions.

6. Filter

Filter is used to filter the search results to obtain more accurate results in a smaller range.

For example, we can search for products that are available between-10-1 and-10-30.
For the date and time, we need to convert it to add it to the index database, and it must also be an index field.

// Index
Document. Add (fielddate, datefield. datetostring (date), field. Store. Yes, field. Index. un_tokenized );

//...

// Search
Filter filter = new datefilter (fielddate, datetime. parse ("2005-10-1"), datetime. parse ("2005-10-30 "));
Hits hits = searcher. Search (query, filter );


In addition to the date and time, you can also use integers. For example, the search price is between 100 and ~ Between 200.
Lucene. Net numbertools performs bitwise processing on numbers. If you need floating point numbers, you can refer to the source code.

// Index
Document. Add (new field (fieldnumber, numbertools. longtostring (long) price), field. Store. Yes, field. Index. un_tokenized ));

//...

// Search
Filter filter = new rangefilter (fieldnumber, numbertools. longtostring (100l), numbertools. longtostring (200l), true, true );
Hits hits = searcher. Search (query, filter );


Use query as the filter condition.

Queryfilter filter = new queryfilter (queryparser. parse ("name2", fieldvalue, analyzer ));


We can also use filteredquery for multi-condition filtering.

Filter filter = new datefilter (fielddate, datetime. parse ("2005-10-10"), datetime. parse ("2005-10-15 "));
Filter filter2 = new rangefilter (fieldnumber, numbertools. longtostring (11l), numbertools. longtostring (13l), true, true );

Query query = queryparser. parse ("name *", fieldname, analyzer );
Query = new filteredquery (query, filter );
Query = new filteredquery (query, filter2 );

Indexsearcher searcher = new indexsearcher (Reader );
Hits hits = searcher. Search (query );


7. distributed search

We can use multireader or multisearcher to search for multiple index libraries.

Multireader reader = new multireader (New indexreader [] {indexreader. Open (@ "C: \ Index"), indexreader. Open (@ "\ Server \ Index ")});
Indexsearcher searcher = new indexsearcher (Reader );
Hits hits = searcher. Search (query );


Or

Indexsearcher searcher1 = new indexsearcher (reader1 );
Indexsearcher searcher2 = new indexsearcher (reader2 );
Multisearcher searcher = new multisearcher (New searchable [] {searcher1, searcher2 });
Hits hits = searcher. Search (query );


You can also use parallelmultisearcher for multi-thread parallel search.

8. Merge the index database

Merge directory1 into directory2.

Directory directory1 = fsdirectory. getdirectory ("index1", false );
Directory directory2 = fsdirectory. getdirectory ("index2", false );

Indexwriter writer = new indexwriter (directory2, analyzer, false );
Writer. addindexes (new directory [] {directory });
Console. writeline (writer. doccount ());
Writer. Close ();


9. display the search syntax string

We have combined many search conditions and may want to see what the equivalent search syntax string is.

Booleanquery query = new booleanquery ();
Query. Add (query1, true, false );
Query. Add (query2, true, false );
//...

Console. writeline ("Syntax: {0}", query. tostring ());


Output:
Syntax: + (Name: name * value: name *) + number: [cannot exceed limit B to exceed limit D]

It's that simple.

10. Operate the index database

Delete (soft Delete, only the delete tag is added. Call indexwriter. Optimize () and delete it .)

Indexreader reader = indexreader. Open (directory );

// Delete the document with the specified serial number (docid.
Reader. Delete (123 );

// Delete the document containing the specified term.
Reader. Delete (new term (fieldvalue, "hello "));

// Restore soft deletion.
Reader. undeleteall ();

Reader. Close ();


Incremental update (you only need to set the create parameter to false to add new data to the existing index database .)

Directory directory = fsdirectory. getdirectory ("Index", false );
Indexwriter writer = new indexwriter (directory, analyzer, false );
Writer. adddocument (doc1 );
Writer. adddocument (doc2 );
Writer. Optimize ();
Writer. Close ();


11. Optimization

When you add indexes to fsdirectory in batches, increasing the merge factor and minmergedocs helps improve performance and reduce the index time.

Indexwriter writer = new indexwriter (directory, analyzer, true );

Writer. maxfieldlength = 1000; // maximum field length
Writer. mergefactor = 1000;
Writer. minmergedocs = 1000;

For (INT I = 0; I <10000; I ++)
{
// Add external entes...
}

Writer. Optimize ();
Writer. Close ();

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.