Lucene3.0 Getting Started Tutorial (ii)--index query

Source: Internet
Author: User

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");

.....................

}

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.