问题
I am doing hibernate lucene search .It is working fine with getting entire domain object.But my requirement is projections.It only getting single value on OneToMany associated fields.So how can I get all values of @IndexedEmbedded fields with projections.Please assists your suggestions. The following snippet is my code
@Indexed(index="Skills")
@AnalyzerDef(name = "skillAnalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "English")
})
})
@Entity
@Table(name = "skills")
public class Skills {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "skill_id")
@Field(name="skillIdPk",index=Index.YES, analyze=Analyze.YES, store=Store.YES)
private int skillId;
@Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
@Column(name = "skill")
private String skill;
@Column(name = "skill_type")
private String skillType = "default";
//setters & getters
}
@Indexed(index = "JobSeeker")
@AnalyzerDef(name = "jobSeekerAnalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "English") }) })
@Entity
@Table(name = "jobseeker")
@Component
public class JobSeeker {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "jobseeker_id")
private long jobSeekerId;
@Column(name = "email_id", unique = true)
private String emailId;
@Column(name = "first_name")
private String firstName;
@Column(name = "middle_name")
private String middleName;
@Column(name = "last_name")
private String lastName;
@Column(name = "password")
private String password;
@IndexedEmbedded
private Set<JobSeekerSkills> jobSeekerSkills = new HashSet<JobSeekerSkills>();
//setters & getters
}
@Indexed(index="JobSeekerSkills")
@AnalyzerDef(name = "jobseekerSkillAnalyzer",
tokenizer = @TokenizerDef(factory=StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "English")
})
})
@Entity
@Table(name="jobseeker_skills")
public class JobSeekerSkills
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="jobseeker_skill_id")
private long jobSeekerSkillId;
@ManyToOne
@JoinColumn(name="jobseeker_jobseeker_id")
private JobSeeker jobSeeker;
@ManyToOne
@JoinColumn(name="skills_skill_id")
@IndexedEmbedded
private Skills skills;
//setters & getters
}
//=======================================================================
//This is my Dao code
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(JobSeeker.class).get();
Query query = null;
for (String string : skills) {
query = qb.keyword().onField("jobSeekerSkills.skills.skillIdPk").matching(Integer.parseInt(string)).createQuery();
}
org.hibernate.search.FullTextQuery query1 =
fullTextSession.createFullTextQuery(query, JobSeeker.class);
query1.setProjection("jobSeekerId", "jobSeekerSkills.skills.skill");
List results = query1.list();
for (int i = 0; i < results.size(); i++) {
Object[] object = (Object[]) results.get(i);
System.out.println(object[0]);
System.out.println(object[1]);
}
// List<Skills> authorName1 = (List<Skills>) firstResult[1];
// System.out.println(authorName1);
return results;
回答1:
As mentioned in the Hibernate Search documentation:
projection does not work on collections or maps which are indexed via @IndexedEmbedded
So in your case, trying to project on jobSeekerSkills.skills.skill
, with jobSeekerSkills
being a collection indexed via @IndexedEmbedded
, is not a good idea.
I'd like to point out that, since you're only performing a keyword query on a numeric field, you'd probably be better off just doing a classic JQPL/SQL query using Hibernate ORM.
Anyway, if for whatever reason you really want to use Hibernate Search for this, judging from your query, you don't need to project on a collection. Instead, just add an @IndexedEmbedded(includePaths = "includePaths")
to the jobSeeker
property of JobSeekerSkills
, and then rework your query to target the index for entity JobSeekerSkills
:
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(JobSeeker.class).get();
Query query = null;
for (String string : skills) {
query = qb.keyword().onField("skills.skillIdPk").matching(Integer.parseInt(string)).createQuery();
}
org.hibernate.search.FullTextQuery query1 =
fullTextSession.createFullTextQuery(query, JobSeeker.class);
query1.setProjection("jobSeeker.jobSeekerId", "skills.skill");
List results = query1.list();
for (int i = 0; i < results.size(); i++) {
Object[] object = (Object[]) results.get(i);
System.out.println(object[0]);
System.out.println(object[1]);
}
// List<Skills> authorName1 = (List<Skills>) firstResult[1];
// System.out.println(authorName1);
return results;
Note: the includePaths
on the new @IndexedEmbedded
is only necessary because you already have an @IndexedEmbedded
on the reverse side of the association (JobSeeker.jobSeekerSkills
); it avoids infinite recursion (JobSeeker.jobSeekerSkills.jobSeeker.jobSeekerSkills.jobSeeker.jobSeekerSkills...). If you remove the @IndexedEmbedded
on JobSeeker.jobSeekerSkills
, then you can also remove the includePaths
on the new @IndexedEmbedded
.
来源:https://stackoverflow.com/questions/43370523/how-to-use-projections-with-hibernate-lucene-search