-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to index the OneToMany field
PostPosted: Thu Jan 17, 2008 4:29 am 
Newbie

Joined: Fri Dec 28, 2007 7:38 am
Posts: 16
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 11:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I am not 100% sure, but I think you might have to add the '.' to your prefix as well, eg

Code:
@IndexedEmbedded(prefix = "subcategoryId.", depth = 1)


The easiest to have a look at the generated Lucene document field names.

By the way - in case you are always searching on the main class TblProducts there is no need to index other classes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 1:14 am 
Newbie

Joined: Fri Dec 28, 2007 7:38 am
Posts: 16
Thank u Mr. hardy.ferentschik
Your reply is very useful to me
Now the search is workes fine

By
Thiagu.m


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 7:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
One more thing. Your initial indexing routine (that you probably copied from the DVDStore example) will not scale for lot's of rows. Check the reference documentation on how to index lot's of data efficiently.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.