Lucene. Net 2.3.1 Development Introduction-4. Search (1)

Source: Internet
Author: User

Since it is content filtering, or a search engine, there must be an index. Although the search is related to the index, it is only related to the index file, and the indexProgramTherefore, the search and index are generally deployed separately. Simply put, an application (desktop program) is used for indexing and a web program for searching. Of course, to make the test simple, nunit is used here. After searching, we will briefly introduce how to deploy a single-host search engine.

4.1 what is search related?

What is the relationship between search? Even if you have not read the previousArticleNow let's just guess.

First, the search must be related to the index. If it is irrelevant, we do not need to create an index at all. Then, the search must be related to word segmentation, because the index is created based on word segmentation. Also, Word Segmentation must be related to the query keyword. Otherwise, how can we search? The search is indeed related to what we have guessed above. However, in the search, the word segmentation function is different from the word segmentation function in the index.

 Code 4.1.1 
/// <Summary>
/// Search
/// </Summary>
/// <Param name = "querystring"> Search Input </Param>
Private Void Searcher ( String Querystring)
{
Analyzer = New Standardanalyzer ();
Indexsearcher searcher = New Indexsearcher ( " Indexdirectory " );
Queryparser parser = New Queryparser ( " Content " , Analyzer );
Query = Parser. parse (querystring );
Hits hits = Searcher. Search (query );
For ( Int I = 0 ; I < Hits. Length (); I ++ )
{
Document Doc = Hits. DOC (I );
}
}

 

For exampleCode4.1.1. the above three elements are used to construct the searcher, but the word divider is not related to indexsearcher. From the code above, we can see that one closely related to search is an index file and a query expression. The index file is like a database, and the query expression is a T-SQL statement. Of course, the query expression here needs to be obtained by using a word divider to split strings.

4.1.1 search and Indexing
What is the relationship between search and index? Indexing is the operation of recording data, while searching is the operation of filtering data, which is essentially no different from "select * from table", but here, this makes the query speed more efficient. It can be said that the index is preparing for the search, or that the index is the data source for the search. The indexing process is to concentrate all the scattered data here according to a certain structure. More accurately, the index action is preparing for search, while the index file itself is only data.

4.1.2 search and Expression
What is the relationship between search and expressions? Think about it. In database operations, search is an action, and an expression is a conditional expression used for filtering. They are an integral whole. If there is no expression, the search will be meaningless; if no search is performed, the expression is a pseudo-code, and nothing can be done.

4.1.3 word divider and Expression
The word divider acts as a translation and serves as a bridge between the index file and the query expression. If you use different word divider for search and index, note that you need a word divider with different word segmentation effects. You cannot find the desired word. The expression parser translates a string into a search language and then filters it in the index file.

4.1.4 Lucene. net search process
In Lucene. net, if you consider the constructor expression, the search will experience:
(1) construct a query expression;
(2) to open the index file, indexsearcher will open the index file through the indexreader class;
(3) Obtain a filter value of the query expression-weight;
(4) check whether there are suitable records in the Query Buffer. If yes, read the logs. Otherwise, the expansion process is calculated based on weight;
(4) return results.

Of course, the above process is handled by many branches.

4.2 search core classes
You can search for four core classes in the operation.

4.2.1 indexsearcher
undoubtedly, indexsearcher must be one of them. It will open the index file. Of course, it will not use the Lucene. Net lock, so it can be understood as a read-only operation. The search method is the most important method and will return the results we need.

4.2.2Query
The query class is also crucial as the carrier of the query expression. While many of its sub-classes make us feel a headache, we are glad that so many sub-classes have such powerful functions.

4.2.3 queryparser
queryparser is the query constructor. queryparser The amazing experience we have with query.

4.2.4 hits
the set of semantic hits, of course, is the result set we want. It records the retrieved document pointers and several important attributes of these documents, such as scores, such as internal ID numbers.

before using classes to implement queries, we will first look at the usage of each subclass of the query, starting from the next one.

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.