1. Search by entry-termquery
Query = new termquery (new term ("name", "word1 "));
Hits = searcher. Search (query );
In this way, you can retrieve all the documents containing word1 whose field is name.
2. "and" search-booleanquery
It is actually a combination of query to see the following code:
Query1 = new termquery (new term ("name", "word1 "));
Query2 = new termquery (new term ("name", "word2 "));
Booleanquery query = new booleanquery ();
Query. Add (query1, booleanclause. occur. Must );
Query. Add (query2, booleanclause. occur. Must );
Hits = searcher. Search (query );
Among them, must, shocould, and must_not indicate and, or, not, which can be easily understood from the literal meaning.
Lucene supports a maximum of 1024 queries in a row.
3. Search for rangequery in a certain range
Indexsearcher searcher = new indexsearcher ("F: \ resource \ Lucene study \ test ");
Term begintime = new term ("time", "200001 ");
Term endtime = new term ("time", "200005 ");
Hits hits = NULL;
Rangequery query = NULL;
Query = new rangequery (begintime, endtime, false );
Hits = searcher. Search (query );
The parameters of the rangequery constructor indicate the start, end, and whether the boundary is included. In this way, we can search as required.
Fourth, use prefix search-prefixquery
This retrieval mechanism is somewhat similar to indexof () from prefix search. This is often used in English and rarely used in Chinese. The Code is as follows:
Indexsearcher searcher = new indexsearcher ("F: \ resource \ Lucene study \ test ");
Term pre1 = new term ("name", "da ");
Query = new prefixquery (pre1 );
Hits = searcher. Search (query );
Fifth, search for multiple keywords-phrasequery
You can query multiple keywords at the same time. Use:
Query = new phrasequery ();
Query. Add (word1 );
Query. Add (word2 );
Query. setslop (0 );
Hits = searcher. Search (query );
Printresult (hits, "'David' and 'Mary 'closely separated document ");
Query. setslop (2 );
Hits = searcher. Search (query );
Printresult (hits, "the phrase" between 'David' and 'Mary ");
Here we should pay attention to the meaning of the query. setslop (); method.
Query. setslop (0); closely connected (this condition is harsh)
Query. setslop (2); separated
6. Search by phrase suffix-pharseprefixquery
Using pharseprefixquery can easily retrieve related phrases.
Instance:
Query = new phraseprefixquery ();
// Add all possible uncertain words
Term word1 = new term ("content", "David ");
Term word2 = new term ("content", "Mary ");
Term word3 = new term ("content", "Smith ");
Term word4 = new term ("content", "Robert ");
Query. Add (new term [] {word1, word2 });
// Add the specified word
Query. Add (word4 );
Query. setslop (2 );
Hits = searcher. Search (query );
Printresult (hits, "a document with the phrase 'David Robert 'or 'Mary Robert ");
7. Search for similar words-fuzzyquery
It is a fuzzy query.
Instance:
Term word1 = new term ("content", "David ");
Hits hits = NULL;
Fuzzyquery query = NULL;
Query = new fuzzyquery (word1 );
Hits = searcher. Search (query );
Printresult (hits, "a word similar to 'David ");
Eighth, use wildcard search-wildcardquery
Instance:
Indexsearcher searcher = new indexsearcher ("F: \ resource \ Lucene
Study \ test ");
Term word1 = new term ("content", "* ever ");
Term word2 = new term ("content", "wh? Ever ");
Term word3 = new term ("content", "H ?? Ever ");
Term word4 = new term ("content", "ever *");
Wildcardquery query = NULL;
Hits hits = NULL;
Query = new wildcardquery (word1 );
Hits = searcher. Search (query );
Printresult (hits, "* ever ");
Query = new wildcardquery (word2 );
Hits = searcher. Search (query );
Printresult (hits, "wh? Ever ");
Query = new wildcardquery (word3 );
Hits = searcher. Search (query );
Printresult (hits, "H ?? Ever ");
Query = new wildcardquery (word4 );
Hits = searcher. Search (query );
Printresult (hits, "ever *");
Can we see the wildcard? Represents 1 character, * represents 0 to multiple characters.
Lucene now supports the search methods listed in the preceding eight versions. You can select a suitable search method as needed. Of course, some of the above may be more effective for English, so Chinese is not desirable, so we start to think about Baidu, we only search results in one input box. With this question, let's open the discussion in the next chapter!
Query string parsing: We often enter the text we want to search in an input box and hand it to the search engine for word segmentation.
The queryparser class is the parsing class for the query string.
Let's take a look at its usage:
Query = queryparser. parse (key1, "name", new standardanalyzer ());
Hits = searcher. Search (query );
It directly returns a query object. The parameters to be passed in are:
The string to be queried, the name of the corresponding field to be retrieved, and the word segmentation class.
Analyzer analyzer = new cjkanalyzer ();
String [] fields = {"FILENAME", "content "};
Query query = multifieldqueryparser. parse (searchword, fields, analyzer );
Hits hits = searcher. Search (query );
"And" and "or" of queryparser ":
The default value is or between queryparser. If we want to change it to or, add the following code:
Queryparser. setoperator (queryparser. default_operator_and );
You can.
Lucece provides so many query methods that you can test it on your own (and advanced search skills are not tested). This makes you feel that many of the search engines you have learned are not that far away!" Combining theory with practice: "That's right !!!
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wangdetian168/archive/2008/06/02/2503194.aspx