Posted on 2010-01-12 18:10
asdtiang 阅读(1430)
评论(0) 编辑 收藏 所属分类:
搜索引擎学习
网站要用搜索功能,本来想用SQL语句来实现,后来感觉没什么意思,并开始考虑用LUCENE,去官网下载了2.9的版本,发现新东西比较多,于时下载了 2.4版本.相比以前的版本有些变化。
首先建立Index:类文件如下:
import jeasy.analysis.MMAnalyzer;
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.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
import com.lmsCard.www.bean.company.Company;
import com.lmsCard.www.tools.Init;
public class LuceneIndex {
private String INDEX_STORE_PATH;
private Analyzer analyzer;
public LuceneIndex(){
Init init=Init.getInit();
init.readInfo();
INDEX_STORE_PATH=init.LUCENE_INDEX;
System.out.println(INDEX_STORE_PATH);
analyzer = new StandardAnalyzer();
}
/////company 为一个bean
public void createCompanyIndex(Company company, boolean b) {
try {
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH + "/company",
analyzer, b, IndexWriter.MaxFieldLength.UNLIMITED);
System.out.println(INDEX_STORE_PATH+"/company");
Document doc = new Document();
// //商家名称
doc.add(new Field("companyName", company.getCompanyName(),
Field.Store.NO, Field.Index.ANALYZED));
// /商家简介
if (company.getInfo() == null) {
doc.add(new Field("info",
company.getInfo() + "www.lmscard.com", Field.Store.NO,
Field.Index.ANALYZED));
} else {
doc.add(new Field("info", company.getInfo(), Field.Store.NO,
Field.Index.ANALYZED));
}
// /折扣
doc.add(new Field("discount", company.getZhekou() + "",
Field.Store.YES, Field.Index.NOT_ANALYZED));
// //备注
if(company.getBeizhu()==null||company.getBeizhu().equals("")){
doc.add(new Field("comment", company.getBeizhu()
+ "www.lmscard.com", Field.Store.NO, Field.Index.ANALYZED));
}
doc.add(new Field("comment", company.getBeizhu()+"www.lmscard.com"
, Field.Store.YES, Field.Index.ANALYZED));
// ///商家编号
doc.add(new Field("companyId", company.getCompanyId(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
// /商家地址
if(company.getCompanyAddress()==null)
{
doc.add(new Field("address", company.getCompanyAddress()+"www.lmscard.com",
Field.Store.NO, Field.Index.ANALYZED));
}
else{
doc.add(new Field("address", company.getCompanyAddress(),
Field.Store.YES, Field.Index.ANALYZED));
}
System.out.println(company.getCompanyId());
writer.addDocument(doc);
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
////建立一个搜索类:
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Sort;
import com.lmsCard.www.tools.Init;
public class LuceneSearch {
private Analyzer analyzer;
private String type;
private String INDEX_STORE_PATH;
public LuceneSearch() {
Init init = Init.getInit();
init.readInfo();
INDEX_STORE_PATH = init.LUCENE_INDEX;
System.out.println(INDEX_STORE_PATH);
analyzer = new StandardAnalyzer();
}
public List searchCompany(String keyWord, String type1) {
List l = new ArrayList();
if (type1 == null) {
this.type = "companyName";
}
try {
BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD };
Searcher searcher = new IndexSearcher(INDEX_STORE_PATH + "/company");
System.out.println(INDEX_STORE_PATH + " ffff " + type
+ " keyWord:" + keyWord);
Query query = MultiFieldQueryParser.parse(keyWord, new String[] {
"companyName", "info", "comment", "address" }, clauses,
analyzer);
// 此处在2.0基础上有改动,此处必须传入一个返回条数,这里用searcher.maxDoc()表示返回所有条数。
Sort sort=new Sort("backMoney",true);
ScoreDoc[] docs = searcher.search(query, searcher.maxDoc()).scoreDocs;
System.out.println(docs.length + "docs.length");
Document doc;
for (int i = 0; i < docs.length; i++) {
doc = searcher.doc(docs[i].doc);
l.add(doc.get("companyId"));
System.out.println(doc.get("companyId"));
}
} catch (Exception e) {
e.printStackTrace();
}
return l;
}
}
天苍苍,野茫茫,风吹草底见牛羊