Heres the code. Please keep in mind it's just a class I'm using to become
familar with the concepts 'll be using.
I switched DATE to lower case as per Terry's suggestion. Same thing.
package lucenetest;
import org.apache.lucene.document.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import java.util.*;
public class LuceneTest {
private Map writerMap = new HashMap();
private Map searcherMap = new HashMap();
public LuceneTest() {}
private IndexWriter getWriter(String name)throws Exception{
if(!writerMap.containsKey(name)){
writerMap.put(name, new IndexWriter(name, new SimpleAnalyzer(),
true));
}
return (IndexWriter)writerMap.get(name);
}
private Searcher getSearcher(String name)throws Exception{
if(!searcherMap.containsKey(name)){
searcherMap.put(name, new IndexSearcher(name));
}
return (Searcher)searcherMap.get(name);
}
public void addInfo(String indexName, String id, String title, String
description, String date)throws Exception{
IndexWriter writer = getWriter(indexName);
Document doc = new Document();
doc.add(Field.UnIndexed("ID", id));
doc.add(Field.Text("TITLE", title));
doc.add(Field.Text("DESCRIPTION", description));
doc.add(Field.Keyword("date", date));
writer.addDocument(doc);
writer.optimize();
}
public Hits find(String indexName, String title, String description,
String description2, String start, String end)throws Exception{
Searcher searcher = getSearcher(indexName);
Analyzer analyzer = new SimpleAnalyzer();
String[] fields = null;
ArrayList fieldList = new ArrayList();
StringBuffer buff = new StringBuffer();
if(title!=null){
buff.append("(TITLE:");
buff.append(title);
buff.append(")");
fieldList.add("TITLE");
}
if(description!=null){
buff.append("(DESCRIPTION:");
buff.append(description);
buff.append(")");
fieldList.add("DESCRIPTION");
}
if(description2!=null){
buff.append("(DESCRIPTION:");
buff.append(description2);
buff.append(")");
}
if(start!=null || end!=null){
buff.append(" (date:[");
buff.append(start);
buff.append(" - ");
buff.append(end);
buff.append("]) ");
fieldList.add("DATE");
}
Object[] objs = fieldList.toArray();
fields = new String[objs.length];
for (int i = 0; i < objs.length; i++) {
fields[i] = (String)objs[i];
}
System.out.println("Query : " + buff.toString());
Query query = MultiFieldQueryParser.parse(buff.toString(), fields,
analyzer);
return searcher.search(query);
}
public void showHits(Hits hits)throws Exception{
for (int i = 0; i < hits.length(); i++) {
System.out.println("Score = " + hits.score(i) + " : Document = " +
hits.doc(i));
}
}
public static void main(String[] args) {
LuceneTest test = new LuceneTest();
try{
test.addInfo("myindex", "1", "This is a book about nothing", "It
really does not contains a damn thing worth mentioning. Read it yourself",
"20021105");
test.addInfo("myindex", "2", "This is a book about Something", "It
really does contains a something worth mentioning. Read it yourself",
"20021126");
test.addInfo("myindex", "3", "This is a book about Everything", "It
really does contains a everything worth mentioning. Read it yourself",
"20021113");
test.addInfo("myindex", "4", "Slim", "He's everything you'd ever want
in a developer", "20021115");
test.addInfo("myindex", "5", "Slim Jim Shady", "I'm not so sure this
is the same guy but he is a developer", "20021103");
test.addInfo("myindex", "6", "Slim Shady", "I know this is not the guy
but he too is a developer who know's everything", "20021125");
/*
test.showHits(test.find("myindex", "this", "everything",null, null,
null));
test.showHits(test.find("myindex", "this", "everything","damn", null,
null));
test.showHits(test.find("myindex", null, null, "20021101",
"20021130"));
test.showHits(test.find("myindex", "this", null, null, null));
*/
test.showHits(test.find("myindex", null, null,null, "20021101",
"20021130"));
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Done");
}
}
Thanks,
----- Original Message -----
From: "Otis Gospodnetic" <[EMAIL PROTECTED]>
To: "Lucene Users List" <[EMAIL PROTECTED]>
Sent: Saturday, November 23, 2002 1:51 AM
Subject: Re: Date Range - I've searched FAQs and mail list archive..... no
help..... Really
> You posted this already?
> Hm, must have missed it. I had half a dozen beers ;), but this look
> good. You don't need those outter parenthesis. Provide a
> self-enclosing demonstration class, please, or a URL to your message if
> it you already sent a message containing it.
>
> Otis
>
> --- Michael Caughey <[EMAIL PROTECTED]> wrote:
> > Part of my problem seems to be that the Range Query Object isn't
> > acting as it should as per the FAQ and other mail list entries.
> > I'm using Lucene 1.2
> >
> > I have a field in my index called DATE. I'd like to do a date range
> > search on it. I am using Strings in the format of yyyyMMdd.
> >
> > I have the following dates in my Index:
> > 20021105
> > 20021126
> > 20021113
> > 20021115
> > 20021103
> > 20021125
> >
> >
> > When I use the follwing code to search, I get an exception:
> > *NOTE: I'm using the MultiFieldQueryParser becuase In some cases I
> > check other field, I've simplified this one to demonstrate (and run
> > my tests isolated from other factors)
> >
> > IndexSearcher search = new IndexSearcher("myindex");
> > SimpleAnalyzer analyzer = new SimpleAnalyzer();
> > String[] fields = new String[1];
> > fields[0] = "DATE"
> >
> > String buff = "( DATE:[20021101 - 20021131] )";
> > Query query = MultiFieldQueryParser.parse(buff, fields,
> > analyzer);
> > searcher.search(query);
> >
> > I get the following error:
> > java.lang.IllegalArgumentException: At least one term must be
> > non-null
> >
> > if buff = "( DATE:20021101 - 20021131 )";
> > as well as
> > if buff = "( DATE:(20021101 - 20021131 ))";
> > I simply get no results.
> >
> > I have added the date to the document by both
> > Field.Text("DATE", dateStr);
> > and
> > Field.Keyword("DATE", dateStr);
> >
> > I have also tried to build the queries up creating Objects. One of
> > the things I notice is that if I use the RangeQuery Object there are
> > no spaces on either side of the "-".
> >
> > The documents which I created have the following Fields:
> > TITLE, DESCRIPTION and DATE.
> > If I search on TITLE or DESCRIPTION or a combination of both I get
> > results just fine.
> >
> > Am I doing something stupid, or is this a bug? Seems to based on
> > what I read that the example above where String buff = "(
> > DATE:[20021101 - 20021131] )"; is correct and should work.
> >
> > I published the complete source in an earlier posting called "Problem
> > with Range". It also contains a stack trace of the error.
> >
> > Thanks in advance,
> > Michael
> >
> >
> >
>
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>