Hibernate Search in a JPA application
来源:互联网 发布:资产管理公司 知乎 编辑:程序博客网 时间:2024/05/16 09:16
1:I downloaded the Hibernate Search JARs and documentation from http://search.hibernate.org, and added them to my project. This PDF is an excellect source of documentation for using the Hibernate Search library with either JPA or the default Hibernate interfaces.
2:I added the following Search properties to the Hibernate configuration. Since I'm using JPA, I added these properties to the persistence-unit element in persistence.xml (if you're using classic Hibernate, these would get added to hibernate.properties or hibernate.cfg.xml).
<property name="hibernate.search.default.directory_provider"value="org.hibernate.search.store.FSDirectoryProvider" />
<property name="hibernate.search.indexing_strategy" value="manual" />
<property name="hibernate.search.default.indexBase" value="/var/searchindex" />
3:I added Hibernate Search annotations to my JPA entities so that the Search library can properly index and search them. Here's an example of my Song class, before and after adding the annotations
@Entity@Table(name="song")
public class Song implements Serializable {
private static final long serialVersionUID = 1L; // yeah, lazy, I know
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 100, nullable = false)
private String name;
@Column(name = "lyrics", length=5000, nullable = true)
private String lyrics;// other attributes and methods}
4:After (new annotations added at the top):
@Indexed
@Entity
@Table(name="song")
public class Song implements Serializable {
private static final long serialVersionUID = 1L; // yeah, lazy, I know
@DocumentId // the equivalent of @Id for the Hibernate Search indexes
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Field(index = Index.TOKENIZED, store = Store.NO)
@Boost(value = 2.0f) // boosts the importance of the song name when searching
@Column(name = "name", length = 100, nullable = false)
private String name;
@Field(index = Index.TOKENIZED, store = Store.NO)
@Column(name = "lyrics", length=5000, nullable = true)
private String lyrics;// other attributes and methods
}
@Transactional
public class SongDAO extends JpaDaoSupport implements SongService {
@SuppressWarnings("unchecked")public void updateIndexes()
{final List songs = getJpaTemplate().find("select s from Song s");getJpaTemplate().execute(new JpaCallback() {
@Overridepublic Object doInJpa(EntityManager em) throws PersistenceException {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
for (Song song : songs) {
fullTextEntityManager.index(song);
}fullTextEntityManager.getSearchFactory().optimize(Song.class);return null;}});
}// other methods
}
@Transactional
public class SongDAO extends JpaDaoSupport implements SongService {
@SuppressWarnings("unchecked")
public List search(final String searchString) {
final String[] fields = new String[]{"name", "lyrics"}; // search on these fields
return (List) getJpaTemplate().execute(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
List results = new ArrayList();
try {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
parser.setDefaultOperator(QueryParser.AND_OPERATOR); // overrides the default OR_OPERATOR, so that all words in the search are required
org.apache.lucene.search.Query query = parser.parse(searchString);
Query fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Song.class);
results = fullTextQuery.getResultList();
}
catch (Exception e) {
throw new PersistenceException(e);
}
return results;
}
});
}
}
- Hibernate Search in a JPA application
- Using Second Level Caching in a JPA Application
- mappedBy in Hibernate/JPA
- Hibernate Search in Action
- Multi-Column Join in Hibernate/JPA Annotations
- Hibernate + JPA + Spring MVC integrated Web Application - 4 easy steps
- [LeetCode] Search in Rotated Sorted Array、Search for a Range、Search Insert Position、Search in Rotate
- 18.2 Log4J in a Web Application
- CSplitterWnd in a Dialog based Application
- csplitterwnd in a dialog based application
- Writing a Big Application in Ext
- Using a Console in Winform Application
- Use RSClientPrint control in a custom application
- Hosting Office in a WPF Application
- Writing a Big Application in Ext
- integrate Barcode4J in a Java application
- using Camel in a Web Application
- Integrating FlywayDB In A Spring Framework Application
- android adb am命令
- @implementation NSString (URL)
- jQuery学习笔记1-4
- MarkdownPad中使用中文
- Spring AOP+ehCache简单缓存系统解决方案
- Hibernate Search in a JPA application
- 常用的数据库管理SQL语句(二)
- ABAP中Char類型數據轉換成Decimal類型
- Silverlight导出excel的另一种方式
- hdu 1253 胜利大逃亡
- 7种员工最受赏识
- 浅析C++中内存分配的方式
- jQuery学习笔记1-5
- Asp.Net自定义错误页心得介绍