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.standard.StandardAnalyzer;
022: import org.apache.lucene.document.Document;
023: import org.apache.lucene.document.Field;
024: import org.apache.lucene.index.IndexWriter;
025: import org.apache.lucene.queryParser.ParseException;
026: import org.apache.lucene.queryParser.QueryParser;
027: import org.apache.lucene.store.Directory;
028: import org.apache.lucene.store.RAMDirectory;
029:
030: import java.io.IOException;
031:
032: /**
033: * Tests {@link MultiSearcher} ranking, i.e. makes sure this bug is fixed:
034: * http://issues.apache.org/bugzilla/show_bug.cgi?id=31841
035: *
036: * @version $Id: TestMultiSearcher.java 150492 2004-09-06 22:01:49Z dnaber $
037: */
038: public class TestMultiSearcherRanking extends LuceneTestCase {
039:
040: private final boolean verbose = false; // set to true to output hits
041: private final String FIELD_NAME = "body";
042: private Searcher multiSearcher;
043: private Searcher singleSearcher;
044:
045: public void testOneTermQuery() throws IOException, ParseException {
046: checkQuery("three");
047: }
048:
049: public void testTwoTermQuery() throws IOException, ParseException {
050: checkQuery("three foo");
051: }
052:
053: public void testPrefixQuery() throws IOException, ParseException {
054: checkQuery("multi*");
055: }
056:
057: public void testFuzzyQuery() throws IOException, ParseException {
058: checkQuery("multiThree~");
059: }
060:
061: public void testRangeQuery() throws IOException, ParseException {
062: checkQuery("{multiA TO multiP}");
063: }
064:
065: public void testMultiPhraseQuery() throws IOException,
066: ParseException {
067: checkQuery("\"blueberry pi*\"");
068: }
069:
070: public void testNoMatchQuery() throws IOException, ParseException {
071: checkQuery("+three +nomatch");
072: }
073:
074: /*
075: public void testTermRepeatedQuery() throws IOException, ParseException {
076: // TODO: this corner case yields different results.
077: checkQuery("multi* multi* foo");
078: }
079: */
080:
081: /**
082: * checks if a query yields the same result when executed on
083: * a single IndexSearcher containing all documents and on a
084: * MultiSearcher aggregating sub-searchers
085: * @param queryStr the query to check.
086: * @throws IOException
087: * @throws ParseException
088: */
089: private void checkQuery(String queryStr) throws IOException,
090: ParseException {
091: // check result hit ranking
092: if (verbose)
093: System.out.println("Query: " + queryStr);
094: QueryParser queryParser = new QueryParser(FIELD_NAME,
095: new StandardAnalyzer());
096: Query query = queryParser.parse(queryStr);
097: Hits multiSearcherHits = multiSearcher.search(query);
098: Hits singleSearcherHits = singleSearcher.search(query);
099: assertEquals(multiSearcherHits.length(), singleSearcherHits
100: .length());
101: for (int i = 0; i < multiSearcherHits.length(); i++) {
102: Document docMulti = multiSearcherHits.doc(i);
103: Document docSingle = singleSearcherHits.doc(i);
104: if (verbose)
105: System.out.println("Multi: "
106: + docMulti.get(FIELD_NAME) + " score="
107: + multiSearcherHits.score(i));
108: if (verbose)
109: System.out.println("Single: "
110: + docSingle.get(FIELD_NAME) + " score="
111: + singleSearcherHits.score(i));
112: assertEquals(multiSearcherHits.score(i), singleSearcherHits
113: .score(i), 0.001f);
114: assertEquals(docMulti.get(FIELD_NAME), docSingle
115: .get(FIELD_NAME));
116: }
117: if (verbose)
118: System.out.println();
119: }
120:
121: /**
122: * initializes multiSearcher and singleSearcher with the same document set
123: */
124: protected void setUp() throws Exception {
125: super .setUp();
126: // create MultiSearcher from two seperate searchers
127: Directory d1 = new RAMDirectory();
128: IndexWriter iw1 = new IndexWriter(d1, new StandardAnalyzer(),
129: true);
130: addCollection1(iw1);
131: iw1.close();
132: Directory d2 = new RAMDirectory();
133: IndexWriter iw2 = new IndexWriter(d2, new StandardAnalyzer(),
134: true);
135: addCollection2(iw2);
136: iw2.close();
137:
138: Searchable[] s = new Searchable[2];
139: s[0] = new IndexSearcher(d1);
140: s[1] = new IndexSearcher(d2);
141: multiSearcher = new MultiSearcher(s);
142:
143: // create IndexSearcher which contains all documents
144: Directory d = new RAMDirectory();
145: IndexWriter iw = new IndexWriter(d, new StandardAnalyzer(),
146: true);
147: addCollection1(iw);
148: addCollection2(iw);
149: iw.close();
150: singleSearcher = new IndexSearcher(d);
151: }
152:
153: private void addCollection1(IndexWriter iw) throws IOException {
154: add("one blah three", iw);
155: add("one foo three multiOne", iw);
156: add("one foobar three multiThree", iw);
157: add("blueberry pie", iw);
158: add("blueberry strudel", iw);
159: add("blueberry pizza", iw);
160: }
161:
162: private void addCollection2(IndexWriter iw) throws IOException {
163: add("two blah three", iw);
164: add("two foo xxx multiTwo", iw);
165: add("two foobar xxx multiThreee", iw);
166: add("blueberry chewing gum", iw);
167: add("bluebird pizza", iw);
168: add("bluebird foobar pizza", iw);
169: add("piccadilly circus", iw);
170: }
171:
172: private void add(String value, IndexWriter iw) throws IOException {
173: Document d = new Document();
174: d.add(new Field(FIELD_NAME, value, Field.Store.YES,
175: Field.Index.TOKENIZED));
176: iw.addDocument(d);
177: }
178:
179: }
|