设为首页 加入收藏

TOP

自己动手写搜索引擎(常搜吧历程四#分词#)(Java、Lucene、hadoop)(二)
2014-11-24 12:04:51 来源: 作者: 【 】 浏览:160
Tags:自己 手写 搜索引擎 常搜吧 历程 #分词 Java Lucene hadoop
String[] names = {"张三", "李四", "李五", "赵六"};
String[] addresses = {"居住在北京", "南京", "北京海淀", "南宁"};
String[] birthdays = {"19820720", "19840203", "19770409", "19830130"};
Analyzer analyzer = new PaodingAnalyzer();
String indexDir = "E:/luceneindex";
Directory dir = FSDirectory.getDirectory(indexDir);
IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
for(int i = 0; i < ids.length; i++){
Document document = new Document();
document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("name", names[i], Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("address", addresses[i], Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("birthday", birthdays[i], Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(document);
}
writer.optimize();
writer.close();
}
}
然后来看简单的检索类
[java]
package com.qianyan.search;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class TestPaodingSearch {
public static void main(String[] args) throws IOException {
String indexDir = "E:/luceneindex";
Directory dir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(dir);
ScoreDoc[] hits = null;
/*Term term = new Term("address", "北京");
TermQuery query = new TermQuery(term);
*/
/*Term term = new Term("name", "张");
PrefixQuery query = new PrefixQuery(term);*/
Term term = new Term("name", "李*");
WildcardQuery query = new WildcardQuery(term);
TopDocs topDocs = searcher.search(query, 100);
hits = topDocs.scoreDocs;
for(int i = 0; i < hits.length; i++){
Document doc = searcher.doc(hits[i].doc);
System.out.print(hits[i].score + " ");
System.out.print(doc.get("id") + " ");
System.out.print(doc.get("name") + " ");
System.out.print(doc.get("address") + " ");
System.out.println(doc.get("birthday") + " ");
}
searcher.close();
dir.close();
}
}
下面是来看QueryParser检索类
[java]
package com.qianyan.search;
import java.io.IOException;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇设计模式详解之建造者模式 下一篇Java中的Annotation(1)----三个基..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: