[jboss-user] [JBoss Seam] - Re: Help!!! Seam with Hibernate Search problem
ljp19820721
do-not-reply at jboss.com
Thu Oct 25 02:06:13 EDT 2007
Here is the source code.
/**
*
* @author lv.jianping
*/
@Stateful
@Name(value = "search")
public class SearchBean implements SearchLocal, Serializable {
@PersistenceContext
private EntityManager em;
int pageSize = 15;
int currentPage = 0;
boolean hasMore = false;
int resultsNumber = 0;
String searchQuery;
@DataModel
List searchResults;
@DataModelSelection
@Out(required = false)
Content selectedContent;
public Content getSelectedContent() {
return selectedContent;
}
public void getSearchResults() {
doSearch();
}
public String getSearchQuery() {
return searchQuery;
}
public void setSearchQuery(String searchQuery) {
this.searchQuery = searchQuery;
}
public int getResultsNumber() {
return resultsNumber;
}
public String doSearch() {
currentPage = 0;
updateResults();
return "searchResults";
}
public void doIndex() {
FullTextEntityManager ftEm = Search.createFullTextEntityManager(em);
List contents = em.createQuery("select c from Content c where c.isdeleted='0' and c.contentstate='1' ").getResultList();
for (Object object : contents){
ftEm.index((Content)object);
}
}
public void nextPage() {
if (!isLastPage()) {
currentPage++;
updateResults();
}
}
public void prePage() {
if (!isFirstPage()) {
currentPage--;
updateResults();
}
}
public boolean isLastPage() {
return (searchResults != null) && !hasMore;
}
public boolean isFirstPage() {
return (searchResults != null) && (currentPage == 0);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = 10;
}
private javax.persistence.Query searchQuery(String searchQuery) throws ParseException {
Map<String, Float> boostPerField = new HashMap<String, Float>();
boostPerField.put("contenttitle", 4f);
boostPerField.put("contentbody", 2f);
boostPerField.put("contentbodysms", 2f);
String[] contentFields = {"contenttitle", "contentbody", "contentbodysms"};
QueryParser parser = new MultiFieldQueryParser(contentFields, new StandardAnalyzer(), boostPerField);
parser.setAllowLeadingWildcard(true);
org.apache.lucene.search.Query luceneQuery = parser.parse(searchQuery);
FullTextEntityManager ftEm = Search.createFullTextEntityManager(em);
//********Here Exception is throwed out*************//
javax.persistence.Query query = ftEm.createFullTextQuery(luceneQuery, Content.class);
return query;
}
private void updateResults() {
Query query;
try {
query = searchQuery(searchQuery);
} catch (ParseException pe) {
return;
}
@SuppressWarnings(value = "unchecked")
List items = query.setMaxResults(pageSize + 1).setFirstResult(pageSize * currentPage).getResultList();
resultsNumber = items.size();
if (items.size() > pageSize) {
searchResults = new ArrayList(items.subList(0, pageSize));
hasMore = true;
} else {
searchResults = items;
hasMore = false;
}
}
@Destroy
@Remove
public void destroy() {
}
}
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4098664#4098664
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4098664
More information about the jboss-user
mailing list