数据库搜索的弊端
索引文件中只需要存放单词及文档编号即可

要查出即包含is,又包括 guangzhou及seemygo的文档,先获得包含is的文档列表,再获得包含guangzhou及seemygo的文档列表,最合做一个集合并运算,就得出文档1及文档3。
Lucene执行的搜索过程以及案例
使用步骤:
具体文件组成:
相关测试类代码:
package lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import java.io.File;
public class lucene {
String path="D:/workforce/lucene/hello"; //创建文件路径
Version version=Version.LUCENE_4_10_4; //创建Lucene的版本
//设置内容
String content1="走好选择的路,别选择好走的路,你才能拥有真正的自己。";
String content2="惟有身处卑微的人,最有机缘看到世态人情的真相。一个人不想攀高就不怕下跌,也不用倾轧排挤,可以保其天真,成其自然,潜心一志完成自己能做的事。";
String content3="我甘心当个“零”,人家不把我当个东西,我正好可以把看不起我的人看个透 ";
@Test
public void testLucene() throws Exception{
//1.定义Lucene存放文件的位置
Directory directory= FSDirectory.open(new File(path));
//2.配置分词对象
Analyzer analyzer=new StandardAnalyzer();
//3.配置对象
IndexWriterConfig config=new IndexWriterConfig(version,analyzer);
IndexWriter writer=new IndexWriter(directory,config);
//4.往库里写入内容
FieldType type=new FieldType();
type.setStored(true);
type.setIndexed(true);
//创建文档对象
Document doc=new Document();
doc.add(new Field("title","doc1",type));
doc.add(new Field("content",content1,type));
writer.addDocument(doc);
Document doc2=new Document();
doc2.add(new Field("title","doc2",type));
doc2.add(new Field("content",content2,type));
writer.addDocument(doc2);
Document doc3=new Document();
doc3.add(new Field("title","doc3",type));
doc3.add(new Field("content",content3,type));
writer.addDocument(doc3);
//5.提交资源
//6.关闭资源
writer.close();
}
}
成功标志:
用Lucene工具查看