|
36 | 36 | import org.infinispan.query.SearchManager;
|
37 | 37 | import java.util.ArrayList;
|
38 | 38 | import org.infinispan.Cache;
|
| 39 | +import org.apache.lucene.search.TermQuery; |
| 40 | +import org.apache.lucene.index.Term; |
| 41 | +import org.apache.lucene.search.BooleanClause; |
| 42 | +import org.apache.lucene.search.BooleanQuery; |
| 43 | +import org.apache.lucene.search.Query; |
| 44 | +import org.hibernate.search.query.dsl.QueryBuilder; |
39 | 45 |
|
40 | 46 | /**
|
41 | 47 | * Adds, retrieves, removes new cars from the cache. Also returns a list of cars stored in the cache.
|
@@ -186,18 +192,42 @@ public String search() {
|
186 | 192 | carCache = provider.getCacheContainer().getCache(CAR_CACHE_NAME);
|
187 | 193 | SearchManager sm = Search.getSearchManager((Cache) carCache);
|
188 | 194 |
|
189 |
| - |
190 |
| - |
191 |
| - |
192 |
| - //TODO: create the query using either QueryBuilder from Hibernate Search or BooleanQuery from Lucene |
193 |
| - |
194 |
| - |
195 |
| - |
196 |
| - |
197 |
| - System.out.println("Lucene Query: " + q.toString() ); //printing out raw Lucene query |
| 195 | +// //match any of the parameters---------------------------------------------------------------- |
| 196 | +// QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Car.class).get(); |
| 197 | +// Query q1 = queryBuilder |
| 198 | +// .bool() |
| 199 | +// .should(queryBuilder.keyword().onFields("brand") |
| 200 | +// .matching(car.getBrand().isEmpty() ? "none" : car.getBrand()) |
| 201 | +// .createQuery()) |
| 202 | +// .should(queryBuilder.keyword().onFields("color") |
| 203 | +// .matching(car.getColor().isEmpty() ? "none" : car.getColor()) |
| 204 | +// .createQuery()) |
| 205 | +// .should(queryBuilder.keyword().onFields("country") |
| 206 | +// .matching(car.getCountry()) |
| 207 | +// .createQuery()) |
| 208 | +// .createQuery(); |
| 209 | +// System.out.println("Lucene Query: " + q1.toString() ); //printing out raw Lucene query |
| 210 | + |
| 211 | + |
| 212 | + //match all the parameters---------------------------------------------------------------------- |
| 213 | + BooleanQuery q2 = new BooleanQuery(); |
| 214 | + |
| 215 | + if (!car.getBrand().isEmpty()) { |
| 216 | + Query query = new TermQuery(new Term("brand", car.getBrand().toLowerCase())); |
| 217 | + q2.add(query, BooleanClause.Occur.MUST); |
| 218 | + } |
| 219 | + if (!car.getColor().isEmpty()) { |
| 220 | + Query query = new TermQuery(new Term("color", car.getColor().toLowerCase())); |
| 221 | + q2.add(query, BooleanClause.Occur.MUST); |
| 222 | + } |
| 223 | + if (!car.getCountry().equals(Car.Country.Unused)) { |
| 224 | + Query query = new TermQuery(new Term("country", car.getCountry().toString().toLowerCase())); |
| 225 | + q2.add(query, BooleanClause.Occur.MUST); |
| 226 | + } |
| 227 | + System.out.println("Lucene Query: " + q2.toString() ); //printing out raw Lucene query |
198 | 228 |
|
199 | 229 | //create a cache query based on the Lucene query
|
200 |
| - CacheQuery cq = sm.getQuery(q, Car.class); |
| 230 | + CacheQuery cq = sm.getQuery(q2, Car.class); |
201 | 231 | searchResults = new ArrayList<Car>();
|
202 | 232 | //invoke the cache query
|
203 | 233 | for (Object o : cq.list()) {
|
|
0 commit comments