lucene使用

本文详细介绍了Lucene全文检索技术的原理与应用,包括结构化与非结构化数据的概念,全文检索的流程,如创建索引、分析文档、查询索引等。特别关注了IK分词器的使用方法及代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 概述
   结构化数据    格式和长度固定,比如数据库表
   非结构化数据  格式和长度不固定,比如word 
   全文检索      针对非结构化数据,采用先建立索引,然后再索引的基础上进行查询
   java全文检索技术lucene  全文检索的工具包 
   应用场景:针对大数据量的情况下,对数据的模糊查询或者自然语言的检索。
二 创建索引
        1 获取原始文档  爬虫Nutch jsoup
        2 创建文档对象   document 相当于表中一条记录   field 属于document 相当于表中的字段   field中存储内容
                filed的属性判断原则:
                是否需要分词 需要在也页面进行查询的大部分都需要进行分词,除了一些拥有特殊业务含义,分割后会失去原有含义的不需要进行分词。比如订单编号,身份证号。
                是否需要索引 需要在页面进行查询的就需要进行索引
                是否需要存储 需要在页面显示的就需要存储
        3 分析文档 
                对存放在field中的内容进行分析,得到term列表。
                过程:对原始文档提取单词、将字母转为小写、去除标点符号、去除停用词
        4 创建索引
       对term列表进行索引,每个term执行文档的id列表。  
三 查询索引
        1 查询接口
        2 创建查询
        3 执行查询
        4 渲染结果
四 IK分词器
    ik分词器即支持中文也支持英文。
        使用方法:
        第一步:把jar包添加到工程中
        第二步:把配置文件和扩展词典和停用词词典添加到classpath下
        注意:mydict.dic和ext_stopword.dic文件的格式为UTF-8,注意是无BOM 的UTF-8 编码。
        分词器的应用时机:        创建索引时和查询索引时针对查询内容先进行分词。
五 代码实现
[Java]  纯文本查看  复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
//创建索引
     public void createIndex() throws Exception{
         IndexWriter indexWriter = getIndexWriter();
         File sourceFile = new File( "E:\\项目二\\lucene\\day06\\资料\\searchsource" );
         for (File file :sourceFile.listFiles() ) {
             String fileName = file.getName();
             String filePath = file.getPath();
             long size = FileUtils.sizeOf(file);
             String fileContent = FileUtils.readFileToString(file);
             Document document = new Document();
             TextField fileNameField = new TextField( "name" , fileName, Store.YES);
             StoredField pathField = new StoredField( "path" , filePath);
             LongField sizeField = new LongField( "size" , size, Store.YES);
             TextField contentFeild = new TextField( "content" ,fileContent,Store.NO);
             
             document.add(fileNameField);
             document.add(pathField);
             document.add(sizeField);
             document.add(contentFeild);
             indexWriter.addDocument(document);
         }
         indexWriter.close();
     }
     
     //查询索引
     public void queryIndex() throws Exception{
         Directory directory = FSDirectory.open( new File( "E:\\index" ));
         IndexReader indexReader = DirectoryReader.open(directory);
         IndexSearcher indexSearcher = new IndexSearcher(indexReader);
         //term查询相当于等值查询
         Query query = new TermQuery( new Term( "name" , "lucene" ));
         TopDocs topDocs = indexSearcher.search(query , 10 );
         for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
             int id = scoreDoc.doc;
             Document document = indexSearcher.doc(id);
             System.out.println(document.getField( "name" ));
             System.out.println(document.getField( "path" ));
             System.out.println(document.getField( "content" ));
             System.out.println(document.getField( "size" ));
         }
         
         indexReader.close();
     }
     
     // 添加文档
     public void addDocument() throws Exception {
         IndexWriter indexWriter = this .getIndexWriter();
         Document document = new Document();
         TextField fileNameField = new TextField( "name" , "测试文档.txt" , Store.YES);
         StoredField pathField = new StoredField( "path" , "c:\\temp" );
         LongField sizeField = new LongField( "size" , 1111 , Store.YES);
         document.add(fileNameField);
         document.add(pathField);
         document.add(sizeField);
         indexWriter.addDocument(document);
         indexWriter.close();
     }
     
     // 根据query删除文档
     public void deleteByQuery() throws Exception {
         IndexWriter indexWriter = this .getIndexWriter();
         Query query = new TermQuery( new Term( "name" , "测试" ));
         
         indexWriter.deleteDocuments(query);
         indexWriter.close();
     }
     
     // 删除全部文档
     public void deleteAll() throws Exception {
         IndexWriter indexWriter = this .getIndexWriter();
         
         indexWriter.deleteAll();
         indexWriter.close();
     }
     
     //更新文档
     //先删除,然后再添加
     public void update() throws Exception{
         IndexWriter indexWriter = this .getIndexWriter();
         Term term = new Term( "name" , "spring" );
         Document doc = new Document();
         TextField fileNameField = new TextField( "name" , "测试文档111.txt" , Store.YES);
         StoredField pathField = new StoredField( "path" , "c:\\temp" );
         LongField sizeField = new LongField( "size" , 1111 , Store.YES);
         doc.add(fileNameField);
         doc.add(pathField);
         doc.add(sizeField);
         indexWriter.updateDocument(term, doc);
         indexWriter.close();
     }
     
     //创建索引写入器
     private IndexWriter getIndexWriter() throws IOException {
         //指定索引库存放路径 ,可以存放在内存也可以存放在硬盘,一般存放在硬盘上。
         Directory directory = FSDirectory.open( new File( "E:\\index" ));
         //创建ik分词器 ik分词器即支持中文也支持英文
         Analyzer analyzer = new IKAnalyzer();
         IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
         //创建索引写入器
         IndexWriter indexWriter = new IndexWriter(directory, config);
         return indexWriter;
     }
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 在 Linux 系统中,查找域名或主机名对应的 IP 地址是网络管理中的一项基础且关键任务,对于排查网络故障、调试网络问题以及监控网络服务是否正常运行等场景都非常重要。本文将介绍五种在 Linux 终端查询域名 IP 地址的方法。 首先,dig 命令(全称 Domain Information Groper)是一个功能强大的 DNS 查询工具,能够向 DNS 服务器发送查询请求并获取详细的响应信息。如果需要查询单个域名的 IP 地址,可以使用命令 dig 2daygeek.com +short 。此外,还可以通过编写 bash 脚本,将包含域名的文本文件中的域名逐个读取,然后利用 dig 命令进行查询,从而实现批量查询域名 IP 地址的功能。 其次,host 命令是一个简单易用的 DNS 查询工具,主要用于将域名解析为 IP 地址。要获取某个域名的 IP 地址,直接使用 host 2daygeek.com 即可。如果只想显示 IP 地址部分,可以通过管道结合 grep 和 sed 命令来实现,例如:host 2daygeek.com | grep "has address" | sed s/has address/-/g 。 再者,nslookup 命令也是一种常用的 DNS 查询工具,它支持交互式查询 DNS 信息。通过 nslookup 2daygeek.com 可以查询域名的 IP 地址。若要以非交互式的方式只显示 IP 地址,可以使用命令 nslookup 2daygeek.com | awk /^Address:/ {print $2} 。 另外,fping 命令与传统的 ping 命令不同,它不会直接进行 DNS 查询,而是通过发送 ICMP Echo Request(pi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值