每日一练

来源:互联网 发布:mac语言设置 编辑:程序博客网 时间:2024/06/12 01:05

关于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 stubString 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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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索引的建立方式。

今天的不足:

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

原创粉丝点击