近日,一直在学习lucene。应用lucene3.0编写了一段简单的代码,实现了搜索引擎最基本的功能:索引和检索。但在进行检索的时候出现了问题:在把field加入到segment后(doc1.add(new Field("content","matter bird scan cancer scan matter",Store.YES,Index.NOT_ANALYZED));),明明有两个"scan",可是在检索的时候返回的hits只有一个,请问为什么?
哪位好心人能帮我解决一下,我是个初学者,望多指教,在此拜过。
package document;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
public class fileDoc {
private Directory index_dir = null;
private String index_path = "c:\\index";
private IndexWriter write = null;
private IndexSearcher indexSearch = null;
public fileDoc() throws CorruptIndexException, LockObtainFailedException, IOException{
File file = new File(index_path);
index_dir = SimpleFSDirectory.open(file);//一般用SimpleFSDirectory创建索引目录
write = new IndexWriter(index_dir,new StandardAnalyzer(Version.LUCENE_30),true,MaxFieldLength.LIMITED);
indexSearch = new IndexSearcher(index_dir);
}
@Test
public void createIndex() throws CorruptIndexException, IOException{
//往哪儿建立索引
Document doc1 = new Document();
doc1.add(new Field("content","matter bird scan cancer scan matter",Store.YES,Index.NOT_ANALYZED));
doc1.add(new Field("title","doca",Store.YES,Index.ANALYZED));
write.addDocument(doc1);
write.close();//只有调用了close方法后,索引器才会将存放于内存中的所有内容写入磁盘,并关闭输出流。否则索引目录下将只有segment文件
}
@Test
public void search() throws CorruptIndexException, IOException, ParseException{
//生成Query对象
Query query = new BooleanQuery();
String key1 = "scan";
//检索
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30));
query = queryParser.parse(key1);
TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);//有序排列
indexSearch.search(query, collector);
System.out.println("查询到该关键词有:"+collector.getTotalHits()+"个");
}
}
哪位好心人能帮我解决一下,我是个初学者,望多指教,在此拜过。
package document;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
public class fileDoc {
private Directory index_dir = null;
private String index_path = "c:\\index";
private IndexWriter write = null;
private IndexSearcher indexSearch = null;
public fileDoc() throws CorruptIndexException, LockObtainFailedException, IOException{
File file = new File(index_path);
index_dir = SimpleFSDirectory.open(file);//一般用SimpleFSDirectory创建索引目录
write = new IndexWriter(index_dir,new StandardAnalyzer(Version.LUCENE_30),true,MaxFieldLength.LIMITED);
indexSearch = new IndexSearcher(index_dir);
}
@Test
public void createIndex() throws CorruptIndexException, IOException{
//往哪儿建立索引
Document doc1 = new Document();
doc1.add(new Field("content","matter bird scan cancer scan matter",Store.YES,Index.NOT_ANALYZED));
doc1.add(new Field("title","doca",Store.YES,Index.ANALYZED));
write.addDocument(doc1);
write.close();//只有调用了close方法后,索引器才会将存放于内存中的所有内容写入磁盘,并关闭输出流。否则索引目录下将只有segment文件
}
@Test
public void search() throws CorruptIndexException, IOException, ParseException{
//生成Query对象
Query query = new BooleanQuery();
String key1 = "scan";
//检索
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30));
query = queryParser.parse(key1);
TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);//有序排列
indexSearch.search(query, collector);
System.out.println("查询到该关键词有:"+collector.getTotalHits()+"个");
}
}