Query for an index that has already been created, that is, a search, which includes two parts, one is how to read all the records of an index directly, and the other is to search for an index.
First, list the documents in the index
Lists the classes or packages that an index needs to introduce
<%@ page import = "org.apache.lucene.document.*"%>
<%@ page import = "org.apache.lucene.index.*"%>
<%@ page import = "org.apache.lucene.store.*"%>
<%@ page import = "Org.apache.lucene.util.Version"%>
Index Directory
String Indexpath = Request.getrealpath ("Site Directory");
Indexreader reader = null;
Long StartTime = (new Java.util.Date ()). GetTime ();
Open Index
try {
Reader = Indexreader.open (Fsdirectory.open (New File (Indexpath)), true);
}
catch (Exception e) {
;
}
Version: <%=reader.getversion ()%> document: <%=pagination.gettotalresult ()%> <br/>
Termdocs Tdocs = Reader.termdocs ();
int i = 0;
while (Tdocs.next ())
{
i++;
Document doc = reader.document (Tdocs.doc ());
String docid = doc.get ("id");
String title = Doc.get ("title");
String area = Doc.get ("Shadowtime");
....
}
Second, search index
In addition to the packages introduced above, it is also necessary to introduce
<%@ page import = "org.apache.lucene.search.*"%>
<%@ page import = "org.apache.lucene.queryparser.*"%>
Index Directory
String Indexpath = Request.getrealpath ("Site Directory");
Analyzer Analyzer = new Ikanalyzer ();
Indexsearcher searcher = null;
Indexreader reader = null;
query query = NULL;
Topdocs hits = null;
Open Index Directory
Try
{
Reader = Indexreader.open (Fsdirectory.open (New File (Indexpath)), true);
Searcher = new Indexsearcher (reader);
}
catch (Exception e)
{
Return
}
Execute Query
Try
{
Multi-field search, where to search how many fields must have many parameters
Booleanclause.occur[] clauses = {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD};
string[] FieldNames = new string[] {"title", "Content"};
Ikqueryparser ikquery = new Ikqueryparser ();
query = Ikquery.parsemultifield (FieldNames, querystring, clauses);
Get Word segmentation results (this is just easy to highlight the results of the search result, not the necessary option)
String WD = "";
Reader r = new StringReader (querystring);
Org.apache.lucene.analysis.TokenStream TSI = Analyzer.tokenstream ("", r);
Org.apache.lucene.analysis.Token Token;
while (Tsi.incrementtoken ())
{
WD = Tsi.tostring (). ReplaceAll ("\ (|, (. *)", "");
Spword + = (spword== "" WD: "," + wd);
}
Tsi.close ();
}
catch (Exception e)
{
return;
}
Read search results
hits = searcher.search (query, 0);
for (int i = startindex i < hits.totalhits; i++)
{
if (i >= hits.totalhits) {break;}
Document doc = Searcher.doc (hits.scoredocs[i].doc);
String docid = doc.get ("id");
String title = Doc.get ("title");
.....................
}