Indexwriter (file path, analyzer A, Boolean create)
Indexwriter (string path, analyzer A, Boolean create)
It can be seen that constructing it requires an index file directory, a analyzer (usually standard), and the last parameter is to identify whether to clear the index directory.
It has some parameter setting functions, such as setting the maximum field length.
Let's look at an example:
Public void indexmaxfield () throws ioexception
{
Indexwriter = new indexwriter ("C: // Index", new standardanalyzer (), true );
Document doc1 = new document ();
Doc1.add (new field ("name1", "programmer's house", field. Store. Yes, field. Index. tokenized ));
Document doc2 = new document ();
Doc2.add (new field ("name2", "Welcome to the home of programers", field. Store. Yes, field. Index. tokenized ));
Indexwriter. setmaxfieldlength (5 );
Indexwriter. adddocument (doc1 );
Indexwriter. setmaxfieldlength (3 );
Indexwriter. adddocument (doc1 );
Indexwriter. setmaxfieldlength (0 );
Indexwriter. adddocument (doc2 );
Indexwriter. setmaxfieldlength (3 );
Indexwriter. adddocument (doc2 );
Indexwriter. Close ();
}
Public void searchermaxfield () throws parseexception, ioexception
{
Query query = NULL;
Hits hits = NULL;
Indexsearcher = NULL;
Queryparser = NULL;
Queryparser = new queryparser ("name1", new standardanalyzer ());
Query = queryparser. parse ("programmer ");
Indexsearcher = new indexsearcher ("C: // Index ");
Hits = indexsearcher. Search (query );
System. Out. println ("You searched for: programmer ");
System. Out. println ("found" + hits. Length () + "result ");
System. Out. println ("they are :");
For (INT I = 0; I {
Document Doc = hits.doc (I );
System. Out. println (Doc. Get ("name1 "));
}
Query = queryparser. parse ("programmer's house ");
Indexsearcher = new indexsearcher ("C: // Index ");
Hits = indexsearcher. Search (query );
System. Out. println ("You searched for: programmer's house ");
System. Out. println ("found" + hits. Length () + "result ");
System. Out. println ("they are :");
For (INT I = 0; I {
Document Doc = hits.doc (I );
System. Out. println (Doc. Get ("name1 "));
}
Queryparser = new queryparser ("name2", new standardanalyzer ());
Query = queryparser. parse ("welcome ");
Indexsearcher = new indexsearcher ("C: // Index ");
Hits = indexsearcher. Search (query );
System. Out. println ("You searched for: Welcome ");
System. Out. println ("found" + hits. Length () + "result ");
System. Out. println ("they are :");
For (INT I = 0; I {
Document Doc = hits.doc (I );
System. Out. println (Doc. Get ("name2 "));
}
Query = queryparser. parse ("");
Indexsearcher = new indexsearcher ("C: // Index ");
Hits = indexsearcher. Search (query );
System. Out. println ("the" You searched ");
System. Out. println ("found" + hits. Length () + "result ");
System. Out. println ("they are :");
For (INT I = 0; I {
Document Doc = hits.doc (I );
System. Out. println (Doc. Get ("name2 "));
}
Query = queryparser. parse ("home ");
Indexsearcher = new indexsearcher ("C: // Index ");
Hits = indexsearcher. Search (query );
System. Out. println ("You searched for: Home ");
System. Out. println ("found" + hits. Length () + "result ");
System. Out. println ("they are :");
For (INT I = 0; I {
Document Doc = hits.doc (I );
System. Out. println (Doc. Get ("name2 "));
}
}
Summary:
1. Setting the field length limit only limits the search. If field. Store. Yes is used
All are saved in the index directory.
2. Why is it because Lucene does not search for useless words such as the to of when analyzing English (it is meaningless to search for these words ).
3. The new standardanlayzer () Word Segmentation in English is based on Spaces and useless words, while Chinese is all single words.
4. Set the maximum field length to start with 0 and be the same as the array length.
Programmer's house ---------- 3 -------- programmer's
0 1 2 3
Welcome to the home of programmers ------ 3 ------ welcome to the home of programmers
0 1 2
You can also try something else to deepen your impression.
Summary:
1. Setting the field length limit only limits the search. If field. Store. Yes is used
All are saved in the index directory.
2. Why is it because Lucene does not search for useless words such as the to of when analyzing English (it is meaningless to search for these words ).
3. The new standardanlayzer () Word Segmentation in English is based on Spaces and useless words, while Chinese is all single words.
4. Set the maximum field length to start with 0 and be the same as the array length.
Programmer's house ---------- 3 -------- programmer's
0 1 2 3
Welcome to the home of programmers ------ 3 ------ welcome to the home of programmers
0 1 2
You can also try something else to deepen your impression.