Abhinav
Abhinav

Reputation: 323

Elastic search(6.2.4) connect using RestHighLevelClient

I have been trying to connect to elastic search(6.2.4) using RestHighLevelClient using java 1.8. I have added all the dependencies and I'm using the following code in a simple jsp.

sample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

  <%@page import="java.util.*" %>
  <%@page import="java.io.*" %>
  <%@page import="com.google.gson.*" %>
  <%@page import="org.apache.http.*"%>
  <%@page import= "org.elasticsearch.client.*"%>
  <%@page import= "org.elasticsearch.common.document.*"%>
  <%@page import= "org.elasticsearch.index.query.*"%>
  <%@page import= "org.elasticsearch.action.get.*"%>
  <%@page import= "org.elasticsearch.search.*"%>
  <%@page import= "org.elasticsearch.search.fetch.subphase.*"%>
  <%@page import= "org.elasticsearch.search.builder.*"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Elastic Report</title>
</head>
<body>
<%
RestHighLevelClient client=null;
Gson gson = new Gson(); 

try{
 client = new RestHighLevelClient(
        RestClient.builder(new HttpHost("xx.yy.xx.zz", 9201, "http")));

GetRequest getRequest = new GetRequest(
        "samples", 
        "sample",  
        "xxxxxxxxxx");
String[] includes = new String[]{"ip"};
String[] excludes = {};
FetchSourceContext fetchSourceContext =
        new FetchSourceContext(true, includes, excludes);
getRequest.fetchSourceContext(fetchSourceContext);  

GetResponse getResponse = client.get(getRequest);
if(getResponse.isExists()){
Map<String,DocumentField> fields = getResponse.getFields();
String json = gson.toJson(fields); 
out.write(json);
}
client.close();
}
catch(Exception ex){
    ex.printStackTrace();
}

%>
</body>
</html>

All the dependencies are added and here is my eclipse snapshot

enter image description here

However, I'm getting the following error immediately once I fire my tomcat on eclipse

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/elasticsearch/common/logging/Loggers
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/elasticsearch/common/logging/Loggers
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
    org.apache.jsp.elasticsearch_jsp._jspService(elasticsearch_jsp.java:140)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Loggers class is not available in the jar(6.2.4 version)

enter image description here

However, once I refresh the page, I get the below error

 org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.common.ParseField
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    root cause

    javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.common.ParseField
        org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
        org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
        org.apache.jsp.elasticsearch_jsp._jspService(elasticsearch_jsp.java:140)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I haven been able to find the ParseField class in the expanded package. However application is failing with No class found error

enter image description here

Can anyone let me know what is the mistake I'm making here? Also, can anyone point me to a full sample project using RestHighlevelclient with all dependencies(jars) to include?

Any help is appreciated.

Upvotes: 1

Views: 957

Answers (1)

Christophe
Christophe

Reputation: 2200

Here is the list of JARs that I had to use in a brand new maven project to get your sample code working :

-rw-r--r-- 1 cth 1049089   58411 Aug 24 14:31 aggs-matrix-stats-client-6.2.4.jar
-rw-r--r-- 1 cth 1049089  284184 Oct  4  2017 commons-codec-1.10.jar
-rw-r--r-- 1 cth 1049089   62050 Aug  7  2017 commons-logging-1.1.3.jar
-rw-r--r-- 1 cth 1049089 9929279 Aug 24 14:31 elasticsearch-6.2.4.jar
-rw-r--r-- 1 cth 1049089   15884 Aug 24 14:31 elasticsearch-cli-6.2.4.jar
-rw-r--r-- 1 cth 1049089   19163 Aug 24 14:31 elasticsearch-core-6.2.4.jar
-rw-r--r-- 1 cth 1049089   38473 Aug 24 14:31 elasticsearch-rest-client-6.2.4.jar
-rw-r--r-- 1 cth 1049089   34071 Aug 24 14:30 elasticsearch-rest-high-level-client-6.2.4.jar
-rw-r--r-- 1 cth 1049089   39452 Aug 24 14:33 gson-2.6.1.jar
-rw-r--r-- 1 cth 1049089  231952 Aug 24 14:33 gson-2.8.0.jar
-rw-r--r-- 1 cth 1049089  114165 Aug  1 11:38 HdrHistogram-2.1.9.jar
-rw-r--r-- 1 cth 1049089 1140290 Jul 27  2017 hppc-0.7.1.jar
-rw-r--r-- 1 cth 1049089  177112 Aug  1 11:38 httpasyncclient-4.1.2.jar
-rw-r--r-- 1 cth 1049089  736658 Aug 22  2017 httpclient-4.5.2.jar
-rw-r--r-- 1 cth 1049089  327373 Aug  1 13:41 httpcore-4.4.5.jar
-rw-r--r-- 1 cth 1049089  356201 Aug  1 11:38 httpcore-nio-4.4.5.jar
-rw-r--r-- 1 cth 1049089  282634 Aug 24 14:31 jackson-core-2.8.10.jar
-rw-r--r-- 1 cth 1049089   50728 Aug 24 14:31 jackson-dataformat-cbor-2.8.10.jar
-rw-r--r-- 1 cth 1049089   73889 Aug 24 14:31 jackson-dataformat-smile-2.8.10.jar
-rw-r--r-- 1 cth 1049089   40881 Aug 24 14:31 jackson-dataformat-yaml-2.8.10.jar
-rw-r--r-- 1 cth 1049089  885297 Aug 24 14:31 jna-4.5.1.jar
-rw-r--r-- 1 cth 1049089  634048 Aug 24 14:31 joda-time-2.9.9.jar
-rw-r--r-- 1 cth 1049089   78074 Aug  1 11:38 jopt-simple-5.0.2.jar
-rw-r--r-- 1 cth 1049089  794991 Jul 27  2017 jts-1.13.jar
-rw-r--r-- 1 cth 1049089  239856 Aug  3 09:21 log4j-api-2.9.1.jar
-rw-r--r-- 1 cth 1049089 1549865 Aug  3 09:21 log4j-core-2.9.1.jar
-rw-r--r-- 1 cth 1049089 1621932 Aug 24 14:31 lucene-analyzers-common-7.2.1.jar
-rw-r--r-- 1 cth 1049089   98904 Aug 24 14:31 lucene-backward-codecs-7.2.1.jar
-rw-r--r-- 1 cth 1049089 2792335 Aug 24 14:31 lucene-core-7.2.1.jar
-rw-r--r-- 1 cth 1049089   87166 Aug 24 14:31 lucene-grouping-7.2.1.jar
-rw-r--r-- 1 cth 1049089  197790 Aug 24 14:31 lucene-highlighter-7.2.1.jar
-rw-r--r-- 1 cth 1049089  146135 Aug 24 14:31 lucene-join-7.2.1.jar
-rw-r--r-- 1 cth 1049089   46969 Aug 24 14:31 lucene-memory-7.2.1.jar
-rw-r--r-- 1 cth 1049089   91675 Aug 24 14:31 lucene-misc-7.2.1.jar
-rw-r--r-- 1 cth 1049089  258756 Aug 24 14:31 lucene-queries-7.2.1.jar
-rw-r--r-- 1 cth 1049089  384677 Aug 24 14:31 lucene-queryparser-7.2.1.jar
-rw-r--r-- 1 cth 1049089  183425 Aug 24 14:31 lucene-sandbox-7.2.1.jar
-rw-r--r-- 1 cth 1049089  284757 Aug 24 14:31 lucene-spatial3d-7.2.1.jar
-rw-r--r-- 1 cth 1049089   14432 Aug 24 14:31 lucene-spatial-7.2.1.jar
-rw-r--r-- 1 cth 1049089  228843 Aug 24 14:31 lucene-spatial-extras-7.2.1.jar
-rw-r--r-- 1 cth 1049089  249543 Aug 24 14:31 lucene-suggest-7.2.1.jar
-rw-r--r-- 1 cth 1049089   76149 Aug 24 14:31 parent-join-client-6.2.4.jar
-rw-r--r-- 1 cth 1049089   58585 Aug 24 14:31 rank-eval-client-6.2.4.jar
-rw-r--r-- 1 cth 1049089    7465 Aug 24 14:31 securesm-1.2.jar
-rw-r--r-- 1 cth 1049089  273599 Oct 31  2017 snakeyaml-1.17.jar
-rw-r--r-- 1 cth 1049089  187537 Aug 24 14:31 spatial4j-0.6.jar
-rw-r--r-- 1 cth 1049089   49754 Jul 27  2017 t-digest-3.0.jar

Here is the pom (just useful if you have also maven)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>elastictest</groupId>
    <artifactId>elastictest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.test.failure.ignore>false</maven.test.failure.ignore>
        <maven.javadoc.skip>true</maven.javadoc.skip>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>2.5</log4j.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.immutables</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>elastictest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Upvotes: 1

Related Questions