lucene使用

一 概述
   结构化数据    格式和长度固定,比如数据库表
   非结构化数据  格式和长度不固定,比如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;
     }

 

更多免费技术资料可关注:annalin1203

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值