001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.analysis.SimpleAnalyzer;
022: import org.apache.lucene.document.Document;
023: import org.apache.lucene.document.Field;
024: import org.apache.lucene.index.IndexReader;
025: import org.apache.lucene.index.IndexWriter;
026: import org.apache.lucene.index.Term;
027: import org.apache.lucene.index.TermEnum;
028: import org.apache.lucene.store.RAMDirectory;
029:
030: import java.io.IOException;
031: import java.util.LinkedList;
032:
033: /**
034: * This class tests PhrasePrefixQuery class.
035: *
036: *
037: * @version $Id: TestPhrasePrefixQuery.java 583534 2007-10-10 16:46:35Z mikemccand $
038: */
039: public class TestPhrasePrefixQuery extends LuceneTestCase {
040: public TestPhrasePrefixQuery(String name) {
041: super (name);
042: }
043:
044: /**
045: *
046: */
047: public void testPhrasePrefix() throws IOException {
048: RAMDirectory indexStore = new RAMDirectory();
049: IndexWriter writer = new IndexWriter(indexStore,
050: new SimpleAnalyzer(), true);
051: Document doc1 = new Document();
052: Document doc2 = new Document();
053: Document doc3 = new Document();
054: Document doc4 = new Document();
055: Document doc5 = new Document();
056: doc1.add(new Field("body", "blueberry pie", Field.Store.YES,
057: Field.Index.TOKENIZED));
058: doc2.add(new Field("body", "blueberry strudel",
059: Field.Store.YES, Field.Index.TOKENIZED));
060: doc3.add(new Field("body", "blueberry pizza", Field.Store.YES,
061: Field.Index.TOKENIZED));
062: doc4.add(new Field("body", "blueberry chewing gum",
063: Field.Store.YES, Field.Index.TOKENIZED));
064: doc5.add(new Field("body", "piccadilly circus",
065: Field.Store.YES, Field.Index.TOKENIZED));
066: writer.addDocument(doc1);
067: writer.addDocument(doc2);
068: writer.addDocument(doc3);
069: writer.addDocument(doc4);
070: writer.addDocument(doc5);
071: writer.optimize();
072: writer.close();
073:
074: IndexSearcher searcher = new IndexSearcher(indexStore);
075:
076: //PhrasePrefixQuery query1 = new PhrasePrefixQuery();
077: MultiPhraseQuery query1 = new MultiPhraseQuery();
078: //PhrasePrefixQuery query2 = new PhrasePrefixQuery();
079: MultiPhraseQuery query2 = new MultiPhraseQuery();
080: query1.add(new Term("body", "blueberry"));
081: query2.add(new Term("body", "strawberry"));
082:
083: LinkedList termsWithPrefix = new LinkedList();
084: IndexReader ir = IndexReader.open(indexStore);
085:
086: // this TermEnum gives "piccadilly", "pie" and "pizza".
087: String prefix = "pi";
088: TermEnum te = ir.terms(new Term("body", prefix + "*"));
089: do {
090: if (te.term().text().startsWith(prefix)) {
091: termsWithPrefix.add(te.term());
092: }
093: } while (te.next());
094:
095: query1.add((Term[]) termsWithPrefix.toArray(new Term[0]));
096: query2.add((Term[]) termsWithPrefix.toArray(new Term[0]));
097:
098: Hits result;
099: result = searcher.search(query1);
100: assertEquals(2, result.length());
101:
102: result = searcher.search(query2);
103: assertEquals(0, result.length());
104: }
105: }
|