如何使用hibernate lucene搜索投影

时间:2017-04-12 13:10:06

标签: java hibernate spring-mvc hibernate-search

我正在做hibernate lucene搜索。它可以正常工作以获取整个域对象。但我的要求是projection.It只在OneToMany关联字段上得到单个值。那么如何通过投影获取@IndexedEmbedded字段的所有值。请帮助你的建议。 以下代码段是我的代码

    @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 个答案:

答案 0 :(得分:2)

Hibernate Search documentation中所述:

  

投影不适用于通过@IndexedEmbedded

编制索引的集合或地图

因此,在您的情况下,尝试投放jobSeekerSkills.skills.skilljobSeekerSkills是通过@IndexedEmbedded索引的集合,这不是一个好主意。

我想指出,由于您只是在数字字段上执行关键字查询,因此使用Hibernate ORM执行经典JQPL / SQL查询可能会更好

无论如何,如果由于某种原因你真的想要使用Hibernate Search,从你的查询来看,你不需要在集合上投影。相反,只需将@IndexedEmbedded(includePaths = "includePaths")添加到jobSeeker的{​​{1}}属性,然后重新编写查询以定位实体JobSeekerSkills的索引:

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; 上的includePaths仅是必需的,因为您已在关联的反面(@IndexedEmbedded)拥有@IndexedEmbedded;它避免了无限递归(JobSeeker.jobSeekerSkills.jobSeeker.jobSeekerSkills.jobSeeker.jobSeekerSkills ...)。如果您移除了JobSeeker.jobSeekerSkills上的@IndexedEmbedded,那么您也可以删除新JobSeeker.jobSeekerSkills上的includePaths