Lucene. Net is queried by time range, and no data is found

Source: Internet
Author: User

Recently, Lucene. Net was used for a full-text search. The Lucene. Net 1.9 version was used at the beginning. There was no problem in querying by keyword. Join the query by time range, and no data was found. The Lucene statements are retrieved directly from the Luke tool and are not recorded.
I sent an email yesterday asking for the rain mark RainTrail (http://www.rainsts.net/), he replied to me, gave me a short piece of code, a little thought, it opened up. Thank you very much!
Use Lucene. net 1.9 I have not found a solution, but still cannot implement query by time range. I use Lucene instead. net 2.0, solution 1: Lucene compound statement + use the RangeFilter class to implement query by time range. Solution 2: only the Lucene compound statement can be used to check the data, but the key display content is null (Why ?). The key code is as follows:

Solution 1: Lucene compound statement + RangeFilter class
Public ResultSet Search (int pageIndex, int PageSize, int PostTime ,.....)
{
ChineseAnalyzer analyze = new ChineseAnalyzer ();
StopWatch sw = new StopWatch (); // defines a StopWatch object
String strlucene = ""; // lucene statement. No time query statement is added.
... // Such as strlucene = + (+ Content: Mobile Phone + Content: digital camera-Content: sales)
QueryParser parser = new QueryParser ("Content", analyze );
Query query = parser. Parse (strlucene );

If (PostTime! = 0)
{// Use the RangeFilter class
String lower = DateTime. Now. AddDays (PostTime * (-1). ToString ("yyyyMMdd ");
String upper = DateTime. Now. ToString ("yyyyMMdd ");
RangeFilter filter = new RangeFilter ("PostDateTime", lower, upper, true, true); Hits hits = searcher. Search (query, filter );
Long executionTime = sw. Peek ();
ResultSet results = GetResults (hits, pageIndex, query, PageSize); // you can view the self-built method at the bottom of the page to return the expected dataset results. ExecutionTime = executionTime;
Return results;
}
Else
{
Hits hits = searcher. Search (query );
Long executionTime = sw. Peek ();
ResultSet results = GetResults (hits, pageIndex, query, PageSize); // same as results. ExecutionTime = executionTime;
Return results;
} Solution 2: only use the Lucene compound statement

Public ResultSet Search (int pageIndex, int PageSize, int PostTime ,.....)
{

ChineseAnalyzer analyze = new ChineseAnalyzer ();
StopWatch sw = new StopWatch (); // defines a StopWatch object

String strlucene = ""; // lucene statement, with the time query statement if (PostTime! = 0)
{
Strposttime = "+ PostDateTime: [" + DateTime. now. addDays (PostTime * (-1 )). toString ("yyyyMMdd") + "TO" + DateTime. now. toString ("yyyyMMdd") + "]" ;}...... // such as strlucene = + (+ Content: Mobile Phone + Content: digital camera-Content: sales)
QueryParser parser = new QueryParser ("Content", analyze );
Query query = parser. Parse (strlucene );
Hits hits = searcher. Search (query );
Long executionTime = sw. Peek ();
ResultSet results = GetResults (hits, pageIndex, query, PageSize); // same as results. ExecutionTime = executionTime;
Return results;
}

Private ResultSet GetResults (Hits hits, int pageIndex, Query query, int PageSize)
{
Int startPosition = (pageIndex-1) * PageSize;
Int endPosition = startPosition + PageSize;
If (hits. Length () <endPosition)
{
EndPosition = hits. Length ();
}
Return GetResults (hits, startPosition, endPosition, query );
}
Private ResultSet GetResults (Hits hits, int startPosition, int endPosition, Query query)
{
ResultSet results = new ResultSet ();
Try
{
ChineseAnalyzer analyzer = new ChineseAnalyzer ();
Results. Count = hits. Length (); Highlighter highlighter = null;
ArrayList al = new ArrayList ();
Result result = null;
Document doc = null;
For (int I = startPosition; I <endPosition; I ++)
{
Result = new Result ();
Doc = hits. Doc (I );
Result. PostDateTime = doc. GetField ("PostDateTime"). StringValue ();
Result. UserName = doc. GetField ("UserName"). StringValue ();
Result. PostUrl = doc. GetField ("PostUrl"). StringValue ();
Result. Title = doc. GetField ("Title"). StringValue ();
If (FragementSize> 0)
{
String text = doc. GetField ("Content"). StringValue ();
// Highlight settings
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter ("<font color = \" red \ ">", "</font> ");
Highlighter = new Highlighter (simpleHTMLFormatter, new QueryScorer (query ));
Highlighter. SetTextFragmenter (new SimpleFragmenter (FragementSize ));
TokenStream tokenStream = analyzer. TokenStream ("Content", new StringReader (text ));
Result. Content = highlighter. GetBestFragment (tokenStream, text); // when a Time query statement is added, the null value is assigned}
Else
{
Result. Content = doc. GetField ("Content"). StringValue ();
}
Result. Score = hits. Score (I); al. Add (result );
}
Results. Results = (Result []) al. ToArray (typeof (Result ));
}
Catch (Exception e)
{
LogError. Write ("GetResult Fail:" + e. Message );
}
Return results ;}

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.