Hi Sanne,
Here is the complete scenario
Step 1:Create a table : author TABLE : author ------------------------------------------- | id | NUMBER (NOT NULL) | | author_name | varchar2(10) | | book_name | varchar2(10) | | publisher | varchar2(10) | --------------------------------------------
Step 2: Insert values into the table
insert into author values (1,'phil','book1','pub1'); insert into author values (2,'rick','garden','pub2'); insert into author values (3,'joseph','city life','pub2'); insert into author values (4,'amanda','rose','pub2'); insert into author values (5,'lee','库控专员','pub3'); insert into author values (6,'yong','Dama 专员','pub4');
Step 3: Create entity @Entity @Table(name = "author") @Indexed public class Requisition {
private int id; private String author_name; private String book_name; private String publisher;
@Id @DocumentId public int getId() { return id; } public void setId(int id) { this.id = id; } @Field(index = Index.TOKENIZED, store = Store.YES) public String getAuthor_name() { return author_name; } public void setAuthor_name(String author_name) { this.author_name = author_name; } @Field(index = Index.TOKENIZED, store = Store.YES) public String getBook_name() { return book_name; } public void setBook_name(String book_name) { this.book_name = book_name; } @Field(index = Index.TOKENIZED, store = Store.YES) public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; }
}
Step 4: Index the entity by the clause : select * from author
Step 5: In the search class
public class SearchDAO {
@PersistenceContext(unitName = "SAMPLE_ENTITY") protected EntityManager entityManager; private FullTextSession fullTextSession;
public EntityManager getEntityManager() { return entityManager; }
public void search(String keyword) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager()); Session session = (Session) fullTextEntityManager.getDelegate(); this.fullTextSession = org.hibernate.search.Search.getFullTextSession(session); BooleanQuery boolquery = new BooleanQuery(); org.apache.lucene.search.Query[] queries = new org.apache.lucene.search.Query[2];
queries[0] = new TermQuery(new Term("author_name", keyword)); queries[1] = new TermQuery(new Term("book_name", keyword));
org.apache.lucene.search.Query termQueryAll = new TermQuery(null); org.apache.lucene.search.Query holder = termQueryAll.combine(queries); booleanQuery.add(holder, BooleanClause.Occur.MUST); org.hibernate.search.FullTextQuery hibernateQuery= fullTextSession.createFullTextQuery(query, Author.class); hibernateQuery.setSort(new Sort(new SortField("author_name",SortField.STRING), new SortField("book_name",SortField.STRING))) List<Object[]> results =hibernateQuery.list();
} }
The results object do not seem to sort the way I would expect.
|