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

**URL:** <https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328>\
**Category:** Elasticsearch\
**Created:** [October 25, 2019, 9:00pm UTC](https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328 "2019-10-25T21:00:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tadija\_Malic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tadija_malic/32/56654_2.png) [@Tadija\_Malic](https://discuss.elastic.co/u/Tadija_Malic)\
**Post date:** [October 25, 2019, 9:00pm UTC](https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328/1 "2019-10-25T21:00:57Z")

</div>

**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](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:

```auto
 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)_

```auto
<!-- 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:

```auto
<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:

```auto
[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.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 28, 2019, 11:04am UTC](https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328/2 "2019-10-28T11:04:08Z")

</div>

> [@Tadija\_Malic](#):
>
> 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 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.

---

<div class="post-metadata">

**Author:** ![Tadija\_Malic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tadija_malic/32/56654_2.png) [@Tadija\_Malic](https://discuss.elastic.co/u/Tadija_Malic)\
**Post date:** [October 29, 2019, 4:31pm UTC](https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328/3 "2019-10-29T16:31:53Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 26, 2019, 4:32pm UTC](https://discuss.elastic.co/t/method-threw-java-lang-nosuchfielderror-exception-cannot-evaluate-org-elasticsearch-action-index-indexrequest-tostring/205328/4 "2019-11-26T16:32:00Z")

</div>

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