FileDocCategorySizeDatePackage
TestParser.javaAPI DocApache Lucene 2.1.06423Wed Feb 14 10:46:18 GMT 2007org.apache.lucene.xmlparser

TestParser

public class TestParser extends TestCase
author
maharwood

Fields Summary
CoreParser
builder
static Directory
dir
Analyzer
analyzer
IndexReader
reader
private IndexSearcher
searcher
boolean
printResults
Constructors Summary
Methods Summary
private voiddumpResults(java.lang.String qType, org.apache.lucene.search.Query q, int numDocs)

		Hits h = searcher.search(q);
		assertTrue(qType +" should produce results ", h.length()>0);
		if(printResults)
		{
			System.out.println("========="+qType+"============");
			for(int i=0;i<Math.min(numDocs,h.length());i++)
			{
				org.apache.lucene.document.Document ldoc=h.doc(i);
				System.out.println("["+ldoc.get("date")+"]"+ldoc.get("contents"));
			}
			System.out.println();
		}
	
private org.apache.lucene.search.Queryparse(java.lang.String xmlFileName)

		InputStream xmlStream=TestParser.class.getResourceAsStream(xmlFileName);
		Query result=builder.parse(xmlStream);
		xmlStream.close();
		return result;
	
protected voidsetUp()

	
	
	/*
	 * @see TestCase#setUp()
	 */
	     
		super.setUp();
		
		//initialize the parser
		builder=new CorePlusExtensionsParser(analyzer,new QueryParser("contents", analyzer));
		
		//initialize the index (done once, then cached in static data for use with ALL tests)		
		if(dir==null)
		{
			BufferedReader d = new BufferedReader(new InputStreamReader(TestParser.class.getResourceAsStream("reuters21578.txt"))); 
			dir=new RAMDirectory();
			IndexWriter writer=new IndexWriter(dir,analyzer,true);
			String line = d.readLine();		
			while(line!=null)
			{
				int endOfDate=line.indexOf('\t");
				String date=line.substring(0,endOfDate).trim();
				String content=line.substring(endOfDate).trim();
				org.apache.lucene.document.Document doc =new org.apache.lucene.document.Document();
				doc.add(new Field("date",date,Field.Store.YES,Field.Index.TOKENIZED));
				doc.add(new Field("contents",content,Field.Store.YES,Field.Index.TOKENIZED));
				writer.addDocument(doc);
				line=d.readLine();
			}			
			d.close();
		}
		reader=IndexReader.open(dir);
		searcher=new IndexSearcher(reader);
		
	
protected voidtearDown()

		reader.close();
		searcher.close();
//		dir.close();
		
	
public voidtestBooleanFilterXML()

			Query q=parse("BooleanFilter.xml");
			dumpResults("Boolean filter", q, 5);
	
public voidtestBooleanQueryXML()

			Query q=parse("BooleanQuery.xml");
			dumpResults("BooleanQuery", q, 5);
	
public voidtestBoostingQueryXML()

			Query q=parse("BoostingQuery.xml");
			dumpResults("boosting ",q, 5);
	
public voidtestCachedFilterXML()

			Query q=parse("CachedFilter.xml");
			dumpResults("Cached filter", q, 5);
	
public voidtestConstantScoreQueryXML()

			Query q=parse("ConstantScoreQuery.xml");
			dumpResults("ConstantScoreQuery",q, 5);
	
public voidtestFuzzyLikeThisQueryXML()

			Query q=parse("FuzzyLikeThisQuery.xml");
			//show rewritten fuzzyLikeThisQuery - see what is being matched on
			if(printResults)
			{
				System.out.println(q.rewrite(reader));
			}
			dumpResults("FuzzyLikeThis", q, 5);
	
public voidtestLikeThisQueryXML()

			Query q=parse("LikeThisQuery.xml");
			dumpResults("like this", q, 5);
	
public voidtestMatchAllDocsPlusFilterXML()

			Query q=parse("MatchAllDocsQuery.xml");
			dumpResults("MatchAllDocsQuery with range filter", q, 5);
	
public voidtestNestedBooleanQuery()

			Query q=parse("NestedBooleanQuery.xml");
			dumpResults("Nested Boolean query", q, 5);
	
public voidtestRangeFilterQueryXML()

			Query q=parse("RangeFilterQuery.xml");
			dumpResults("RangeFilter", q, 5);
	
public voidtestSimpleTermsQueryXML()

			Query q=parse("TermsQuery.xml");
			dumpResults("TermsQuery", q, 5);
	
public voidtestSimpleXML()

			Query q=parse("TermQuery.xml");
			dumpResults("TermQuery", q, 5);
	
public voidtestSpanTermXML()

			Query q=parse("SpanQuery.xml");
			dumpResults("Span Query",q, 5);
	
public voidtestTermsFilterXML()

			Query q=parse("TermsFilterQuery.xml");
			dumpResults("Terms Filter",q, 5);
	
public voidtestUserQueryXML()

			Query q=parse("UserInputQuery.xml");
			dumpResults("UserInput with Filter", q, 5);