public List<Audit> searchAuditPhrase(int fromRownum, int maxRownums, @NotNull UnaryOperator<Predicates> where) { FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(em); QueryBuilder qb = fullTextEntityManager.getSearchFactory() .buildQueryBuilder() .forEntity(Audit.class) .get(); Function<Matcher<?>, Query> query = m -> qb.phrase() .onField(m.getFieldName()) .sentence(m.getSearchedText() == null ? null : m.getSearchedText().toString()) .createQuery(); BiFunction<MustJunction, Matcher<?>, MustJunction> and = (j, m) -> j.must(query.apply(m)); Predicates p = where.apply(predicates()); MustJunction junction = p.getMatchers() .stream() .reduce(null, (j, m) -> j == null ? qb.bool().must(query.apply(m)) : and.apply(j, m), (a, b) -> a); Query luceneQuery = junction.createQuery(); FullTextQuery ftq = fullTextEntityManager.createFullTextQuery(luceneQuery, Audit.class); Sort sort = toSort(p.getSorters()); if (sort.getSort().length != 0) { ftq.setSort(sort); } return ftq.setFirstResult(fromRownum) .setMaxResults(maxRownums) .getResultList(); }
public List<Audit> searchAuditLike(int fromRownum, int maxRownums, @NotNull UnaryOperator<Predicates> where) { FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(em); QueryBuilder qb = fullTextEntityManager.getSearchFactory() .buildQueryBuilder() .forEntity(Audit.class) .get(); Function<Matcher<?>, Query> query = m -> { Audit e = new Audit(); INVOKER.writeProperty(e, m.getFieldName(), m.getSearchedText()); return qb.moreLikeThis() .comparingField(m.getFieldName()) .toEntity(e) .createQuery(); }; BiFunction<MustJunction, Matcher<?>, MustJunction> and = (j, m) -> j.must(query.apply(m)); Predicates p = where.apply(predicates()); MustJunction junction = p.getMatchers() .stream() .reduce(null, (j, m) -> j == null ? qb.bool().must(query.apply(m)) : and.apply(j, m), (a, b) -> a); Query luceneQuery = junction.createQuery(); FullTextQuery ftq = fullTextEntityManager.createFullTextQuery(luceneQuery, Audit.class); ftq.limitExecutionTimeTo(TIMEOUT, SECONDS); Sort sort = toSort(p.getSorters()); if (sort.getSort().length != 0) { ftq.setSort(sort); } return ftq.setFirstResult(fromRownum) .setMaxResults(maxRownums) .getResultList(); }
private Query buildQuery(String q, Member homeMember, StandingState standingState, boolean standingStateNot) { QueryBuilder queryBuilder = getSearchFactory().buildQueryBuilder() .forEntity(Affiliate.class).get(); //Odd issue with Camel case text not finding exact matches. Workaround using lowercase. Query textQuery = buildWildcardQueryForTokens(queryBuilder, q.toLowerCase()); if (homeMember == null && standingState == null) { return textQuery; } else { BooleanJunction <MustJunction> building = queryBuilder .bool() .must(textQuery); if (homeMember != null) { Query homeMemberQuery = buildQueryMatchingHomeMember(queryBuilder, homeMember); building = building.must(homeMemberQuery); } if (standingState != null) { Query standingStateQuery = buildQueryMatchingStandingState(queryBuilder, standingState); MustJunction standingStateBuilding = building.must(standingStateQuery); if (standingStateNot) { // Caught up in .not() returning boolean rather than must... building = standingStateBuilding.not(); } else { building = standingStateBuilding; } } return building.createQuery(); } }