• ElasticSearch导入PDF,WORD到ES进行全文检索,全文高亮等操作。


    1.环境配置

    使用ElasticSearch导入文本只需要使用ES的javaapi添加文本即可,解析pdf和word我使用的是Tika来解析文档数据,每当一个文本文件被传递到Tika,它将检测在其中的语言。它接受没有语言的注释文件和通过检测该语言添加在该文件的元数据信息。

    1.1 导入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.6.RELEASE</version>
            <relativePath/>
        </parent>
    
        <groupId>com.lun</groupId>
        <artifactId>SpringDataWithES</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
            </dependency>
        </dependencies>
    </project>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    1.2 创建实体类

    
    
    > 这里是引用
    
    package com.item.esdemo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    
    @Document(indexName = "shopping", shards = 3, replicas = 1)
    public class Product {
        //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
        @Id
        private Long id;//商品唯一标识
    
        /**
         * type : 字段数据类型
         * analyzer : 分词器类型
         * index : 是否索引(默认:true)
         * Keyword : 短语,不进行分词
         */
        @Field(type = FieldType.Text, analyzer = "ik_max_word")
        private String title;//商品名称
    
        @Field(type = FieldType.Keyword)
        private String category;//分类名称
    
        @Field(type = FieldType.Double)
        private Double price;//商品价格
    
        @Field(type = FieldType.Keyword, index = false)
        private String images;//图片地址
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    1.3 创建ElasticsearchRestTemplate配置类

    在 Elasticsearch 7.0 中不建议使用TransportClient,并且在8.0中会完全删除TransportClient。因此,官方更建议我们用Java High Level REST Client,它执行HTTP请求,而不是序列化的Java请求。
    我们的使用的 ElasticsearchRestTemplate 就是基于RestHighLevelClient的再一层封装,和RestHighLevelClient一样的功能。

    package com.item.esdemo;
    
    import lombok.Data;
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
    
    
    @ConfigurationProperties(prefix = "elasticsearch")
    @Configuration
    @Data
    public class ElasticasearchConfig  extends AbstractElasticsearchConfiguration {
    
        private String host ;
        private Integer port ;
        //重写父类方法
        @Override
        public RestHighLevelClient elasticsearchClient() {
            RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
            RestHighLevelClient restHighLevelClient = new
                    RestHighLevelClient(builder);
            return restHighLevelClient;
        }
    
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    1.4.创建dao数据访问对象

    业务方法继承后可以有基本的CRUD和分页功能,两个泛型第一个是实体类后面是主键ID的类型

    package com.item.esdemo.dao;
    
    import com.item.esdemo.Product;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    import org.springframework.stereotype.Repository;
    
    /**
     * @author 30856
     */
    @Repository
    public interface ProductDao extends ElasticsearchRepository<Product, Long> {
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.5 添加配置文件信息

    # es 服务地址
    elasticsearch.host=127.0.0.1
    # es 服务端口
    elasticsearch.port=9200
    # 配置日志级别,开启 debug 日志
    logging.level.com.atguigu.es=debug
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. 使用Tika提取文本

    导入流程就麻烦在提取,但是可以通过使用Tika工具来提取我们的pdf和word里面的文本内容。

    2.1 导入Tika依赖

    	<dependencies>
          
            <dependency>
                <groupId>org.apache.tika</groupId>
                <artifactId>tika-parsers</artifactId>
                <version>1.17</version>            
            </dependency>
            
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>jbig2-imageio</artifactId>
                <version>3.0.0</version>
            </dependency>
            
            <dependency>
                <groupId>org.xerial</groupId>
                <artifactId>sqlite-jdbc</artifactId>
                <version>3.8.11.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>       
        </dependencies>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2.2 提取PDF文件内容

    public String paserPdf() {
    
        try {
            File file = new File("C:\\Users\\FileRecv\\test1.pdf");
    
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream fileInputStream = new FileInputStream(file);
            ParseContext parseContext = new ParseContext();
    
            //提取图像信息
            //JpegParser JpegParser = new JpegParser();
            //提取PDF
            PDFParser pdfParser = new PDFParser();
            pdfParser.parse(fileInputStream,handler,metadata,parseContext);
    
            return handler.toString();
            /*String[] names = metadata.names();
            for (String name : names) {
                System.out.println("name:"+metadata.get(name));
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return "";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    把代码给封装好然后把提取到的文件通过ES添加方法添加到ES里面去就可以了。

     @Autowired
        private ProductDao productDao;
        /**
         * 新增
         */
        @Test
        public void save(){
            Product product = new Product();
            product.setId(2L);
            product.setTitle("华为手机");
            product.setCategory(s1.paserPdf());
            product.setPrice(2999.0);
            product.setImages("http://www.atguigu/hw.jpg");
            productDao.save(product);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

  • 相关阅读:
    支持5G LAN的UPF转发模型
    什么是交互设计?大神总结
    【Java JVM】垃圾回收
    NVIDIA JETSON AGX XAVIER DEVELOPER KIT刷机教程(各种踩雷篇)
    Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
    【buildroot】buildroot使用笔记-01 | 常规使用步骤
    Flutter Fair逻辑动态化架构设计与实现
    半小时到秒级,京东零售定时任务优化怎么做的?
    服务器配置环境(Anaconda3、zsh、VSCode、Xshell)
    操作系统 | 内存管理
  • 原文地址:https://blog.csdn.net/qq_45007567/article/details/126589660