每日一练

关于lucene建立检索的练习:

package jim.java.testlucene;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class testlucene {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String indexPath = "index";//文件路径
		try {
			IndexWriter writer = new IndexWriter(indexPath,new StandardAnalyzer());//建立索引器
			Document doc = new Document();//建立文档对象
			String title = "love.txt";//文件的标题
			Field field = new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED);//建立信息字段对象
			doc.add(field);//将field添加到document里
			String content = getText(new File(title));//取得文章的内容
			//String content = "I love china";
			System.out.println(content);//输出文章的内容
			Field Content = new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED);//建立信息字段对象
			doc.add(Content);//将field添加到document里
			writer.addDocument(doc);//将Document添加到IndexWrite里
			writer.close();//关闭索引器
			System.out.println("Index Created!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static String getText(File f){
		String text = null; 
		String t = null;
		try {
			BufferedReader In = new BufferedReader(new FileReader(f));//从文件读入
			t = In.readLine();
			while(t != null)
				{text = text+(t);t = In.readLine();}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

}


新知识:

Field field = new Field(Field 名称,Field 内容,储存方式,引索方式)

IndexWrite write = new IndexWrite(储存引索的路径,分析器的实例)

使用 lucene 执行搜索,首先要创建IndexSearcher对象,然后要通过Term和Query对象来封装用户输入的搜索条件,最后将结果封装到Hits对象中,返回给用户。

IndexSearcher searcher = new IndexSearcher(索引存放路径)

创建完IndexSearcher对象后就可以使用它进行搜索,最常用的方法是seach()。其将返回一个结果集对象,即Hits

Hits h = seacher.seach()

搜索完毕后应关闭IndexSearcher对象

Term t = new Term("字段名称",“关键词”);

关于搜索的练习:

package isearch;

import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

public class Searcher{

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String indexPath = "loop";
		String searchField = "content";
		String searchPrase = "i";
		IndexSearcher searcher = new IndexSearcher(indexPath);
		Term t = new Term(searchField,searchPrase);
		Query q = new TermQuery(t);
		Hits hs = searcher.search(q);
		int num = hs.length();
		StringBuffer sb = new StringBuffer();
		for(int i = 0;i<num;i++)
		{
			Document doc = hs.doc(i);
			Field fname = doc.getField("name");
			sb.append("name: "+"\n");
			sb.append(fname.stringValue()+"\n");
			Field fcontent = doc.getField("content");
			sb.append("content: "+"\n");
			sb.append(fcontent.stringValue().substring(0,50)+"\n");
			sb.append("--------------"+"n");
			
		}

		searcher.close();
		System.out.print(sb);
	}

}

以上代码明天还要再详细的敲一边(无奈啊)

今天的收获:

了解了lucene索引的建立方式。

今天的不足:

今天白天各种活动,各种忙,晚上呢又偷了点小懒,所以今天没有学到较多的东西,估计明天也够呛,这个周末貌似平时还要忙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值