弹性搜索(6.2.4)使用RestHighLevelClient连接

时间:2018-08-24 10:08:58

标签: elasticsearch

我一直在尝试使用Java 1.8使用RestHighLevelClient连接到弹性搜索(6.2.4)。我添加了所有依赖关系,并在一个简单的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>

添加了所有依赖项,这是我的日食快照

enter image description here

但是,一旦我在Eclipse上启动了tomcat,我会立即收到以下错误消息

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类在jar(6.2.4版)中不可用

enter image description here

但是,刷新页面后,出现以下错误

 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)

我一直无法在扩展包中找到ParseField类。但是应用程序失败,没有找到类错误

enter image description here

有人可以让我知道我在这里犯的错误吗?另外,有人可以使用RestHighlevelclient并包含所有依赖项(jar)将我指向一个完整的示例项目吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

以下是我必须在一个全新的maven项目中使用的JAR列表,才能使您的示例代码正常工作:

-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

这是pom(如果您也有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>