ClassCastException: Cannot cast ScopedElasticsearchAnalyzerReference to LuceneAnalyzerReference

Description

After Integrating our existing Hibernate Search application with ElasticSearch,
we are getting the following exception upon querying from created indexes :
java.lang.ClassCastException:

Cannot cast org.hibernate.search.elasticsearch.analyzer.impl.ScopedElasticsearchAnalyzerReference to org.hibernate.search.analyzer.impl.LuceneAnalyzerReference
java.lang.Class.cast(Class.java:3369)
org.hibernate.search.analyzer.impl.RemoteAnalyzerReference.unwrap(RemoteAnalyzerReference.java:28)
org.hibernate.search.engine.impl.ImmutableSearchFactory.getAnalyzer(ImmutableSearchFactory.java:394)
org.hibernate.search.engine.impl.MutableSearchFactory.getAnalyzer(MutableSearchFactory.java:211)
org.hibernate.search.impl.SearchFactoryImpl.getAnalyzer(SearchFactoryImpl.java:53)
com.nucleus.controller.HibernateSearchController.search(HibernateSearchController.java:62)

The indexes were created successfully on ElasticSearch server.
But when we try to query the indexes using the analyzer we get the above exception.
The query is:

Environment

None

Activity

Show:
Yoann Rodière
December 21, 2018, 8:11 AM
Edited

Thank you for your feedback. The error message is confusing; I will change it and document the limitation.

An explanation of what is happening: you are trying to get an instance of org.apache.lucene.analysis.Analyzer, which is a Lucene-specific type, while using the Elasticsearch integration, which basically just manipulates JSON and delegates most of the operations (including analyzers) to a remote server across the network.

This cannot be done; there is no Analyzer instance in your application when you use the Elasticsearch application, and there never will be.

Now about a solution... I see you only need the analyzer to use MultiFieldQueryParser in order to parse a query string. If that string is provided by your users, you may want to use "simple query string" queries. They provide more or less the same feature as MultiFieldQueryParser, but on the contrary to MultiFieldQueryParser they can be used in the Elasticsearch backend.
In your case you will have to do something like this:

If you need to run multiple queries, you should do this multiple times and combine the query objects using a boolean junction: see "Combining queries" in the documentation.

Yoann Rodière
December 21, 2018, 9:19 AM

The code in my previous message was wrong (there were remnants of the code I based my example on). I edited my message.

Yoann Rodière
January 31, 2019, 3:01 PM

Fixed by improving the error message and documenting the limitation in the javadoc.

Assignee

Yoann Rodière

Reporter

Shivendra Kumar

Labels

None

Suitable for new contributors

None

Feedback Requested

None

Components

Fix versions

Affects versions

Priority

Minor
Configure