# Using a subquery in a hibernate FullTextEntityManager query

**URL:** <https://discourse.hibernate.org/t/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/2752>\
**Category:** Hibernate Search\
**Created:** [May 28, 2019, 3:40pm UTC](https://discourse.hibernate.org/t/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/2752 "2019-05-28T15:40:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gringogordo](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gringogordo](https://discourse.hibernate.org/u/gringogordo)\
**Post date:** [May 28, 2019, 3:40pm UTC](https://discourse.hibernate.org/t/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/2752/1 "2019-05-28T15:40:17Z")

</div>

Apologies if this is a duplicate question, I have seen [this](https://discourse.hibernate.org/t/jpa-query-sub-filter-with-hibernate-search/2259) and it comes close to what I am asking I think -  
but the suggested solution of building a list of id’s seems a little impractical to me and so I hope there may be a slightly different slant to my question !

I have a set of tables which contain their own previous version records and I am trying to modify some existing code using a hibernate FullTextEntityManager query. Essentially it works currently against all records in the given table but I would like to constrain the results to a subset of records in a table (the most recent of a given type).

So for example the underlying query of data to search might be something along these lines

```auto
> SELECT name, address 
> FROM Persons p
> WHERE p.name = sq.name
> FROM 
> (SELECT name, max(datemodified)
> FROM Persons 
> GROUP BY name) sq

```

Currently the java code is just selecting from the raw table (essentially this is what the forEntity option does I think)

```auto
> FullTextEntityManager ftem = Search.getFullTextEntityManager(getEntityManager());
> 
> SearchFactory sf= ftem.getSearchFactory();
> QueryContextBuilder qcb = sf.buildQueryBuilder();
> QueryBuilder qb= qcb.forEntity(entityClass).get();
> 
> //processSearchExpression builds a lucene style full text search
> org.apache.lucene.search.Query q= processSearchExpression();
> 
> FullTextQuery ftq= ftem.createFullTextQuery(q, entityClass);

```

What I can’t quite work out is how to add a sub query or something producing similar functionality so that I can just query the most recent records of each type ? Or whether this is even possible ?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [May 29, 2019, 10:29am UTC](https://discourse.hibernate.org/t/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/2752/2 "2019-05-29T10:29:10Z")

</div>

Hello,

I answered you question on stack overflow: [https://stackoverflow.com/questions/56344916/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/56358069#56358069](https://stackoverflow.com/questions/56344916/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/56358069#56358069)

Let’s continue the discussion there if necessary.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [May 29, 2019, 10:29am UTC](https://discourse.hibernate.org/t/using-a-subquery-in-a-hibernate-fulltextentitymanager-query/2752/3 "2019-05-29T10:29:15Z")

</div>


