my entity bean classes are
Code:
@Table(name = "TBL_PRODUCTS")
@Indexed
public class TblProducts implements Serializable{
@Id
@Column(name="PRODUCT_ID")
@DocumentId
private BigDecimal productId;
@Column(name="PRODUCT_NAME")
@Field(index=Index.TOKENIZED)
private String productName;
@ManyToOne
@JoinColumn(name="SUBCATEGORY_ID")
@IndexedEmbedded(prefix = "subcategoryId", depth = 1)
private TblSubCategory subcategoryId;
@ManyToOne
@JoinColumn(name="BRAND_ID")
@IndexedEmbedded(prefix = "brandId", depth = 1)
private TblBrand brandId;
@ManyToOne
@JoinColumn(name="CATEGORY_ID")
@IndexedEmbedded(prefix = "categoryId", depth = 1)
private TblCategory categoryId;
}
-----------------------------------------------------------
@Table(name = "TBL_SUBCATEGORY")
@Indexed
public class TblSubCategory implements Serializable{
@Id
@Column(name="SUBCATEGORY_ID")
@DocumentId
private BigDecimal subcategoryId;
@Column(name="SUBCATEGORY_NAME")
@Field(index=Index.UN_TOKENIZED)
private String subcategoryName;
@OneToMany(mappedBy="subcategoryId")
@ContainedIn
private Set<TblProducts> tblProductsCollection;
}
--------------------------------------------------------------------
@Table(name = "TBL_CATEGORY")
@Indexed
public class TblCategory implements Serializable{
@Id
@Column(name="CATEGORY_ID")
@DocumentId
private BigDecimal categoryId;
@Column(name="CATEGORY_NAME")
@Field(index=Index.UN_TOKENIZED)
private String categoryName;
@OneToMany(mappedBy="categoryId")
@ContainedIn
private Set<TblProducts> tblProductsCollection;
}
---------------------------------------------------------------------
@Table(name = "TBL_BRAND")
@Indexed
public class TblBrand implements Serializable{
@Id
@Column(name="BRAND_ID")
@DocumentId
private BigDecimal brandId;
@Column(name="BRAND_NAME")
@Field(index=Index.UN_TOKENIZED)
private String brandName;
@OneToMany(mappedBy="brandId")
@ContainedIn
private Set<TblProducts> tblProductsCollection;
}
my initial indexing action is
Code:
@Stateful
@Scope(ScopeType.APPLICATION )
@Startup
public class IndexerAction implements Indexer
{
@PersistenceContext
private EntityManager em;
@Create
public void index()
{
indexAllClasses(TblBrand.class,TblCategory.class,TblSubCategory.class);
indexTblProducts();
}
@SuppressWarnings("unchecked")
private void indexTblProducts()
{
FullTextSession fullTextSession = getFullTextSession();
List results = fullTextSession.createCriteria(TblProducts.class)
.list();
for (Object obj : results)
{
fullTextSession.index(obj);
}
}
private FullTextSession getFullTextSession()
{
return (FullTextSession) em.getDelegate();
}
@SuppressWarnings("unchecked")
private void indexAllClasses(Class... entityTypes)
{
FullTextSession fullTextSession = getFullTextSession();
for (Class entityType : entityTypes)
{
for (Object obj : fullTextSession.createCriteria(entityType).list())
{
fullTextSession.index(obj);
}
}
}
@Remove
@Destroy
public void stop() {}
}
This is my search action (Session bean) class
Code:
@Stateful
@Name("search")
public class SearchAction implements Search, Serializable
{
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;
String searchString="Nokia";
org.apache.lucene.search.Query luceneQuery;
@Begin(join=true)
public String doSearch() {
FullTextQuery query;
try
{
query = searchQuery(searchString);
}
catch (ParseException pe)
{
return null;
}
List<TblProducts> searchReasult=query.getResultList();
}
private FullTextQuery searchQuery(String searchQuery) throws ParseException
{
Map<String,Float> boostPerField = new HashMap<String,Float>();
boostPerField.put("productName", 4f);
boostPerField.put("subcategoryId.subcategoryName", 2f);
boostPerField.put("categoryId.categoryName", 2f);
boostPerField.put("brandId.brandName", 2f);
String[] productFields = {"productName", "subcategoryId.subcategoryName","categoryId.categoryName","brandId.brandName"};
QueryParser parser = new MultiFieldQueryParser(productFields, new StandardAnalyzer(), boostPerField);
parser.setAllowLeadingWildcard(true);
luceneQuery = parser.parse(searchQuery);
return ( (FullTextEntityManager) em ).createFullTextQuery(luceneQuery, TblProducts.class);
}
}
The problem is, the search does not take brand, category, and subcategory names
please help me how to resolve this problem
By
thiagu.m