Hi John,

Further to my question below, I did some back-to-basics investigation of PhraseQueries and found that even basic ones fail for me... I found the attached code on the Internet (see http://affy.blogspot.com/2003/04/codebit-examples-for-all-of-lucenes.html) and this fails too... Can you explain why? I would expect the first test to deliver 2 hits.

I have tried with Lucene 2.0 and 2.3.2 jars and both fail.

Thanks again,

- Chris



Chris Bamford wrote:
Hi John,

Just continuing from an earlier question where I asked you how to handle strings like "from:fred flintston*" (sorry I have lost the original email). You advised me to write my own BooleanQuery and add to it Prefix- / Term- / Phrase- Querys as appropriate. I have done so, but am having trouble with the result - my PhraseQueries just do not get any hits at all :-( My code looks for quotes - if it finds them, it treats the quoted phrase as a PhraseQuery and sets the slop factor to 0.
so,  an input of:

   subject:"Good Morning"

results in a PhraseQuery (which I add to my BooleanQuery and then dump with toString()) of:

   +subject:"good morning"

... which fails.
However, if I break it into 2 TermQuerys, it works (but that's not what I want).

What am I missing?

Thanks,

- Chris

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



--
------------------------------------------------------------------------
*Chris Bamford*
Senior Development Engineer     <http://www.scalix.com>
------------------------------------------------------------------------
/Email / MSN/   [EMAIL PROTECTED]
/Tel/   +44 (0)1344 381814              /Skype/         c.bamford

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Experimental;


import java.io.IOException;
import java.util.LinkedList;

import junit.framework.TestCase;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;


public class TestRunQueries extends TestCase {
    
    public void testQueries() throws Exception {

		RAMDirectory indexStore = new RAMDirectory();
		Query query = null;

		String docs[] = {
			"aaa bbb ccc",
			"aaa ddd eee",
			"aaa ddd fff",
			"aaa dee fff",
			"AAA fff ggg",
			"ggg hhh iii",
			"123 123 1z2",	// document containing z for 
					// WildcardQuery example.
			"999 123 123",	// document for fuzzy search.
			"9x9 123 123",	// document for fuzzy search.
			"99 123 123",	// document for fuzzy search.
			"xxx yyy zzz"
		};
		
		IndexWriter writer = new IndexWriter(indexStore, 
			new StandardAnalyzer(), true);
		for (int j = 0; j < docs.length; j++) {
			Document d = new Document();
			d.add(new Field("body", docs[j], Field.Store.YES, Field.Index.UN_TOKENIZED));
			writer.addDocument(d);
		}
		writer.close();

		IndexReader indexReader = IndexReader.open(indexStore);

		System.out.println("\n****** PhraseQuery Example ******");
		System.out.println("NOTE: 2 documents are found " 
			+ "with 'aaa ddd' in order.");
		System.out.println("--------------" 
			+ "------------------------");
		PhraseQuery pq = new  PhraseQuery();		
		pq.add(new Term("body", "aaa"));
		pq.add(new Term("body", "ddd"));
		TestRunQueries.runQueryAndDisplayResults(indexStore, pq);
                
		System.out.println("");
		System.out.println("NOTE: ZERO documents are" 
			+ " found with 'xxx ddd' in order.");
		System.out.println("-----------" 
			+ "---------------------------");
		pq = new  PhraseQuery();		
		pq.add(new Term("body", "xxx"));
		pq.add(new Term("body", "ddd"));
		TestRunQueries.runQueryAndDisplayResults(indexStore, pq);
	}

	public static void runQueryAndDisplayResults(Directory indexStore, Query q) throws IOException {
		IndexSearcher searcher = new IndexSearcher(indexStore);
                
                System.out.println("runQueryAndDisplayResults: query = " + q.toString());
		Hits hits = searcher.search(q);
		int _length = hits.length();
		System.out.println("HITS: " + _length);
		for (int i = 0; i < _length; i++) {
			Document doc = hits.doc(i);
			Field field = doc.getField("body");
			System.out.println("  value: " + field.stringValue());
		}
		searcher.close();
	}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to