全文检索引擎-lucene3.5的例子.doc

上传人:s9****2 文档编号:554510822 上传时间:2022-10-20 格式:DOC 页数:6 大小:59.50KB
返回 下载 相关 举报
全文检索引擎-lucene3.5的例子.doc_第1页
第1页 / 共6页
全文检索引擎-lucene3.5的例子.doc_第2页
第2页 / 共6页
全文检索引擎-lucene3.5的例子.doc_第3页
第3页 / 共6页
全文检索引擎-lucene3.5的例子.doc_第4页
第4页 / 共6页
全文检索引擎-lucene3.5的例子.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《全文检索引擎-lucene3.5的例子.doc》由会员分享,可在线阅读,更多相关《全文检索引擎-lucene3.5的例子.doc(6页珍藏版)》请在金锄头文库上搜索。

1、1,创建一个实体类java view plaincopyprint?1. packagecom.yutel.lucene;2. 3. publicclassContactInfo4. privateintid;5. privateStringFirstName;6. privateStringLastName;7. publicContactInfo()8. 9. publicContactInfo(intid,StringfirstName,StringlastName)10. this.id=id;11. FirstName=firstName;12. LastName=lastName;

2、13. 14. publicintgetId()15. returnid;16. 17. publicvoidsetId(intid)18. this.id=id;19. 20. publicStringgetFirstName()21. returnFirstName;22. 23. publicvoidsetFirstName(StringfirstName)24. FirstName=firstName;25. 26. publicStringgetLastName()27. returnLastName;28. 29. publicvoidsetLastName(StringlastN

3、ame)30. LastName=lastName;31. 32. 2,索引的操作类 java view plaincopyprint?1. packagecom.yutel.lucene;2. 3. importjava.io.File;4. importjava.io.IOException;5. importjava.util.ArrayList;6. importjava.util.List;7. 8. importorg.apache.lucene.analysis.Analyzer;9. importorg.apache.lucene.analysis.standard.Stand

4、ardAnalyzer;10. importorg.apache.lucene.document.Document;11. importorg.apache.lucene.document.Field;12. importorg.apache.lucene.index.CorruptIndexException;13. importorg.apache.lucene.index.IndexReader;14. importorg.apache.lucene.index.IndexWriter;15. importorg.apache.lucene.index.IndexWriterConfig

5、;16. importorg.apache.lucene.index.IndexWriterConfig.OpenMode;17. importorg.apache.lucene.queryParser.ParseException;18. importorg.apache.lucene.queryParser.QueryParser;19. importorg.apache.lucene.search.IndexSearcher;20. importorg.apache.lucene.search.Query;21. importorg.apache.lucene.search.ScoreD

6、oc;22. importorg.apache.lucene.search.TopDocs;23. importorg.apache.lucene.store.FSDirectory;24. importorg.apache.lucene.util.Version;25. 26. publicclassContactInfoSearcher27. privatestaticfinalFileINDEX_DIR=newFile(d:tempindex);28. privatestaticfinalAnalyzeranalyzer=newStandardAnalyzer(Version.LUCEN

7、E_35);29. /*30. *在索引库中进行查询31. *paramfieldName32. *paramcriteria33. *return34. *throwsCorruptIndexException35. *throwsIOException36. *throwsParseException37. */38. publicListsearch(StringfieldName,Stringcriteria)39. throwsCorruptIndexException,IOException,ParseException40. IndexReaderreader=IndexRead

8、er.open(FSDirectory.open(INDEX_DIR);41. IndexSearchersearcher=newIndexSearcher(reader);42. Listlist=newArrayList();43. try44. Queryquery=buildQuery(fieldName,criteria);45. TopDocstopDocs=searcher.search(query,10);46. inttotal=topDocs.totalHits;47. System.out.println(total=+total);48. ScoreDocscoreDo

9、cs=topDocs.scoreDocs;49. for(inti=0;iscoreDocs.length;i+)50. Documentdoc=searcher.doc(scoreDocsi.doc);51. ContactInfoinfo=newContactInfo();52. info.setId(Integer.valueOf(doc.get(id);53. info.setFirstName(doc.get(firstName);54. info.setLastName(doc.get(lastName);55. list.add(info);56. 57. returnlist;

10、58. finally59. searcher.close();60. 61. 62. /*63. *将联系方式添加到索引库中64. *paramlist65. *throwsIOException66. */67. publicvoidindex(Listlist)throwsIOException68. IndexWriterwriter=openIndexWriter();69. try70. for(ContactInfocontact:list)71. Documentdocument=builderDocument(contact);72. writer.addDocument(d

11、ocument);73. 74. finally75. writer.close();76. 77. 78. 79. privateIndexWriteropenIndexWriter()throwsIOException80. IndexWriterConfigiwc=newIndexWriterConfig(Version.LUCENE_35,81. analyzer);82. iwc.setOpenMode(OpenMode.CREATE);83. returnnewIndexWriter(FSDirectory.open(INDEX_DIR),iwc);84. 85. 86. privateDocumentbuilderDocument(ContactInfocontact)87. Documentdocument=newDocument();88. Fieldid=newField(id,String.valueOf(contact.getId(),89. Field.Store.YES,Field.Index.ANAL

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号