lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lucifer Hammer" <luce...@gmail.com>
Subject Suspected problem in the QueryParser
Date Fri, 03 Nov 2006 13:53:54 GMT
Hi,

I recently stumbled across what I think might be a bug in the QueryParser.
Before I enter it as a bug, I wanted to run it by this group to see if I'm
just not looking at the boolean expression correctly.

Here's the issue:

I created an index with 5 documents, all have one field: "text", with the
following contents:
doc1:text:"Table Chair Spoon"
doc2:text:"Table Chair Spoon Fork"
doc3:text:"Table Spoon Fork"
doc4:text:"Chair Spoon Fork"
doc5:text:"Spoon Fork"

When I enter the query: "Table AND NOT Chair"  I get one hit, doc3
When I enter the query: "Table AND (NOT Chair)" I get 0 hits.

I had thought that both queries would return the same results.  Is this a
bug, or, am I not understanding the query language correctly?

I'm attaching test code.  The program creates an index in the directory
which you pass into the main program.

Thanks!
L
------------------------------------------------------------------------------

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.index.Term;



import java.io.File;
import java.io.IOException;
import java.io.FileReader;

public class IndexTest {
    public static void create(File indexDir) throws IOException {

        IndexWriter writer = new IndexWriter(indexDir, new
                WhitespaceAnalyzer(), true);
        Document doc = new Document();
        doc.add(new Field("text",
                "Table Chair Spoon",
                Field.Store.YES,
                Field.Index.TOKENIZED,
                Field.TermVector.NO));

        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("text",
                "Table Chair Spoon Fork",
                Field.Store.YES,
                Field.Index.TOKENIZED,
                Field.TermVector.NO));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("text",
                "Table Spoon Fork!",
                Field.Store.YES,
                Field.Index.TOKENIZED,
                Field.TermVector.NO));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("text",
                "Chair Spoon Fork",
                Field.Store.YES,
                Field.Index.TOKENIZED,
                Field.TermVector.NO));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("text",
                "Spoon Fork",
                Field.Store.YES,
                Field.Index.TOKENIZED,
                Field.TermVector.NO));
        writer.addDocument(doc);

        writer.close();
    }


    public static void query(File indexDir, String queryString) throws
IOException
    {
        Query query = null;
        Hits hits = null;

        try {
            QueryParser qp = new QueryParser("text",new
WhitespaceAnalyzer());
            qp.setDefaultOperator(QueryParser.OR_OPERATOR);
            query = qp.parse(queryString);
        } catch (Exception qe) {System.out.println(qe.toString());}
        if (query == null) return;
        System.out.println("Query: " + query.toString());
        IndexReader reader = IndexReader.open(indexDir);
        IndexSearcher searcher = new IndexSearcher(reader);

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

        for (int i = 0; i < hits.length(); i++)
        {
            System.out.println( hits.doc(i).get("text") + " ");
        }

        searcher.close();
        reader.close();

    }
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            throw new Exception("Usage: " + IndexTest.class.getName() +
"<index dir>");
        }
        File indexDir = new File(args[0]);
        create(indexDir);
        query(indexDir,"Table AND NOT Chair");
        query(indexDir,"Table AND (NOT Chair)");
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message