我想对具有特定于实体的限制的多个实体建立一个休眠查询。理想情况下,所有结果都将从同一查询中返回,以便可以根据相关性对它们进行排序。
实体是事件,组和地点(Postable的每个子类)和配置文件。对事件的限制是它们从今天开始或将来开始。对Postables的限制是它们都具有PostableVisibility.PUBLIC的可见性。
Profile类没有可见性字段,只有Event类具有schedule.startDate。此外,“个人档案”和“可发布物品”具有不同的搜索字段(与“名称”相同)。我已经能够分别创建可见性/开始日期限制,但无法同时应用这两个限制并无法从所有课程中获取结果。
以下是有关域模型的详细信息:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "postables")
public abstract class Postable{
/**
* determines who can see the postable in relation to the creatorId
*/
@Column(name = "visibility", nullable = false)
@Enumerated(EnumType.STRING)
@Field(analyze=Analyze.NO)
protected PostableVisibility visibility;
@Column(name = "name", nullable = false)
@Field
protected String name;
@Column(name = "type", nullable = false)
@Field
protected String type;
@Column(name = "description", nullable = false)
@Field
protected String description;
}
@Entity
@Table(name = "events")
@Indexed
public class Event extends Postable {
@IndexedEmbedded
private Schedule schedule;
}
@Embeddable
public class Schedule {
@Column(name = "end_date")
private LocalDate endDate;
@Column(name = "end_time")
private LocalTime endTime;
@Column(name = "start_date")
@Field
private LocalDate startDate;
@Column(name = "start_time")
@Field
private LocalTime startTime;
}
@Entity
@Table(name = "groups")
@Indexed
public class Group extends Postable {
}
@Entity
@Table(name = "places")
@Indexed
public class Place extends Postable {
}
public class Profile {
@Column(name = "name")
@Field
private String name;
@Column(name = "username")
@Field(analyze= Analyze.NO)
private String username;
}
最后,尝试建立查询。我已经尝试过使用查询生成器的不同组合来完成此操作,但是没有运气。在下面的查询中,所有用户查询,可见性限制和当前事件限制都被应用,但我仅获得事件的结果。
public List<Object> searchAll(String userQueryString) {
int maxResults = 20;
Session session = this.currentSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
// Query builders:
QueryBuilder postableQB = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Postable.class).get();
QueryBuilder eventQB = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Event.class).get();
// Filter queries
Query publicPostableQuery = postableQB.keyword().onField("visibility").matching(PostableVisibility.PUBLIC).createQuery();
String defaultTimeZone = "America/New_York";
LocalDate today = LocalDate.now(ZoneId.of(defaultTimeZone));
Query currentEventQuery = eventQB.range().onField("schedule.startDate").above(today).createQuery();
// User text query
Query userQuery = postableQB.simpleQueryString().onField("name").boostedTo(5f) // working
.andField("type").boostedTo(3f)
.andField("description")
.andField("username").boostedTo(5f)
.matching(userQueryString)
.createQuery();
Query combinedQuery = postableQB.bool()
.must(userQuery)
.must(publicPostableQuery)
.must(currentEventQuery)
.createQuery();
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(combinedQuery, Postable.class, Profile.class);
fullTextQuery.setFirstResult(0).setMaxResults(maxResults);
return fullTextQuery.list();
}
答案 0 :(得分:1)
您当前的查询要求将来所有结果都为startDate
,即使它们不是“事件”也是如此。由于非事件没有startDate
,因此它们不匹配。
表达您想要的内容的一种更好的方法是,要求所有结果中都没有startDate
。这样,没有startDate
的文档也将匹配。可见性也是如此,它不包含在配置文件中。
Query privatePostableQuery = postableQB.bool()
.should(postableQB.keyword().onField("visibility").matching(PostableVisibility.FOLLOWERS).createQuery())
.should(postableQB.keyword().onField("visibility").matching(PostableVisibility.INVITE).createQuery())
.createQuery();
String defaultTimeZone = "America/New_York";
LocalDate today = LocalDate.now(ZoneId.of(defaultTimeZone));
Query pastEventQuery = eventQB.range().onField("schedule.startDate").below(today).excludeLimit().createQuery();
// User text query
Query userQuery = postableQB.simpleQueryString().onField("name").boostedTo(5f) // working
.andField("type").boostedTo(3f)
.andField("description")
.andField("username").boostedTo(5f)
.matching(userQueryString)
.createQuery();
Query combinedQuery = postableQB.bool()
.must(userQuery)
.must(privatePostableQuery).not()
.must(pastEventQuery).not()
.createQuery();