-->
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.  [ 2 posts ] 
Author Message
 Post subject: Search for entities restricted by associated entities
PostPosted: Tue Jun 24, 2014 6:42 am 
Newbie

Joined: Mon Jun 23, 2014 3:21 pm
Posts: 2
Hi all,

I want to search for all entities of Type A whose name field "name" matches some searchTerm and whose associated Set of bs contains a specific instance of B, identified by id.

Here a sketch of my approach:

Entity A:
Code:
@Indexed
@Entity
public class A {
   @Id
   private String id;
   
   @Field
   private String name;
   
   @ManyToMany
   @IndexEmbedded
   private Set<B> bs;   
}


Entity B:
Code:
@Entity
public class B {
   @Id
   private String id;
   
   @ManyToMany
   @ContainedIn
   private Set<A> as;
}


Function to search for all instances of A whose "name" field matches "searchTerm" and whose Set of B contains a specific b with b.id == bId.
Code:
public List<A> searchForAs(final String bId, String searchTerm) {
   Filter filter = new Filter() {            
      private static final long serialVersionUID = -6169078852495957992L;
      @Override
      public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
         OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
           TermDocs termDocs = reader.termDocs(new Term("bs.id", bId));
           while (termDocs.next()) {
               bitSet.set(termDocs.doc());
           }
           return bitSet;
      }
   };
      
   return search(searchTerm, new String[]{ "name" }, filter);
}

public List<A> search(String searchTerm, String[] entityFields, Filter filter) {
   List<A> resultList = null;
   FullTextEntityManager ftem = Search.getFullTextEntityManager(JpaUtil.getEntityManager());
   try {
      QueryBuilder qb = ftem.getSearchFactory().buildQueryBuilder().forEntity(A.class).get();
      org.apache.lucene.search.Query luceneQuery = qb.keyword().onFields(entityFields).matching(searchTerm).createQuery();
      FullTextQuery query = ftem.createFullTextQuery(luceneQuery, entityClass);
      if (filter != null)
         query.setFilter(filter);         
      resultList = (List<A>) query.getResultList();
   } catch (EmptyQueryException e) {
      e.printStackTrace();
   }
   return resultList;
}


Could you please tell me if the above approach will work? Espacially the part
Code:
new Term("bs.id", bId)
I know that it works for simple instances, that is replace Set<B> by a single B, but does it also work for collections? Did I miss something?

P.S.: I know that query.setFilter is semi-deprecated, however it was the least amount of code to illustrate my question.

Thanks for your help!


Top
 Profile  
 
 Post subject: Re: Search for entities restricted by associated entities
PostPosted: Wed Jun 25, 2014 3:37 am 
Newbie

Joined: Mon Jun 23, 2014 3:21 pm
Posts: 2
Anyone from the "experts" has an idea? I looked through the unit tests but could not find a matching example.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.