Elasticsearch 6.4 ingest-attachment对office文件IK分词器全文检索(2) JavaAPI使用

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/LazyBoy_Z_z/article/details/82865115

该文基于 idea 和 maven环境下实现,并记录为bug人生,如果博友们对ES感兴趣:

首先我先根据官网API编写:

第一步: 新建一个maven项目(略) ,修改pom.xml

<dependency>

    <groupId>org.elasticsearch.client</groupId>

    <artifactId>transport</artifactId>

    <version>6.4.0</version>

</dependency>

第二步:编写代码:

    

Settings settings = Settings.builder()

.put("cluster.name", "my-esLearn") //连接的集群名

.put("client.transport.ignore_cluster_name", true) //如果集群名不对,也能连接

.build();

//创建client

transportClient = new PreBuiltTransportClient(settings)

.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.45"), 9300));

第三步: 报错

Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/script/ScriptEngine

    at java.lang.Class.getDeclaredConstructors0(Native Method)

    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)

    at java.lang.Class.getConstructor0(Class.java:3075)

    at java.lang.Class.getConstructor(Class.java:1825)

    at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:427)

    at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:104)

    at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:104)

    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:129)

    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:268)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:128)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:114)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:104)

    at com.lhy.example.bootui.template.EsDemo.init(EsDemo.java:32)

    at com.lhy.example.bootui.template.EsDemo.main(EsDemo.java:59)

Caused by: java.lang.ClassNotFoundException: org.elasticsearch.script.ScriptEngine

    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

    ... 14 more

 

第四步调试:

    缺少这个plugin

结论:少jar包 ,官网不全


根据ElasticSearch技术实战 5 + 经验 编写demo:

第一步:修改pom.xml

<dependency>

    <groupId>org.elasticsearch</groupId>

    <artifactId>elasticsearch</artifactId>

    <version>6.4.0</version>

</dependency>

<dependency>

    <groupId>org.elasticsearch.client</groupId>

    <artifactId>transport</artifactId>

    <version>6.4.0</version>

</dependency>

第二步:编写代码:

 

Settings settings = Settings.builder()

.put("cluster.name", "my-esLearn") //连接的集群名

.put("client.transport.ignore_cluster_name", true) //如果集群名不对,也能连接

.build();

//创建client

transportClient = new PreBuiltTransportClient(settings)

.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.45"), 9300));

第三步:报错

    Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/InetSocketTransportAddress

    at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:55)

    at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:87)

    at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267)

    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)

    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)

    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)

    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)

    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)

    at org.elasticsearch.plugins.PluginsService.getPluginSettings(PluginsService.java:87)

    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:145)

    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:283)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:128)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:114)

    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:104)

    at com.lhy.example.bootui.template.EsDemo.init(EsDemo.java:31)

    at com.lhy.example.bootui.template.EsDemo.main(EsDemo.java:58)

Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.transport.InetSocketTransportAddress

    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

    ... 17 more

结论:书上不全,方法行不通


根据以上经验+https://stackoverflow.com/

第一步:修改pom.xml:

<dependency>

    <groupId>org.elasticsearch</groupId>

    <artifactId>elasticsearch</artifactId>

    <version>6.4.0</version>

</dependency>

 

<dependency>

    <groupId>org.elasticsearch.client</groupId>

    <artifactId>transport</artifactId>

    <version>6.4.0</version>

</dependency>

 

<dependency>

    <groupId>org.elasticsearch.plugin</groupId>

    <artifactId>transport-netty4-client</artifactId>

    <version>6.4.0</version>

</dependency>

 第二步:编写代码   

ublic class EsDemo {

private TransportClient transportClient;

 

public void init() throws Exception {

if (transportClient != null){

transportClient.close();

}

 

Settings settings = Settings.builder()

.put("cluster.name", "my-esLearn") //连接的集群名

.put("client.transport.ignore_cluster_name", true) //如果集群名不对,也能连接

.build();

//创建client

transportClient = new PreBuiltTransportClient(settings)

.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.45"), 9300));

 

//主机和端口号

}

 

public void index(String path) throws IOException {

String contentStr = Base64.encodeFromFile(path);

XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject()

.field("id", "java001")

.field("title", "这是我第一个javaAPI")

.field("data", contentStr)

.endObject();

 

transportClient.prepareIndex("index","doc","java001").setPipeline("simple_attachment")

.setSource(xContentBuilder).get();

}

 

public void close(){

transportClient.close();

}

 

 

 

public static void main(String[] args) throws Exception {

String path = "F:\\0jinxiandai-lhy\\1.doc";

EsDemo esDemo = new EsDemo();

esDemo.init();

esDemo.index(path);

esDemo.close();

}

}

查询结果:

 

{

  "took": 4,

  "timed_out": false,

  "_shards": {

    "total": 1,

    "successful": 1,

    "skipped": 0,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 1,

    "hits": [

      {

        "_index": "index",

        "_type": "doc",

        "_id": "java001",

        "_score": 1,

        "_source": {

             "attachment": {

            "content_type": "application/msword",

            "title": "Elasticsearch-attachment java实现方案",

            "content": """

Elasticsearch-attachment java实现方案

 

这是我的第一个测试文件

作者:惠玚

二○一八年九月

 

目录

 

1

 

1

引言

 

1

 

1.1

编写目的

 

1

 

1.2

读者对象

 

1

 

1.3

专业术语

 

1

 

2

组织机构维护

 

1

 

2.1

登陆统一权限平台

 

2

 

2.2

新增组织机构

 

4

 

2.3

编辑组织机构

 

6

 

2.4

删除组织机构

 

8

 

3

用户权限维护

 

8

 

3.1

新增组织角色

 

11

 

3.2

组织角色授权

 

14

 

3.3

删除组织角色

 

17

 

4

技术支持

 

 

 

 

PAGE

"""

          },

          "id": "java001",

          "title": "这是我第一个javaAPI"

        }

      }

    ]

  }

}

 

 

展开阅读全文

没有更多推荐了,返回首页