public void testNamedFilters() { createData(); FullTextSession s = Search.getFullTextSession(openSession()); s.getTransaction().begin(); BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD); FullTextQuery ftQuery = s.createFullTextQuery(query, Driver.class); assertEquals("No filter should happen", 3, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.disableFullTextFilter("bestDriver"); // was not enabled, but should be harmless ftQuery.enableFullTextFilter("bestDriver"); assertEquals("Should filter out Gavin", 2, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("bestDriver"); ftQuery.enableFullTextFilter("security").setParameter("login", "andre"); assertEquals("Should filter to limit to Emmanuel", 1, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("bestDriver"); ftQuery.enableFullTextFilter("security").setParameter("login", "andre"); ftQuery.disableFullTextFilter("security"); ftQuery.disableFullTextFilter("bestDriver"); assertEquals("Should not filter anymore", 3, ftQuery.getResultSize()); s.getTransaction().commit(); s.close(); deleteData(); }
public void testStraightFilters() { createData(); FullTextSession s = Search.getFullTextSession(openSession()); s.getTransaction().begin(); BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD); FullTextQuery ftQuery; ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("bestDriver"); Filter dateFilter = new TermRangeFilter("delivery", "2001", "2005", true, true); ftQuery.setFilter(dateFilter); assertEquals("Should select only liz", 1, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.setFilter(dateFilter); ftQuery.enableFullTextFilter("bestDriver"); ftQuery.enableFullTextFilter("security").setParameter("login", "andre"); ftQuery.disableFullTextFilter("security"); ftQuery.disableFullTextFilter("bestDriver"); ftQuery.setFilter(null); assertEquals("Should not filter anymore", 3, ftQuery.getResultSize()); s.getTransaction().commit(); s.close(); deleteData(); }
public void testCache() { createData(); FullTextSession s = Search.getFullTextSession(openSession()); s.getTransaction().begin(); BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD); FullTextQuery ftQuery = s.createFullTextQuery(query, Driver.class); assertEquals("No filter should happen", 3, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("cacheresultstest"); assertEquals("Should filter out all", 0, ftQuery.getResultSize()); // HSEARCH-174 - we call System.gc() to force a garbage collection. // Prior to the fix for HSEARCH-174 this would cause the filter to be // garbage collected since Lucene used weak references. System.gc(); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("cacheresultstest"); try { ftQuery.getResultSize(); } catch (IllegalStateException e) { fail("Cache results does not work"); } ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("cacheinstancetest"); assertEquals("Should filter out all", 0, ftQuery.getResultSize()); ftQuery = s.createFullTextQuery(query, Driver.class); ftQuery.enableFullTextFilter("cacheinstancetest"); try { ftQuery.getResultSize(); fail("Cache instance does not work"); } catch (IllegalStateException e) { // success } s.getTransaction().commit(); s.close(); deleteData(); }
/** * Exclude any items with the given values in the specified field. * * <p>It is a filter applied before the query. * * @param field * @param values * @return the query */ public LuceneQuery<T> exclude(String field, Object[] values) { if (values != null && values.length != 0) { for (Object value : values) { excludeTerms.add(new Term(field, value.toString())); } fullTextQuery .enableFullTextFilter("termsFilterFactory") .setParameter("includeTerms", includeTerms) .setParameter("excludeTerms", excludeTerms); } return this; }
@Test public void testFullTextFilterDefAtMappingLevel() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); Transaction tx = s.beginTransaction(); Address address = new Address(); address.setStreet1("Peachtree Rd NE"); address.setStreet2("Peachtnot Rd NE"); address.setOwner("test"); Calendar c = GregorianCalendar.getInstance( TimeZone.getTimeZone("GMT"), Locale.ROOT); // for the sake of tests c.set(2009, Calendar.NOVEMBER, 15); address.setLastUpdated(c); s.persist(address); address = new Address(); address.setStreet1("Peachtnot Rd NE"); address.setStreet2("Peachtree Rd NE"); address.setLastUpdated(c); address.setOwner("testowner"); s.persist(address); tx.commit(); s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser("id", TestConstants.standardAnalyzer); org.apache.lucene.search.Query luceneQuery = parser.parse("street1:Peachtnot"); FullTextQuery query = s.createFullTextQuery(luceneQuery).setProjection(FullTextQuery.THIS, FullTextQuery.SCORE); query.enableFullTextFilter("security").setParameter("ownerName", "testowner"); assertEquals("expecting 1 results", 1, query.getResultSize()); @SuppressWarnings("unchecked") List<Object[]> results = query.list(); for (Object[] result : results) { s.delete(result[0]); } tx.commit(); s.close(); }
public List<Opportunity> findOpportunities( String position, String location, String skills, String additionalKeyword, String additionalSkillNames) { List OpportunityList = new ArrayList(); SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory(); Session session = sessionFactory.openSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx1 = fullTextSession.beginTransaction(); List<Opportunity> opportunities = session.createQuery("from Opportunity").list(); for (Opportunity opportunity1 : opportunities) { fullTextSession.index(opportunity1); } MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[] {"title", "skillsNeeded", "str_location"}, new StandardAnalyzer()); org.apache.lucene.search.Query q = null; if (!position.equals("") && !skills.equals("") && !location.equals("")) { // when all the values are present try { q = parser.parse( new String[] {position, location, skills}, new String[] {"title", "str_location", "skillsNeeded"}, new StandardAnalyzer()); } catch (ParseException e) { e.printStackTrace(); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else if (!position.equals("") && !location.equals("") && skills.equals("")) { // when only skill value is not present try { q = parser.parse( new String[] {position, location}, new String[] {"title", "str_location"}, new StandardAnalyzer()); } catch (ParseException e) { e.printStackTrace(); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else if (!position.equals("") && location.equals("") && !skills.equals("")) { // search wrt position and skills try { q = parser.parse( new String[] {position, skills}, new String[] {"title", "skillsNeeded"}, new StandardAnalyzer()); } catch (ParseException e) { e.printStackTrace(); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else if (position.equals("") && !location.equals("") && !skills.equals("")) { // search wrt location and skills try { q = parser.parse( new String[] {location, skills}, new String[] {"str_location", "skillsNeeded"}, new StandardAnalyzer()); } catch (ParseException e) { e.printStackTrace(); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else if (!position.equals("") && location.equals("") && skills.equals("")) { // search for position try { // q = parser.parse(new String[] { position },new String[] { "title" }, new // StandardAnalyzer()); String titles = position + "*"; q = parser.parse("title:" + titles); } catch (ParseException e) { e.printStackTrace(); } if (additionalKeyword.equals("")) { org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else { try { FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List list = hibQuery.list(); hibQuery .enableFullTextFilter("keyword") .setParameter("advancedKeyword", additionalKeyword); List filterList = hibQuery.list(); if (list != null) { list.removeAll(filterList); filterList.addAll(list); list = filterList; } else { list = filterList; } OpportunityList = list; } catch (Exception e) { e.printStackTrace(); } } } else if (position.equals("") && !location.equals("") && skills.equals("")) { // search for location try { q = parser.parse( new String[] {location}, new String[] {"str_location"}, new StandardAnalyzer()); } catch (ParseException e) { e.printStackTrace(); } org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List result = hibQuery.list(); OpportunityList = result; } else if (position.equals("") && location.equals("") && !skills.equals("")) { List result = new ArrayList(); String strSkill = null; if (skills.contains(",")) { strSkill = skills.replaceAll(",", " "); } else { strSkill = skills; } try { q = parser.parse("skillsNeeded:" + strSkill); } catch (ParseException e) { e.printStackTrace(); } try { if (additionalSkillNames.equals("")) { FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List list = hibQuery.list(); result.addAll(list); } else { FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class); List list = hibQuery.list(); hibQuery .enableFullTextFilter("skills") .setParameter("advancedSkills", additionalSkillNames); List filterList = hibQuery.list(); List<Opportunity> result1 = list; if (list != null) { list.removeAll(filterList); filterList.addAll(list); list = filterList; } else { list = filterList; } result.addAll(list); } OpportunityList = result; } catch (Exception e) { e.printStackTrace(); } } tx1.commit(); session.close(); return OpportunityList; }