Reputation: 2130
I have a fulltext query which under advice from a previous question I have filtered with an Id query. For some reason the query fails to fire (I'm sure it fired before but...) Can anyone see why ?
Class<?> entityClass = entityType.getJavaType();
List<String> fields = buildFields(entityType.getProperties());
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
QueryContextBuilder queryContextBuilder = searchFactory.buildQueryBuilder();
QueryBuilder queryBuilder = queryContextBuilder.forEntity(entityClass).get();
CriteriaBuilder idBuilder = m_storage.getCriteriaBuilder();
CriteriaQuery idQuery = idBuilder.createQuery(entityClass);
Root from = idQuery.from(entityClass);
idQuery.select(from.get("OID"));
Subquery<Long> subquery = idQuery.subquery(Long.class);
Root dateSQ = subquery.from(entityClass);
subquery.select(idBuilder.max(dateSQ.get("modDateTime")));
subquery.groupBy(dateSQ.get("surname"));
idQuery.where(idBuilder.in(from.get("modDateTime")).value(subquery));
TypedQuery typedQuery = m_storage.createQuery(idQuery);
List<Long> ids = typedQuery.getResultList();
BooleanJunction<?> idJunction = queryBuilder.bool();
for (Long id : ids) {
idJunction.must(queryBuilder.keyword().onField("recId").matching(id).createQuery());
}
SearchOption searchOption = uriInfo.getSearchOption();
Query query = queryBuilder.bool()
.must(processSearchExpression(searchOption.getSearchExpression(), queryBuilder,
fields.toArray(new String[fields.size()]))).filteredBy(idJunction.createQuery())
.createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, entityClass);
return fullTextQuery;
//fullTextQuery fails to run
If I remove the filter and so
query = queryBuilder.bool()
.must(processSearchExpression(searchOption.getSearchExpression(), queryBuilder,
fields.toArray(new String[fields.size()])))
.createQuery();
The query runs but obviously doesn't filter by ids. There are ids in the filter. When I set a showSQL to true I see a query if I don't have the filter but I don't get a query displayed when I have the filter (but no error is thrown)
I can't seem to find any problem ?!
Upvotes: 0
Views: 106
Reputation: 9977
I'm not sure what you mean by "the query doesn't fire":
typedQuery.getResultList();
, a query was definitely executed.Anyway, I assume your problem is rather that the query does not return any hit? That's a problem with the way you wrote your query. You wrote this:
BooleanJunction<?> idJunction = queryBuilder.bool();
for (Long id : ids) {
idJunction.must(queryBuilder.keyword().onField("recId").matching(id).createQuery());
}
... which means "the field 'recId' must have every single one of these id values".
Instead of "must", you should use "should":
BooleanJunction<?> idJunction = queryBuilder.bool();
for (Long id : ids) {
idJunction.should(queryBuilder.keyword().onField("recId").matching(id).createQuery());
}
... which means "the field 'recId' must have at least one of these id values".
Upvotes: 1