Hi all,

I've been heavily modifying the demo code, and really like what I see thus far. However I've run into a problem when trying to add a date range to the BooleanQuery. When I leave the date range out of my query, I get the expected results. When I put the date range in (even if the range is [00000000 - 99999999]) I get no results....

The code and some sample output (leaving the date range blank, and using the date range [00000000 - 99999999]) are included below..

Thanks for your help,
Dominic
madison.com

*******CODE********
package com.madison.lucene;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.index.Term;

class SearchFilesComplex {
public static void main(String[] args) {
try {
Searcher searcher = new IndexSearcher("/home/dominic/lucene_test/index");
Analyzer analyzer = new StandardAnalyzer();

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("Section: ");
String section = in.readLine();

// if (section.length() <= 0)
// break;

System.out.print("Search Words: ");
String search_words = in.readLine();

System.out.println("From Date (yyyymmdd): ");
String from_date = in.readLine();

System.out.println("To Date (yyyymmdd): ");
String to_date = in.readLine();

// Query query = QueryParser.parse(line, "contents", analyzer);
BooleanQuery query = new BooleanQuery();
//if someone has entered a section....
if (section.length() > 0) {
query.add(new TermQuery( new Term ("section", section)), true, false);
System.out.println("section added *" + section + "*");
}
//if someone has entered keywords
if (search_words.length() > 0) {
query.add(new TermQuery( new Term ("contents", search_words)), true, false);
System.out.println("search_words added *" + search_words + "*");
}

//if someone has entered a date range
if ( (from_date.length() == 8) && (to_date.length() == 8) ) {
query.add(new TermQuery( new Term ("date_published", "[" + from_date + " - " + to_date + "]")), true, false);
System.out.println("date range added [" + from_date + " - " + to_date + "]");
}

Hits hits = searcher.search(query);
System.out.println(hits.length() + " total matching documents");

final int HITS_PER_PAGE = 10;
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
int end = Math.min(hits.length(), start + HITS_PER_PAGE);
for (int i = start; i < end; i++) {
Document doc = hits.doc(i);
String path = doc.get("path");
if (path != null) {
System.out.print(i + ". " + path);
System.out.println(" ++ " + doc.get("date_published"));

} else {
String url = doc.get("url");
if (url != null) {
System.out.println(i + ". " + url);
System.out.println(" - " + doc.get("title"));
} else {
System.out.println(i + ". " + "No path nor URL for this document");
}
}
}

if (hits.length() > end) {
System.out.print("more (y/n) ? ");
String line = in.readLine();
if (line.length() == 0 || line.charAt(0) == 'n')
break;
}
}
}
// searcher.close();

} catch (Exception e) {
System.out.println(" caught a " + e.getClass() +
"\n with message: " + e.getMessage());
}
}
}
***********************************************


*****OUTPUT (Without date range parameters filled in)****
[dominic@localhost classes]$ java com.madison.lucene.SearchFilesComplex
Section: communities
Search Words: capital
From Date (yyyymmdd):
To Date (yyyymmdd):

section added *communities *
search_words added *capital*
22 total matching documents
0. /home/dominic/tct/2002/10/02/19623.php ++ 20021002
1. /home/dominic/tct/2002/03/07/27506.php ++ 20020307
2. /home/dominic/tct/2002/11/05/36510.php ++ 20021105
3. /home/dominic/tct/2002/09/13/18159.php ++ 20020913
4. /home/dominic/tct/2002/11/07/36780.php ++ 20021107
5. /home/dominic/tct/2002/09/13/17651.php ++ 20020913
6. /home/dominic/tct/2002/03/05/27214.php ++ 20020305
7. /home/dominic/tct/2002/08/02/12995.php ++ 20020802
8. /home/dominic/tct/2002/03/21/29105.php ++ 20020321
9. /home/dominic/tct/2002/08/19/14759.php ++ 20020819
more (y/n) ?

*********Same query w/ date ranges**********

Section: communities
Search Words: capital
From Date (yyyymmdd):
00000000
To Date (yyyymmdd):
99999999
section added *communities *
search_words added *capital*
date range added [00000000 - 99999999]
0 total matching documents
Section:


_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=features/junkmail


--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to