# Querying IndexedEmbedded entities (sometimes) returning wrong result

**URL:** <https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692>\
**Category:** Hibernate Search\
**Created:** [September 11, 2021, 1:00am UTC](https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692 "2021-09-11T01:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimitri](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dimitri/32/513_2.png) [@Dimitri](https://discourse.hibernate.org/u/Dimitri)\
**Post date:** [September 11, 2021, 1:00am UTC](https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692/1 "2021-09-11T01:00:25Z")

</div>

I’m attaching a test case that demonstrates unwanted search results.

I understand that Hibernate Search uses some tricks to support joined entities, using Apache Lucene, but I’m hoping you can suggest a solution.

I’m searching for “Foo” entities where its “Bar” children match TWO fields:  
bar\_name: Bar2  
AND  
bar\_visibility: public

```auto
			QueryBuilder qb = session.getSearchFactory().buildQueryBuilder().forEntity(Foo.class).get();

			BooleanJunction<?> bool = qb.bool();
			bool.must(qb.keyword().onField("bar_name").matching("Bar2").createQuery());
			bool.must(qb.keyword().onField("bar_visibility").matching("public").createQuery());

			org.apache.lucene.search.Query query = bool.createQuery();

```

I only want Foo results where Bars match BOTH of the above criteria (hence my use of bool.must).

But, that’s not what’s happening. I’m seeing Foo results when just ONE of the two “must” criteria match:

```auto
########################
# executing query: +bar_name:bar2 +bar_visibility:public
# 1 results returned:
# Foo: id=1, name=Foo1, bars: [Bar: id=2, name=Bar2, visibility=private, Bar: id=1, name=Bar1, visibility=public]
########################

```

This happens when there are 2 Bar entities associated to a Foo entity. I.e., if you comment one lines

```auto
			Bar bar1 = new Bar(1L, "Bar1", "public", foo1);
			foo1.getBars().add(bar1);
			s.persist(bar1);

```

in “YourTestCase.java”, it correctly returns 0 results, as I’d expect.

---

<div class="post-metadata">

**Author:** ![Dimitri](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dimitri/32/513_2.png) [@Dimitri](https://discourse.hibernate.org/u/Dimitri)\
**Post date:** [September 11, 2021, 1:01am UTC](https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692/2 "2021-09-11T01:01:45Z")

</div>

Sorry, your system is not letting me upload my test case. How should I send it to you?

---

<div class="post-metadata">

**Author:** ![Dimitri](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dimitri/32/513_2.png) [@Dimitri](https://discourse.hibernate.org/u/Dimitri)\
**Post date:** [September 11, 2021, 1:12am UTC](https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692/3 "2021-09-11T01:12:16Z")

</div>

Here’s the ticket: [https://hibernate.atlassian.net/browse/HSEARCH-4314](https://hibernate.atlassian.net/browse/HSEARCH-4314)

---

<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:** [September 13, 2021, 6:47am UTC](https://discourse.hibernate.org/t/querying-indexedembedded-entities-sometimes-returning-wrong-result/5692/4 "2021-09-13T06:47:39Z")

</div>

Copying the answer from the ticket here:

> [@yrodiere](#):
>
> This is expected behavior.
> 
> By default, for performance reasons, objects in Lucene documents are flattened, i.e. the values of all their fields are put together and merged into the root document. So your query:
> 
> ```auto
> Foo results where a child Bar matches BOTH "Bar2" AND "public"
> 
> ```
> 
> Is actually interpreted as:
> 
> ```auto
> Foo results where (a child Bar matches "Bar2") AND (a child Bar matches "public")
> 
> ```
> 
> If you want to preserve the identity of “Bar” children, then you need nested documents, and that’s a feature added in Hibernate Search 6.
> 
> More info in [this section of the documentation](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-indexedembedded-structure).
