lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steven A Rowe <>
Subject RE: PrefixQuery and special characters
Date Wed, 14 Apr 2010 12:24:38 GMT
Hi Franz,

The likely problem is that you're using an index-time analyzer that strips out the parentheses.
 StandardAnalyzer, for example, does this; WhitespaceAnalyzer does not.

Remember that hits are the result of matches between index-analyzed terms and query-analyzed
terms.  Except in the case of synonyms, most people will want their index and query analyzers
to be the same.


From: Franz Roth []
Sent: Wednesday, April 14, 2010 7:42 AM
Subject: PrefixQuery and special characters

Hi all,

say I have an Index with one field named "category". There are two documents one with value
"(testvalue)" and one with value "test value".
Now somone search with "test". My Searchenine uses the
and finds 2 documents. Maybe he  estimated only one hit; owever: if he searches for "(test"
and the Searchengine uses the QueryParser.escape to clean the request and takes that PrefixQuery
to search nothing results.

How can I search for the document "(testvalue)" and only this one?




import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;

public class TestPrefixQuery extends TestCase {
        public void testEscapeAndPrefix() throws CorruptIndexException,
        LockObtainFailedException, IOException {

            RAMDirectory directory = new RAMDirectory();
            IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(),
                    true, IndexWriter.MaxFieldLength.LIMITED);
            Document doc = new Document();
            doc.add(new Field("category", "(testvalue)", Field.Store.YES,
            doc = new Document();
            doc.add(new Field("category", "test value", Field.Store.YES,

            String value= "test";
            PrefixQuery query = new PrefixQuery(new Term("category", value));
            IndexSearcher searcher = new IndexSearcher(directory);
            ScoreDoc[] hits =, null, 1000).scoreDocs;
            assertEquals("One for " + value , 2, hits.length); //I want one for this?!

            value= "(test";
            String escaped = QueryParser.escape(value);
            query = new PrefixQuery(new Term("category", escaped));
            hits =, null, 1000).scoreDocs;
            assertEquals("One for " + value + "/" + escaped, 1, hits.length); //FAILS!

GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter

To unsubscribe, e-mail:
For additional commands, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message