add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / complexPhrase / TestComplexPhraseQuery.java
1 package org.apache.lucene.queryParser.complexPhrase;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.HashSet;
21
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexWriter;
27 import org.apache.lucene.queryParser.QueryParser;
28 import org.apache.lucene.search.IndexSearcher;
29 import org.apache.lucene.search.Query;
30 import org.apache.lucene.search.ScoreDoc;
31 import org.apache.lucene.search.TopDocs;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.util.LuceneTestCase;
34
35 public class TestComplexPhraseQuery extends LuceneTestCase {
36   Directory rd;
37   Analyzer analyzer = new MockAnalyzer(random);
38
39   DocData docsContent[] = { new DocData("john smith", "1"),
40       new DocData("johathon smith", "2"),
41       new DocData("john percival smith", "3"),
42       new DocData("jackson waits tom", "4") };
43
44   private IndexSearcher searcher;
45
46   String defaultFieldName = "name";
47
48   public void testComplexPhrases() throws Exception {
49     checkMatches("\"john smith\"", "1"); // Simple multi-term still works
50     checkMatches("\"j*   smyth~\"", "1,2"); // wildcards and fuzzies are OK in
51     // phrases
52     checkMatches("\"(jo* -john)  smith\"", "2"); // boolean logic works
53     checkMatches("\"jo*  smith\"~2", "1,2,3"); // position logic works.
54     checkMatches("\"jo* [sma TO smZ]\" ", "1,2"); // range queries supported
55     checkMatches("\"john\"", "1,3"); // Simple single-term still works
56     checkMatches("\"(john OR johathon)  smith\"", "1,2"); // boolean logic with
57     // brackets works.
58     checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with
59     // brackets works.
60
61     // checkMatches("\"john -percival\"", "1"); // not logic doesn't work
62     // currently :(.
63
64     checkMatches("\"john  nosuchword*\"", ""); // phrases with clauses producing
65     // empty sets
66
67     checkBadQuery("\"jo*  id:1 smith\""); // mixing fields in a phrase is bad
68     checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
69   }
70
71   private void checkBadQuery(String qString) {
72     QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
73     Throwable expected = null;
74     try {
75       qp.parse(qString);
76     } catch (Throwable e) {
77       expected = e;
78     }
79     assertNotNull("Expected parse error in " + qString, expected);
80
81   }
82
83   private void checkMatches(String qString, String expectedVals)
84       throws Exception {
85     QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
86     qp.setFuzzyPrefixLength(1); // usually a good idea
87
88     Query q = qp.parse(qString);
89
90     HashSet<String> expecteds = new HashSet<String>();
91     String[] vals = expectedVals.split(",");
92     for (int i = 0; i < vals.length; i++) {
93       if (vals[i].length() > 0)
94         expecteds.add(vals[i]);
95     }
96
97     TopDocs td = searcher.search(q, 10);
98     ScoreDoc[] sd = td.scoreDocs;
99     for (int i = 0; i < sd.length; i++) {
100       Document doc = searcher.doc(sd[i].doc);
101       String id = doc.get("id");
102       assertTrue(qString + "matched doc#" + id + " not expected", expecteds
103           .contains(id));
104       expecteds.remove(id);
105     }
106
107     assertEquals(qString + " missing some matches ", 0, expecteds.size());
108
109   }
110
111   @Override
112   public void setUp() throws Exception {
113     super.setUp();
114     rd = newDirectory();
115     IndexWriter w = new IndexWriter(rd, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
116     for (int i = 0; i < docsContent.length; i++) {
117       Document doc = new Document();
118       doc.add(newField("name", docsContent[i].name, Field.Store.YES,
119           Field.Index.ANALYZED));
120       doc.add(newField("id", docsContent[i].id, Field.Store.YES,
121           Field.Index.ANALYZED));
122       w.addDocument(doc);
123     }
124     w.close();
125     searcher = new IndexSearcher(rd, true);
126   }
127
128   @Override
129   public void tearDown() throws Exception {
130     searcher.close();
131     rd.close();
132     super.tearDown();
133   }
134
135   static class DocData {
136     String name;
137
138     String id;
139
140     public DocData(String name, String id) {
141       super();
142       this.name = name;
143       this.id = id;
144     }
145   }
146
147 }