/** * Returns a filter that forces a given field to be one of the given values */ public static Filter terms(final String field, final Collection<?> values) { if (CollectionUtils.isEmpty(values)) { return null; } final TermsFilter filter = new TermsFilter(); int count = 0; for (final Object object : values) { final String term = object == null ? null : StringUtils.trimToNull("" + object); if (term != null) { filter.addTerm(new Term(field, term)); count++; } } return count == 0 ? null : filter; }
/** * Returns a filter that forces a given field to be one of the given values */ public void addTerms(final String field, final Collection<?> values) { if (CollectionUtils.isEmpty(values)) { return; } boolean used = false; final TermsFilter filter = new TermsFilter(); for (final Object object : values) { final String term = CoercionHelper.coerce(String.class, object); if (StringUtils.isNotEmpty(term)) { filter.addTerm(new Term(field, term)); used = true; } } if (used) { add(filter); } }
/** * The termMap keys are facetField terms, and the values are the occurrances * of terms * *@param field a facetField (aka indexed field) *@return The termMap value */ Map getTermMap(String field) { Map termMap = new TreeMap(); // get a count for each term of termList // prtln ("getTermMap() field: " + field); List termList = this.index.getTerms(field); /* * Note: termlist contains INDEX-WIDE occurances for a given term, * so we don't add any 0-size results to the term list */ for (Iterator termIter = termList.iterator(); termIter.hasNext(); ) { String term = (String) termIter.next(); // prtln(" term: " + term); Filter finalFilter = null; // Query method: //Query query = getFacetQuery(field, term); //ResultDocList results = rm.getIndex().searchDocs(query); // Filter method: TermsFilter termsFilter = new TermsFilter(); termsFilter.addTerm(new Term(field,term)); if(this.existingFilter != null) { BooleanFilter booleanFilter = new BooleanFilter(); booleanFilter.add(new FilterClause(termsFilter,BooleanClause.Occur.MUST)); booleanFilter.add(new FilterClause(this.existingFilter,BooleanClause.Occur.MUST)); finalFilter = booleanFilter; } else { finalFilter = termsFilter; } ResultDocList results = rm.getIndex().searchDocs(this.booleanQuery,finalFilter); if (results != null && !results.isEmpty()) termMap.put(term, results.size()); else termMap.put(term, 0); } return termMap; }
public SearchResult search(Long listId, String query, int skip, int count) throws NotFoundException, PermissionException, SearchException { Person me = this.getMe(); // Are we allowed to view archives? MailingList list = this.getListFor(listId, Permission.VIEW_ARCHIVES, me); // We use Hibernate Search for this FullTextEntityManager ftem = Search.getFullTextEntityManager(this.em); QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29, new String[]{"subject", "content"}, new StandardAnalyzer(Version.LUCENE_29)); org.apache.lucene.search.Query luceneQuery; try { luceneQuery = parser.parse(query); } catch (ParseException e) { throw new SearchException(e); } FullTextQuery ftquery = ftem.createFullTextQuery(luceneQuery, Mail.class); ftquery.setFirstResult(skip).setMaxResults(count); TermsFilter filter = new TermsFilter(); filter.addTerm(new Term("list", listId.toString())); ftquery.setFilter(filter); @SuppressWarnings("unchecked") List<Mail> rawHits = ftquery.getResultList(); // Now turn these into real hits List<SearchHit> hits = new ArrayList<SearchHit>(rawHits.size()); for (Mail mail: rawHits) { hits.add(new SearchHit( mail.getId(), mail.getSubject(), list.getPermissionsFor(me).contains(Permission.VIEW_ADDRESSES) ? mail.getFromAddress().getAddress() : null, mail.getFromAddress().getPersonal(), mail.getSentDate() )); } return new SearchResult(ftquery.getResultSize(), hits); }