Skip to content

Commit

Permalink
Support for multi-word full text queries.
Browse files Browse the repository at this point in the history
(cherry picked from commit 5fe57aa)
  • Loading branch information
mederly committed Sep 26, 2017
1 parent 6a0a2dd commit 8044097
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
Expand Up @@ -3654,6 +3654,40 @@ public void test940FullTextSimple() throws Exception {
}
}

@Test
public void test945FullTextMulti() throws Exception {
Session session = open();

try {
ObjectQuery query = QueryBuilder.queryFor(UserType.class, prismContext)
.fullText("\t\nPeter\t\tMravec\t")
.build();

String real = getInterpretedQuery2(session, UserType.class, query);
String expected = "select\n"
+ " u.oid,\n"
+ " u.fullObject,\n"
+ " u.stringsCount,\n"
+ " u.longsCount,\n"
+ " u.datesCount,\n"
+ " u.referencesCount,\n"
+ " u.polysCount,\n"
+ " u.booleansCount\n"
+ "from\n"
+ " RUser u\n"
+ " left join u.textInfoItems t\n"
+ " left join u.textInfoItems t2\n"
+ "where\n"
+ " (\n"
+ " t.text like :text and\n"
+ " t2.text like :text2\n"
+ " )";
assertEqualsIgnoreWhitespace(expected, real);
} finally {
close(session);
}
}

@Test
public void testAdHoc100ProcessStartTimestamp() throws Exception {
Session session = open();
Expand Down
Expand Up @@ -25,6 +25,13 @@
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
* @author mederly
*/
Expand All @@ -37,16 +44,30 @@ public FullTextRestriction(InterpretationContext context, FullTextFilter filter,

@Override
public Condition interpret() throws QueryException {
String textInfoItemsAlias = getItemPathResolver().addTextInfoJoin(getBaseHqlEntity().getHqlPath());
String textPath = textInfoItemsAlias + "." + RObjectTextInfo.F_TEXT;

// TODO implement multiple values
if (filter.getValues().size() != 1) {
throw new QueryException("FullText filter currently supports only a single string");
}
String text = filter.getValues().iterator().next();
String normalized = getContext().getPrismContext().getDefaultPolyStringNormalizer().normalize(text);
normalized = StringUtils.normalizeSpace(normalized);
return getContext().getHibernateQuery().createLike(textPath, normalized, MatchMode.ANYWHERE, false);
String[] words = StringUtils.split(normalized);
if (words.length == 0) {
throw new QueryException("No words to query for using full-text search filter");
}
List<Condition> conditions = new ArrayList<>(words.length);
for (String word : words) {
conditions.add(createWordQuery(word));
}
if (conditions.size() == 1) {
return conditions.get(0);
} else {
return getContext().getHibernateQuery().createAnd(conditions);
}
}

private Condition createWordQuery(String word) throws QueryException {
String textInfoItemsAlias = getItemPathResolver().addTextInfoJoin(getBaseHqlEntity().getHqlPath());
String textPath = textInfoItemsAlias + "." + RObjectTextInfo.F_TEXT;
return getContext().getHibernateQuery().createLike(textPath, word, MatchMode.ANYWHERE, false);
}
}

0 comments on commit 8044097

Please sign in to comment.