it编程 > 网页制作 > 网页播放器

ElasticSearch 实现 全文检索 支持(PDF、TXT、Word、HTML等文件)通过 ingest-attachment 插件实现 文档的检索_es全文检索word文件

331人参与 2024-08-03 网页播放器

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里p7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加v获取:vip204888 (备注大数据)
img

正文

3、小结

安装完成后需要重新启动es

接下来我们需要创建一个关于ingest-attachment的文本抽取管道

put /_ingest/pipeline/attachment
{
    "description": "extract attachment information",
    "processors": [
        {
            "attachment": {
                "field": "content",
                "ignore_missing": true
            }
        },
        {
            "remove": {
                "field": "content"
            }
        }
    ]
}

后续我们的文件需要base64后储存到 attachment.content 索引字段中

三、如何应用?

1、通过命令语句简易检索

创建一个es 索引 并且添加一些测试数据

post /pdf_data/_doc?pretty
{

  "id": "3",

  "name": "面试题文件1.pdf",

  "age": 18,

  "type": "file",

  "money": 1111,

  "createby": "阿杰",

  "createtime": "2022-11-03t10:41:51.851z",

  "attachment": {

    "content": "面试官:如何保证消息不被重复消费啊?如何保证消费的时候是幂等的啊?kafka、activemq、rabbitmq、rocketmq 都有什么区别,以及适合哪些场景?",

    "date": "2022-11-02t10:41:51.851z",

    "language": "en"

  }
}

通过插入的文档内容为条件进行检索

# 简单 单条件查询 文档内容检索
get /pdf_data/_search
{
  "query": {
    "match": {
      "attachment.content": "面试官:如何保证消息不被重复消费啊?如何保证消费的时候是幂等的啊?"
    }
  }
}

2、整合java代码实现es通过ingest-attachment进行全文检索

1、首先将文件转为base64进行es数据插入
/**
     * 将文件 文档信息储存到数据中
     * @param file
     * @return
     */
    @postmapping("/insertfile")
    @apioperation(value="创建索引es-传入es索引-传入文件", notes="创建索引es-传入es索引-传入文件")
    public indexresponse insertfile(@requestattribute("file") multipartfile file,@requestparam("indexname")string indexname){
        fileobj fileobj = new fileobj();
        fileobj.setid(string.valueof(system.currenttimemillis()));
        fileobj.setname(file.getoriginalfilename());
        fileobj.settype(file.getname().substring(file.getname().lastindexof(".") + 1));
        fileobj.setcreateby(randomnamegenerator.generaterandomname());
        fileobj.setcreatetime(string.valueof(system.currenttimemillis()));
        fileobj.setage(randomnamegenerator.getage());
        fileobj.setmoney(randomnamegenerator.getmoney());
        // 文件转base64
        byte[] bytes = new byte[0];
        try {
            bytes = file.getbytes();
            //将文件内容转化为base64编码
            string base64 = base64.getencoder().encodetostring(bytes);
            fileobj.setcontent(base64);

           indexresponse indexresponse=  elasticsearchutil.upload(fileobj,indexname);
            if (0==indexresponse.status().getstatus()){
                // 索引创建并插入数据成功
                system.out.println("索引创建并插入数据成功");
            }
            return indexresponse;

        } catch (exception e) {
            e.printstacktrace();
        }
        return null;
    }
2、创建索引、插入数据,并且将文档数据抽取到管道中
    @autowired
    private resthighlevelclient resthighlevelclient;

    private  static  resthighlevelclient levelclient;

    @postconstruct
    public void initclient() {
        levelclient = this.resthighlevelclient;
    }

/**
     * 创建索引并插入数据
     * @param file
     * @param indexname
     * @return
     * @throws ioexception
     */
    public static indexresponse upload(fileobj file,string indexname) throws ioexception {
        // todo 创建前需要判断当前文档是否已经存在
        if (!isindexexist(indexname)) {
            createindexrequest request = new createindexrequest(indexname);
        // 如果需要ik分词器就添加配置,不需要就注释掉 
            // 添加 ik 分词器设置  ik_max_word
//            request.settings(settings.builder()
//                    .put("index.analysis.analyzer.default.type", "ik_max_word")
//                    .put("index.analysis.analyzer.default.use_smart", "true")
//            );
            
            // 添加 ik 分词器设置 ik_smart 
            request.settings(settings.builder()
                    .put("index.analysis.analyzer.default.type", "ik_smart")
            );
            createindexresponse response = levelclient.indices().create(request, requestoptions.default);
            log.info("执行建立成功?" + response.isacknowledged());
        }
        indexrequest indexrequest = new indexrequest(indexname);
        //上传同时,使用attachment pipline进行提取文件
        indexrequest.source(json.tojsonstring(file), xcontenttype.json);
        indexrequest.setpipeline("attachment");
        indexresponse indexresponse= levelclient.index(indexrequest,requestoptions.default);
        system.out.println(indexresponse);
        return indexresponse;
    }
3、其他代码补充

es config 配置类

/**
 * es配置类
 * author: 阿杰
 */
@configuration
public class elasticsearchclientconfig {

    /**
     * es 地址:127.0.0.1:9200
     */
    @value("${es.ip}")
    private string hostname;

    @bean
    public resthighlevelclient resthighlevelclient() {
        string[] points = hostname.split(",");
        httphost[] httphosts = new httphost[points.length];
        for (int i = 0; i < points.length; i++) {
            string point = points[i];
            httphosts[i] = new httphost(point.split(":")[0], integer.parseint(point.split(":")[1]), "http");
        }
        resthighlevelclient client = new resthighlevelclient(
                restclient.builder(httphosts));
        return client;
    }

    @bean
    public elasticsearchutil elasticsearchutil() {
        return new elasticsearchutil();
    }


}

数据插入使用的实体类

/**
 * author: 阿杰
 */
@data
public class fileobj {
    /**
     * 用于存储文件id
     */
    string id;


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加v获取:vip204888 (备注大数据)**
![img](https://img-blog.csdnimg.cn/img_convert/5933a47925ba6785096df7daa46c7db2.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事it行业的老鸟或是对it行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

string id;


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加v获取:vip204888 (备注大数据)**
[外链图片转存中...(img-vfbhulk3-1713120469612)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事it行业的老鸟或是对it行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

ElasticSearch 实现 全文检索 支持(PDF、TXT、Word、HTML等文件)通过 ingest-attachment 插件实现 文档的检索

08-03

「图文教程」Windows系统Microsoft Edge浏览器设置搜索框搜索引擎为百度

08-03

ESP32进程开启http和webSocket服务

08-03

高效抓取网页模板:Go 1.19站点模板爬虫实战指南

08-03

智慧电商SAAS平台、门店管理、仓储管理、订单管理、店铺运营、采购管理、数据分析、交易分析、留存分析、用户运营、围栏管理、自提点管理、商城运营、流量分析、用户权限、销量分析、佣金明细、Axure原型

08-03

Selenium安装WebDriver:ChromeDriver谷歌浏览器驱动下载安装与使用最新版116/117/118/119/120/121/122/123

08-03

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论