I was just created a hibernate full text search using hibernate-search-4.1.1.Final.jar and all runtime dependencies. There is no errors in this application. But my Lucene query unsing the query DSL doesn't return any results. I mean It doesn't return any of rows in the table. can any one please help me.
Main Search program This Java code is used to perform hibernate full text search.
public class MainSearch { public static void main(String args[]) { Iterator iterator; Session session = HibernateUtil.getSession(); // FullTextSession fullTextSession = Search.getFullTextSession(session);
FullTextSession fullTextSession = Search.getFullTextSession(session); org.hibernate.Transaction tx = fullTextSession.beginTransaction();
// create native Lucene query unsing the query DSL // alternatively you can write the Lucene query using the Lucene query // parser // or the Lucene programmatic API. The Hibernate Search DSL is // recommended though QueryBuilder qb = fullTextSession.getSearchFactory() .buildQueryBuilder().forEntity(Book.class).get(); org.apache.lucene.search.Query query = qb.keyword() .onFields("title", "subtitle", "authors.name").matching("cpp") .createQuery();
// wrap Lucene query in a org.hibernate.Query org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, Book.class);
// execute search
List result = hibQuery.list(); iterator = result.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } System.out.println(); // Check list empty or not if (result.isEmpty()) { System.out.println("Linked list is empty"); }
tx.commit(); session.close(); } }
Book.java Hibernate pojo class for mapping book table. @Entity @Indexed @Table(name = "book") public class Book {
@Id @GeneratedValue private Integer id;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO) private String title;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO) private String subtitle;
@IndexedEmbedded @ManyToMany private Set<Author> authors = new HashSet<Author>();
public Book() { }
@Column(name = "id") public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "tittle") public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
@Column(name = "subtittle") public String getSubtitle() { return subtitle; }
public void setSubtitle(String subtitle) { this.subtitle = subtitle; }
public Set<Author> getAuthors() { return authors; }
public void setAuthors(Set<Author> authors) { this.authors = authors; } public String toString() { StringBuilder stringBuilder = new StringBuilder("Id: ").append(this.getId()).append(" | Tittle:").append(this.getTitle()).append(" | SubTittle:").append(this.getSubtitle());
return stringBuilder.toString(); }
}
**Author.java** Hibernate pojo class for mapping author table
@Entity @Table(name = "author") public class Author {
@Id @GeneratedValue private Integer id;
@Field private String name;
public Author() { }
@Column(name = "id") public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@Column(name = "name") public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
**hibernate.cfg.xml**
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <session-factory> <property name="show_sql">false</property> <property name="format_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hybernet</property> <property name="connection.username">root</property> <property name="connection.password">rot</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.search.default.directory_provider">filesystem</property> <property name="hibernate.search.default.indexBase">file:///home/lijo/lijo</property>
<mapping class="example.Author"/> <mapping class="example.Book"/> </session-factory> </hibernate-configuration>
can any one please help me..
|