Method threw 'java.lang.NoSuchFieldError' exception. Cannot evaluate org.elasticsearch.action.index.IndexRequest.toString()

Describe the feature : Comparison between Elasticsearch, Solr and OOTB Lucene in Adobe Experience Manager
Elasticsearch version : Tried 7.0.1 and 7.4.1
JVM version: 1.8.0_191
OS version macOs - Darwin Kernel Version 18.7.0
I expect to be able to index content, but cannot resolve an error from the title

Steps to reproduce : This is done on AEM reference WeRetail project. Here is complete code:
https://github.com/tadijam64/search-engines-comparison-on-we-retail/tree/elasticsearch-servlet-impl (one of the versions)

I am trying to use Elasticsearch with AEM. Documentation of Elasticsearh says that only elasticsearch-high-level-client is needed, but it did not work for me because transitive dependencies were not imported. This is my code:

 Session session = resourceResolver.adaptTo(Session.class);
 XContentBuilder builder = this.elasticsearchService.crawlContent(session);

IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.id(version);
try
{
           indexRequest.source(builder);
           IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
           writeResponse(indexResponse, response);
}

It breaks on this line: IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
with the following exception from the log:

25.10.2019 22:41:30.887 ERROR [0:0:0:0:0:0:0:1 [1572035909141] GET /content/we-retail/language-masters/en/equipment.indexElasticsearchPages.json/_jcr_content/root/header/search HTTP/1.1] org.apache.sling.servlets.resolver.internal.SlingServletResolver Original error class java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.rest.BytesRestResponse

This are Maven parts regarding the Elasticsearch:
(SInce it could not add all transitive dependencies, I added all of them manually)

<!-- Elasticsearch -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch-x-content</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch-core</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.plugin</groupId>
                <artifactId>rank-eval-client</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-imaging</artifactId>
                <version>1.0-alpha1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.plugin</groupId>
                <artifactId>lang-mustache-client</artifactId>
                <version>7.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpasyncclient</artifactId>
                <version>4.1.4</version>
            </dependency>

Maven bundle plugin to embed third party dependencies:

<plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Embed-Dependency>org.apache.servicemix.bundles.solr-solrj, noggit, zookeeper,
                            elasticsearch-rest-high-level-client;scope=compile|runtime,
                            elasticsearch;scope=compile|runtime,
                            elasticsearch-rest-client;scope=compile|runtime,
                            elasticsearch-x-content;scope=compile|runtime,
                            elasticsearch-core;scope=compile|runtime,
                            rank-eval-client;scope=compile|runtime,
                            lang-mustache-client;scope=compile|runtime,
                            httpasyncclient;scope=compile|runtime;
                        </Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <Embed-Directory>OSGI-INF/lib</Embed-Directory>
                        <Export-Package>we.retail.core.model*</Export-Package>
                        <_exportcontents>
                            org.elasticsearch.*,
                            org.elasticsearch.client.*,
                            org.elasticsearch.plugin.*,
                            org.apache.commons.*
                        </_exportcontents>
                        <Import-Package>
                            *;resolution:=optional;
                        </Import-Package>
                        <Private-Package>we.retail.core*</Private-Package>
                        <Sling-Model-Packages>
                            we.retail.core.model
                        </Sling-Model-Packages>
                        <_fixupmessages>"Classes found in the wrong directory";is:=warning</_fixupmessages>
                    </instructions>
                </configuration>
            </plugin>

Note : _<_fixupmessages>"Classes found in the wrong directory";is:=warning</ fixupmessages>
This was needed because I am getting the following error when embeding org.elasticsearch:

[WARNING] Bundle com.adobe.cq.sample:we.retail.core:bundle:3.0.0-SNAPSHOT : Classes found in the wrong directory: {META-INF/versions/9/org/elasticsearch/monitor/jvm/JvmPid.class=org.elasticsearch.monitor.jvm.JvmPid}
[WARNING] Bundle com.adobe.cq.sample:we.retail.core:bundle:3.0.0-SNAPSHOT : Classes found in the wrong directory: {META-INF/versions/9/org/elasticsearch/core/internal/io/Streams.class=org.elasticsearch.core.internal.io.Streams}
[INFO]

Also there are some warnings on build similar to this one:

[WARNING] Bundle com.adobe.cq.sample:we.retail.core:bundle:3.0.0-SNAPSHOT : Export org.elasticsearch.common.lucene.search.function, has 1, private references [org.apache.lucene.index],

I have provided all info I got, so I hope someone will have at least some ideas how to solve this.

This to me indicates that the servlet (I don't know this one, but it appears to be part of Apache Sling) is not able to load the "BytesRestResponse" class which should be inside the elasticsearch-.jar. From the maven snippet you posted it looks like this should be pulled, however I don't know about your maven project setup and how it gets on the classpath of whatever processes that the servlet is running. I would focus my attention on that part.

1 Like

Whenever I add org.elasticsearch dependency, I got an following error:
[ERROR] Bundle com.adobe.cq.sample:we.retail.core:bundle:3.0.0-SNAPSHOT : Classes found in the wrong directory: {META-INF/versions/9/org/elasticsearch/monitor/jvm/JvmPid.class=org.elasticsearch.monitor.jvm.JvmPid}
[ERROR] Error(s) found in bundle configuration

Any info on that? I am using Java jdk 1.8.0_191 (tried using java 9 and 11 as well, the error is the same)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.