我是新手来休眠Lucene搜索的。在我的应用程序中,我有一些旧数据我知道hibernate Lucene不知道事务。因此对于那些旧数据,我手动创建了索引,因此它像魅力一样工作,但它没有为新插入/更新的记录提供结果。但是,如果我在这些记录即将到来时再次进行手动索引。我认为通过hibernate ORM索引插入/更新的记录会在我正确的时候自动更新。如果是,则表示它为什么不来。请帮我一些建议。
这是我的代码段
fullTextSession.createIndexer().startAndWait();
我的搜索代码:
MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] { "skills.skill","role" },
new StandardAnalyzer());
parser.setDefaultOperator(Operator.OR);
org.apache.lucene.search.Query luceneQuery = null;
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Student.class).get();
BooleanQuery boolQuery = new BooleanQuery();
boolQuery.add(qb.keyword().onField("vstatus").matching(1).createQuery(), BooleanClause.Occur.MUST);
boolQuery.add(qb.range().onField("percentage").from(80).to(100).createQuery(),
BooleanClause.Occur.MUST);
FullTextQuery createFullTextQuery = fullTextSession.createFullTextQuery(boolQuery, Student.class);
createFullTextQuery.setProjection("studentId", "firstName","lastName","personal.city",);
return createFullTextQuery.list();
以下是我的制图实体
@Entity
@Table(name="student")
@Component
@Indexed(index="student")
public class Student
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="student_id")
private long studentId;
@Column(name="first_name")
@Field(store=Store.YES)
private String firstName;
@Column(name="last_name")
@Field(store=Store.YES)
private String lastName;
@Column(name="visibility_status",columnDefinition="TINYINT")
@Field(analyze=Analyze.YES,index=Index.YES)
private int vstatus;
@OneToOne(mappedBy="student",cascade=CascadeType.ALL)
@IndexedEmbedded
private ProfileSummary profileSummary;
@OneToOne(mappedBy="student",cascade=CascadeType.ALL)
@IndexedEmbedded
private PersonalInfo personal;
@OneToMany(mappedBy="student",cascade=CascadeType.ALL)
@IndexedEmbedded
private List<StudentSkills> StudentSkills = new ArrayList<StudentSkills>();
//setters & getters
}
========================
@Entity
@Table(name="profile_summary")
@Indexed(index="ProfileSummary")
@AnalyzerDef(name = "summaryAnalyzer",
tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
})
public class ProfileSummary implements Serializable
{
private static final long serialVersionUID = 4672310608280406464L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="profile_summary_id")
private long ProfileSummaryId;
@OneToOne
@JoinColumn(name="student_student_id")
private Student student
@Column(name="profile_percentage")
@Analyzer(definition = "summaryAnalyzer")
@Field(index=Index.YES, store=Store.NO)
private int percentage ;
}
@Entity
@Table(name="student_skills")
public class StudentSkills
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="student_skill_id")
private long studentSkillId;
@ManyToOne
@JoinColumn(name="student_student_id")
private Student student
@ManyToOne
@JoinColumn(name="skills_skill_id")
@IndexedEmbedded
private Skills skills;
}
类似我添加了技能索引
答案 0 :(得分:1)
我遇到了同样的问题。我认为这也是其中一个原因。它将取决于保存实体。如果你在你的情况下保存主要实体,即 Student.class @IndexedEmbedded够。就像你在孩子上执行Hibernate操作意味着你必须通过使用@InededEmbedded和@ ContainedIn建立双向关系。我认为你错过了@ContainedIn在关系的另一面,这就是索引没有更新的原因
在个人资料摘要实体中以及在StudentSkill实体上:
@OneToOne
@JoinColumn(name="student_student_id")
@ContainedIn
private Student student
答案 1 :(得分:0)
hibernate.search.indexing_strategy
配置属性。如果它设置为manual
,则表示您已禁用自动索引。您应该将其设置为event
。EntityManager
/ Session
的代码)以及如何管理交易(Hibernate Search)通常在提交事务时触发索引)