原文出处: http://blog.sina.com.cn/s/blog_694448320100lyfw.html
本篇文章主要介绍lucene建立的索引进行搜索,如果想看看索引的创建请看我的另外一篇文章《lucene学习之创建索引的方式》
1:执行搜索的基本方式:
①创建搜索器对象:IndexSearch
创建搜索器对象:IndexSearch的方法如下:
IndexSearch search = new IndexSearch(索引存放路径);
创建搜索器之后就可以使用它进行搜索了,它常用的方法是search()。使用search方法返回一个结果集对象,即Hits。例如:
Hits b = search.search();
执行完之后不要忘了关闭这个对象:
search.close();
②封转搜索条件:使用Term和Query对象
如:我们需要搜索字段为title的值里面的关键词为”中国“。
创建Term对象来封装这个搜索条件,可用:Term t = new Term("title","中国");
Term对象的方法,即:
Term t = newTerm(”字段名称“,”关键词“);
然后我们还需要创建一个query对象,从而把Term对象转化为可执行的查询条件,query对象有很多种,我们暂时只介绍最简单的TermQuery对象。用法如下:
Query q = new Query(t);
至此,用户搜索请求就被封装好了,封装在Query对象中。
③执行搜索
Hits hs = search.search(q);
④提取搜索结果:了解Hits对象
搜索结果被封转在Hits对象中,这个hits对象中有如下常用方法:
(1)Document doc(n)
返回执行序号的Document
(2)int id(n)
返回执行序号的Document的id属性
(3)int length()
返回Hits对象的长度,也就是Hits对象中包含的Document对象
(4)foat score(int n)
返回制定序号的Document的score属性(即文档得分)
有了以上的方法就可以灵活操纵搜索结果了.
⑤提取搜索结果:了解Document对象
从Hits对象中提取出Document对象之后,想要获取这个Document中含有的具体内容时,就需要了解它的一些常用方法
(1)Field getField(String name)
参数是Field名称,返回值是该Field对象
(2)List getField()
无参数,返回值是List类型,包含该Document的所有Field
(3)Enumeration fields()
无参数,返回值是Enumeration类型,包含该Document的所有Field
(4)String get(String name)
参数是Field名称,返回值是该Field对象的字符串值
⑥提取搜索结果:了解Field对象
获得了Field对象之后就可以提取具体的信息了,Field对象的常用方法如下:
(1)byte[]binaryValue();
获得指定Field对象的二进制。
(2)Reader readerValue();
获得指定Field对象的内容,以Reader内容。
(3)String stringValue();
获得指定Field对象的字符值,这个方法是最常用的。
下面一个简单的搜索来实现搜索的效果:
首先简单创建一个索引:
下面文件创建一个索引:
说明:
//LoopIndexer.java
package tianen;
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;
import java.io.*;
import tool.FileText;
import tool.FileList;
public class LoopIndexer
{
public static void main(String[] args) throws java.io.IOException
{
String indexPath = "loop";
//IndexWriter
IndexWriter writer = new IndexWriter(indexPath,new StandardAnalyzer());
String[] files = FileList.getFiles("doc");
int num = files.length;
for(int i=0;i<num;i++)
{
Document doc = new Document();
File f = new File(files[i]);
//Field -name
String name = f.getName();
Field field = new Field("name",name ,Field.Store.YES, Field.Index.TOKENIZED);
//add field
doc.add(field);
//Field -content
String content = FileText.getText(f);
field = new Field("content", content ,Field.Store.YES, Field.Index.TOKENIZED);
//add field
doc.add(field);
//Field -path
String path = f.getPath();
field = new Field("path", path ,Field.Store.YES, Field.Index.NO);
//add field
doc.add(field);
System.out.println("File : " + name + " Indexed!");
//add document
writer.addDocument(doc);
}
//close IndexWriter
writer.close();
//message
System.out.println("Loop Index Created!");
}
}
执行的之后生成的索引文件:
以上是创建了一个简单的索引器,下面就开始搜索创建过的索引文件:
//BasicSearcher.java
package tianen;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.document.*;
public class BasicSearcher
{
public static void main(String[] args) throws java.io.IOException
{
String indexPath = "loop";
String searchField = "content";
String searchPhrase = "禹";
StringBuffer sb = new StringBuffer("");
//IndexSearcher
IndexSearcher searcher = new IndexSearcher(indexPath);
//Term & Query
Term t = new Term(searchField, searchPhrase);
Query q = new TermQuery(t);
//Hits
Hits hs = searcher.search(q);
int num = hs.length();
//view details
for(int i=0;i<num;i++)
{
//get document
Document doc = hs.doc(i);
//field name
Field fname = doc.getField("name");
sb.append("name:" + "/n");
sb.append(fname.stringValue() + "/n");
//field content
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);
}
}
执行结果:
到此为止我们已经可以进行简单的根据简单的关键字搜索信息了,更复杂的关键字搜索,在之后的系列文章中会接着将luence的搜索.