my first app——用lucene搭建简单的搜索引擎


这次的web实验应该是我第一次接触到的能够称得上是项目的东西,在此写一些具体的步骤和一些收获。

  1. 将待搜索的文件转化为lucene中的Document类
    public static Document file2Document(String path) throws IOException { // 将File转换为lucene识别的document对象
    		File file = new File(path);
    		/*
    		 * StringField:一个索引但不分词的Field,通过构造传过来的String值会是一个单独的Token,
    		 * 也就是会把这个字符串当成一个完整的词来进行索引
    		 * 
    		 * TextField:这个Field是一个索引分词,不包含term * vectors,
    		 * 例如将被用到“body”属性,包含大量文本的Document中
    		 * 
    		 * int类型的Field在一个有效的范围内过滤和排序,下面是一个例子:
    		 * document.add(new IntField(name, 6, Field.Store.NO));
    		 * 为了获得最佳性能,使用一个IntField和Document实例来保存多个文档
    		 */
    		Document doc = new Document();
    		doc.add(new StringField("name", file.getName(), Store.YES));
    		doc.add(new TextField("content", readfilecontent(file), Store.YES));
    
    		return doc;
    	}

  2. 根据生成的Document类建立索引  
    	/*
    	 * 根据传入的Document对象建立索引库
    	 */
    	public void CreateIndex(Document document) throws IOException {
    		//分词器 参数为分词器的版本
    	//	analyzer = new StandardAnalyzer(Version.LUCENE_44);
    		//索引所在的路径
    		Directory dir = FSDirectory.open(new File(indexDir));
    		//进行索引文件的写入
    		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    		IndexWriter indexWriter = new IndexWriter(dir, iwc);
    		
    		indexWriter.addDocument(document);
    		indexWriter.close();
    		
    	}

  3. 根据索引进行搜索
    /*
    	 * 在索引库中查找关键词,传入索引目录和搜索词
    	 */
    	public static void searchIndex(String indexDir,String queryStr) throws IOException, ParseException{
    		
    		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
    		//创建索引搜索器
    		Directory dir = FSDirectory.open(new File(indexDir));
    		DirectoryReader ireader = DirectoryReader.open(dir);
    		IndexSearcher isearcher = new IndexSearcher(ireader);
    		
    		//将搜索的文本解析为query对象
    		String[] searchfield = {"name","content"}; 		//在name和content域中进行搜索
    		QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_44, searchfield, analyzer);
    		Query query = parser.parse(queryStr);
    		
    		TopDocs result = isearcher.search(query, 20 );
    		System.out.println("共有"+result.totalHits+"条结果");
    		for(ScoreDoc i : result.scoreDocs){
    			int docid = i.doc;
    			Document docu = isearcher.doc(docid);
    			printDocumentInfo(docu);
    		}
    	}

至此,主要的流程就完成了。可是就这么简单的东西我花了不少时间才搞定。一是网上好多的资料都是之前的Lucene版本,二是自己英语水平好差,官方的api文档根本看不下去,到现在我的eclipse都还是汉化的。别说是英文,就是正体字我都不愿意看。

下面谈几点自己的收获和感受:

  1. 自己英语差的可以
  2. 原来自己写的那些根本就不是代码,无非就是实现一个数据结果实现一个算法,对代码的结构几乎没有要求,因此我的代码没有什么规范。看上面的方法就知道了,一会是静态方法,一会又是类的方法,一会穿参一会又不传参。很简单的东西可是总是把很多东西揉在一起,代码的复用性还很差。这种垃圾代码根本就不能达到工业级别的水平,只能自娱自乐罢了。因此要学习一些代码规范设计什么的。
  3. 自己写代码的时候脑子并不清晰,经常想起来什么就写什么。对于学校那种写个快排的程序还行,代码量稍大就不知道自己写出来的是什么了。之前听过一些培训学校的视频,感觉他们在代码清晰性、复用性和规范上比我这种所谓“科班”出身的人强太多。自己必须要写一些类似这样称得上是工程的代码,而不是自娱自乐的快排生成树啥的(当然并不是说那些不重要)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值