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!