lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Hostetter <hossman_luc...@fucit.org>
Subject Re: Confused about boolean query and how an IndexReader is associated with Hits
Date Wed, 07 Dec 2005 21:33:16 GMT

:  -ID:0 Category:Category1 Category:Category2
:
: What I hope this says is
:
: "Give me all documents whose ID is not "0" AND which have a Category Field
: which contains "Category1" or "Category2"

That's what you've got.  If it's not matching what you expect it to, then
i'm guessing your index wasn't built the way you expect.


: How do I do that?  As a jar file with the relevent source code in it?  Posted
: as an attachment to this mailing list?

Just doing a cut/paste inline is fine (the mailing list software doesn't
like most attachments).  Here's an example of what you're talking about
that seems to work just fine for me...


    public void sampleTest() throws Exception {

        Directory dir = new RAMDirectory();
        IndexWriter w = new IndexWriter(dir,new WhitespaceAnalyzer(),true);

        Document d = new Document();
        d.add(Field.Keyword("ID","0"));
        w.addDocument(d);
        d = new Document();
        d.add(Field.Keyword("Category","Category1"));
        w.addDocument(d);
        d = new Document();
        d.add(Field.Keyword("Category","Category1"));
        d.add(Field.Keyword("Category","Category2"));
        w.addDocument(d);
        d = new Document();
        d.add(Field.Keyword("Category","Category2"));
        w.addDocument(d);
        d = new Document();
        d.add(Field.Keyword("bogus","don't match"));
        w.addDocument(d);
        w.close();

        IndexSearcher s = new IndexSearcher(dir);
        BooleanQuery q = new BooleanQuery();
        q.add(new TermQuery(new Term("ID","0")),false,true);
        q.add(new TermQuery(new Term("Category","Category1")),false,false);
        q.add(new TermQuery(new Term("Category","Category2")),false,false);
        assertEquals(3, s.search(q).length());
    }






-Hoss


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message