The search results do not seem to return newly-created entities

I am using Hibernate Search 5.11 but the search results do not seem to return newly-created entities.

My objects are:

@Indexed
public class objectA {
    @Id
	@Column(name = "id", nullable = false, precision = 18)
	@Field(name = "pkid_sort", bridge = @FieldBridge(impl = BigIntegerNumericFieldBridge.class))
	@Field(name = "pkid")
	private BigInteger id;
	
	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "objectBid", nullable = false)
	**@IndexedEmbedded**
	private objectB objectB;
}

@Indexed
public class objectB  {
    @Id
	@Column(name = "id", nullable = false, precision = 18)
	@Field(name = "pkid_sort", bridge = @FieldBridge(impl = BigIntegerNumericFieldBridge.class))
	@Field(name = "pkid")
	private BigInteger id;
	
	@Column(name = "code", length = 20)
	@Field
	@SortableField
	private String code;
}

Update the objectA with crud repository
JSON of objectA

{
	"id": null,
	"objectB": {
               "id": 1234,
               "code": test
                }
}

objectB already exists in db.

objectARepository.save(objectA);

after i am trying to search the new created objectA with the below code:

Query query = qb.keyword().onField("objectB.code").matching("test").createQuery();
final FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query , ObjectA.class);
fullTextQuery.getResultList();

and it s not working, always return empty list.

It works only, searching with primary key id

 Query query = qb.keyword().onField("objectB.pkid").matching(1234).createQuery();
final FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query , ObjectA.class);
fullTextQuery.getResultList();

With massindexer for this class all works fine but i would like to be able to search objectA with all properties of objectB during the save(update).
Thank you!

Hey,

Your code samples seem fine, so the problem is probably somewhere else.

I’d say the most likely problem is that you try to run your search query before Elasticsearch had time to refresh. See here. You can force synchronous indexing with Hibernate Search with the following configuration, but you really only should do this for tests:

Whether to perform an explicit refresh after a set of operations has been executed against a specific index (true or false)

hibernate.search.default.elasticsearch.refresh_after_write false (default)

This is useful in unit tests to ensure that a write is visible by a query immediately without delay. This keeps unit tests simpler. You should not rely on the synchronous behaviour for your production code except in rare cases as Elasticsearch is optimised for asynchronous writes: leave at false for optimal performance.

If that’s not the problem, please provide a simple project to reproduce your problem, and I’ll have a look.

i am using lucene hibernate search and not elasticsearch. I will check your comment and i will come back !

Ok, if you’re using Lucene, my previous comment is irrelevant.

Another possibility is that you forgot to set both sides of the association when creating ObjectB: objectA.setObjectB(objectB) and objectB.setObjectA(objectA).

If you forget one side, you run the risk of things not being indexed correctly.

You can find an explanation of this in the Hibernate Search 6 documentation: Hibernate Search 6.1.7.Final: Reference Documentation