1   /*  Copyright 2004 Brian McCallister
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  package org.skife.lucene.graph;
16  
17  import junit.framework.TestCase;
18  import org.apache.lucene.analysis.standard.StandardAnalyzer;
19  import org.apache.lucene.document.Field;
20  import org.apache.lucene.index.IndexReader;
21  import org.apache.lucene.queryParser.QueryParser;
22  import org.apache.lucene.search.Hits;
23  import org.apache.lucene.search.IndexSearcher;
24  import org.apache.lucene.search.Query;
25  import org.skife.lucene.graph.helper.Beer;
26  import org.skife.lucene.graph.helper.Cozy;
27  import org.skife.lucene.graph.helper.Cooler;
28  
29  import java.io.File;
30  import java.util.Collection;
31  import java.util.Iterator;
32  import java.util.HashSet;
33  
34  public class SearchTest extends TestCase
35  {
36      private QueryParser parser = new QueryParser("name", new StandardAnalyzer());
37      private GraphIndexer indexer;
38  
39      public void setUp() throws Exception
40      {
41          final SimpleNameMapper mapper = new SimpleNameMapper();
42  
43          indexer = new GraphIndexer(new MetadataFactory()
44          {
45              public Field[] build(final Object entity)
46              {
47                  final Field name = Field.Text("name", mapper.build(entity));
48                  return new Field[]{name};
49              }
50          });
51      }
52  
53      public void testChainFromTop() throws Exception
54      {
55          final Cozy cozy = new Cozy(1, new Beer(2, "Schlitz"));
56  
57          final File index = indexer.index(cozy);
58          final IndexReader reader = IndexReader.open(index);
59          final IndexSearcher searcher = new IndexSearcher(reader);
60  
61          final Query query = parser.parse("cozy.beer.name: Schlitz");
62  
63          final Hits hits = searcher.search(query);
64          assertEquals(1, hits.length());
65  
66          searcher.close();
67          reader.close();
68      }
69  
70      public void testChainFromMiddle() throws Exception
71      {
72          final Cozy cozy = new Cozy(1, new Beer(2, "Schlitz"));
73  
74          final File index = indexer.index(cozy);
75          final IndexReader reader = IndexReader.open(index);
76          final IndexSearcher searcher = new IndexSearcher(reader);
77  
78          final Query query = parser.parse("beer.name: Schlitz");
79  
80          final Hits hits = searcher.search(query);
81          assertEquals(1, hits.length());
82  
83          searcher.close();
84          reader.close();
85      }
86  
87      public void testMultipleMatches() throws Exception
88      {
89          final Cozy cozy_one = new Cozy(1, new Beer(2, "Schlitz"));
90          final Cozy cozy_two = new Cozy(3, new Beer(4, "Schlitz"));
91  
92          final HashSet cozies = new HashSet(2);
93          cozies.add(cozy_one);
94          cozies.add(cozy_two);
95  
96          final File index = indexer.index(cozies);
97          final IndexReader reader = IndexReader.open(index);
98          final IndexSearcher searcher = new IndexSearcher(reader);
99  
100         final Query query = parser.parse("beer.name: Schlitz");
101 
102         final Hits hits = searcher.search(query);
103         assertEquals(2, hits.length());
104 
105         searcher.close();
106         reader.close();
107     }
108 
109     public void testFunComplexOne() throws Exception
110     {
111         final Cozy cozy_one = new Cozy(1, new Beer(2, "Schlitz)"));
112         final Cozy cozy_two = new Cozy(3, new Beer(4, "Schlitz)"));
113 
114         final HashSet cozies = new HashSet(2);
115         cozies.add(cozy_one);
116         cozies.add(cozy_two);
117 
118         final File index = indexer.index(cozies);
119         final IndexReader reader = IndexReader.open(index);
120         final IndexSearcher searcher = new IndexSearcher(reader);
121 
122         final Query query = parser.parse("beer.name: Schlitz AND beer.identity: (4 or 2)");
123 
124         final Hits hits = searcher.search(query);
125         assertEquals(2, hits.length());
126 
127         searcher.close();
128         reader.close();
129     }
130 
131     public void testFuzzy() throws Exception
132     {
133         final Cooler cooler = new Cooler(1);
134         cooler.stock(new Beer(2, "Schlitz"));
135         cooler.stock(new Beer(3, "Caffreys"));
136         cooler.stock(new Beer(4, "McEwan's"));
137         final File index = indexer.index(cooler);
138         final IndexReader reader = IndexReader.open(index);
139         final IndexSearcher searcher = new IndexSearcher(reader);
140 
141         final Query query = parser.parse("cooler.beer.name: Schitz~");
142         final Hits hits = searcher.search(query);
143 
144         assertEquals(1, hits.length());
145     }
146 
147     public void testSimplePropertyOnMiddleInstances() throws Exception
148     {
149         final Cozy cozy_one = new Cozy(1, new Beer(2, "Schlitz)"));
150         final Cozy cozy_two = new Cozy(3, new Beer(4, "Schlitz)"));
151 
152         final HashSet cozies = new HashSet(2);
153         cozies.add(cozy_one);
154         cozies.add(cozy_two);
155 
156         final File index = indexer.index(cozies);
157         final IndexReader reader = IndexReader.open(index);
158         final IndexSearcher searcher = new IndexSearcher(reader);
159 
160         final Query query = parser.parse("name: Schlitz");
161 
162         final Hits hits = searcher.search(query);
163         assertEquals(2, hits.length());
164 
165         searcher.close();
166         reader.close();
167     }
168 
169     public void testRootProperty() throws Exception
170     {
171         final Cozy cozy = new Cozy(1, new Beer(2, "Schlitz"));
172 
173         final File index = indexer.index(cozy);
174         final IndexReader reader = IndexReader.open(index);
175         final IndexSearcher searcher = new IndexSearcher(reader);
176 
177         final Query query = parser.parse("cozy.identity: 1");
178 
179         final Hits hits = searcher.search(query);
180         assertEquals(1, hits.length());
181 
182         searcher.close();
183         reader.close();
184     }
185 
186     public void testNestedProperty() throws Exception
187     {
188         final Cozy cozy = new Cozy(1, new Beer(2, "Schlitz)"));
189 
190         final File index = indexer.index(cozy);
191         final IndexReader reader = IndexReader.open(index);
192         final IndexSearcher searcher = new IndexSearcher(reader);
193 
194         final Query query = parser.parse("beer.identity: 2");
195 
196         final Hits hits = searcher.search(query);
197         assertEquals(1, hits.length());
198 
199         searcher.close();
200         reader.close();
201     }
202 
203     public void testFindInCollection() throws Exception
204     {
205         final Cooler cooler = new Cooler(1);
206         cooler.stock(new Beer(2, "Coors"));
207         cooler.stock(new Beer(3, "Caffreys"));
208         cooler.stock(new Beer(4, "McEwan's"));
209         final File index = indexer.index(cooler);
210         final IndexReader reader = IndexReader.open(index);
211         final IndexSearcher searcher = new IndexSearcher(reader);
212 
213         final Query query = parser.parse("cooler.beer.name: Coors");
214         final Hits hits = searcher.search(query);
215 
216         assertEquals(1, hits.length());
217     }
218 
219     public void testFindMultipleInCollection() throws Exception
220     {
221         final Cooler cooler = new Cooler(1);
222         cooler.stock(new Beer(2, "Coors"));
223         cooler.stock(new Beer(3, "Caffreys"));
224         cooler.stock(new Beer(4, "McEwan's"));
225         final File index = indexer.index(cooler);
226         final IndexReader reader = IndexReader.open(index);
227         final IndexSearcher searcher = new IndexSearcher(reader);
228 
229         final Query query = parser.parse("cooler.beer.name: Coors OR Caffreys");
230         final Hits hits = searcher.search(query);
231 
232         assertEquals(2, hits.length());
233     }
234 
235     public void testExperimenting() throws Exception
236     {
237         final Cooler cooler = new Cooler(1);
238         cooler.stock(new Beer(2, "Coors"));
239         cooler.stock(new Beer(3, "Caffreys"));
240         cooler.stock(new Beer(4, "McEwan's"));
241         final File index = indexer.index(cooler);
242         final IndexReader reader = IndexReader.open(index);
243         final IndexSearcher searcher = new IndexSearcher(reader);
244 
245         final Collection fields = reader.getFieldNames();
246         for (Iterator iterator = fields.iterator(); iterator.hasNext();)
247         {
248             final String field = (String) iterator.next();
249             System.err.println("Field: " + field);
250         }
251 
252         searcher.close();
253         reader.close();
254     }
255 }