我有一个全文查询,根据上一个问题的建议,我已使用ID查询进行过滤。由于某种原因,查询无法触发(我确信它在之前触发过,但是...)有人可以看到原因吗?
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
如果我删除了过滤器,等等
query = queryBuilder.bool()
.must(processSearchExpression(searchOption.getSearchExpression(), queryBuilder,
fields.toArray(new String[fields.size()])))
.createQuery();
查询运行,但显然不会按ID进行过滤。过滤器中有ID。当我将showSQL设置为true时,如果没有过滤器,则会看到一个查询,但有过滤器时却没有显示查询(但不会引发错误)
我似乎找不到任何问题吗?!
答案 0 :(得分:1)
我不确定“查询不触发”的含义:
typedQuery.getResultList();
,则肯定会执行查询。无论如何,我认为您的问题是查询不返回任何匹配?这是您编写查询方式的问题。您是这样写的:
BooleanJunction<?> idJunction = queryBuilder.bool();
for (Long id : ids) {
idJunction.must(queryBuilder.keyword().onField("recId").matching(id).createQuery());
}
...”,这意味着“字段'recId'必须具有这些ID值中的每一个”。
应该使用“应该”代替“必须”:
BooleanJunction<?> idJunction = queryBuilder.bool();
for (Long id : ids) {
idJunction.should(queryBuilder.keyword().onField("recId").matching(id).createQuery());
}
...”,这意味着“字段'recId'必须至少具有这些ID值之一”。