How to use Hibernate Search Elastic /backend

Hi All,
Can any one please help me, where Case (Parent) , Patent(Child) as below
public class Case extends VersioningEntity {

public class Patent extends Case {

Using both, how to prepare Elastic Search Query using Predicates or any other ways.
When I tried, to write, I am getting only Patent Data . I am not getting Case data.

Hey,

What have you tried so far? It’s best to include the query you were attempting to execute next time.
To get both Case and Parent they both have to be indexed entities and you should target the Case entity to get both back in the results, e.g.:

searchSession.search( Case.class )
	.where( f -> f.matchAll() )
	.fetch( 20 );

yes, both are indexed Entity,
Ya I am targeting Case entity , but getting only Patent entity result.
This is my Issue

I am not sure, I can share full code, but I will try to brief you by adding some main points




Hey @shrinivasva

I’ve created a simple test with two entities and search results containing both types:

Try searching with a simple predicate like “match all”. Maybe your results are filtered because of the predicate, and that’s why you do not see other entries.

Then, looking at the code you’ve added in the screenshots… it raises a question… initially you mentioned that you are using the Elasticsearch backend (e.g. in the topic title), but then your code examples are using the Lucene extension … so there’s definitely some mismatch here…

I’d suggest that you try isolating your search query in a simple test, like the one I’ve shared, starting with match all predicate and no sorting and build on that adding more advanced predicates/sorting etc and see where the results start to not match your expectation. You can use the test template we have for Hibernate Search here hibernate-test-case-templates/search/hibernate-search-7 at main · hibernate/hibernate-test-case-templates · GitHub

Hi @mbekhta ,

Ignore those lucene related code, as we are migrating Hibernate search5 to 7 ,
old code still exist , I will clear it once I am able to get result, with My predicates
Just now we tried as you mentioned you can see the result still we are getting only Patent object in results,

If you have a lot of records, it is OK to get patent entries returned; they may have just scored better than case entities… that was why I was suggesting to try with a few records (2-3) so you are more sure of what’s happening. But if you have to run it against more records, then next, I’d try adding a predicate that you know definitely matches the case entity only and not patent.
If that still doesn’t return the case entity for you, then please create a small reproducer using the test case template I shared earlier hibernate-test-case-templates/search/hibernate-search-7 at main · hibernate/hibernate-test-case-templates · GitHub and we’ll take a look at it.

@mbekhta ,

With your suggestion, I am able to fetch only Id of Case by using @DocumentId to My Id field.
But I want to get List SearchResult of Case insted List of Long

Can you help me with that .

@mbekhta ,

I want to add some more point here ,
I am unable to get Case object , as I tried below code .

 List<Case> result = fullTextEntityManager.search(Case.class).where(f ->
		  f.match().field("fileReference").matching("dscsc")).fetchHits(10);

But Strange that, With the below code ,I am able to get Id of particular entity .

List<Long> result = fullTextEntityManager.search(Case.class) .select( f ->
		  f.id( Long.class ) ).where( f -> f.match().field(
		  "title").matching("cccc")).fetchHits(10);

So please help me, why I am not getting Case object or list of Case object .

Hey @shrinivasva

I’m not entirely sure what you are asking of me here… In the two examples you’ve shared, you have different predicates in the where clause, so it’s ok to get different hits, first one is returning back an entity since you haven’t specified any projection, while the second one uses the projection and returns just an id …

The query you’ve shared seems fine, so not sure what particular problem you’ve encountered. If you want me to look closer at what’s happening, please put together a test case using the template I’ve shared earlier.

I have tried adding in your test template, their it is working fine,
So it’s look me strange behavior in local. this is my primary entity it contain many
@IndexedEmbedded and multiple mapped entity exist, it may be the Cause ,
Or I am missing any particular persistnace.xml Configuration missed.

I have as below configuration :

<property name="hibernate.search.default.refresh" value="10"/>
			<property name="hibernate.search.enabled" value="true"/>
			<property name="hibernate.search.backend.type" value="elasticsearch" />
            <property name="hibernate.search.backend.analysis.configurer" value="class:com.xyz.core.config.jpa.MyElasticsearchAnalysisConfigurer" />
            <property name="hibernate.search.backend.layout.strategy" value= "no-alias" />
            <property name="hibernate.search.schema_management.strategy" value="drop-and-create" />
            <property name="hibernate.search.indexing.plan.synchronization.strategy" value="sync" />
            <property name="hibernate.search.backend.connection_timeout" value="30000" />

I need to add one more info, the code which I am using to index a single case,
can you please confirm is it correct way

@Override
	@Traceable
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public ExtData<?> performCaseIndex(Long caseId) throws InterruptedException {
		SearchSession searchSession = Search.session(entityManager);
		SearchIndexingPlan indexingPlan = searchSession.indexingPlan();
		Case caseEntity = entityManager.getReference(Case.class, caseId);
		indexingPlan.addOrUpdate(caseEntity);
		indexingPlan.execute();
		return new ExtData<>(true, "Case index finished successfully");

	}