码迷,mamicode.com
首页 > 其他好文 > 详细

lucene学习笔记(一)

时间:2014-06-07 06:51:41      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:Lucene   c   class   java   a   ext   

package l002;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
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;

public class IndexFile1 {
protected String[] ids={"1", "2"};

protected String[] content={" I LOVE you", " I LOVE you"};

protected String[] city={"Amsterdam", "Venice"};

private Directory dir;
/**
* 初始化添加文档
* @throws IOException
*/
public void init() throws IOException{
String pathFile = "D://lucene//index";
dir = FSDirectory.open(new File(pathFile));
IndexWriter writer = getWriter();
for (int i = 0; i < ids.length; i++) {
Document doc=new Document();
doc.add(new StringField("id", ids[i], Store.YES));
doc.add(new TextField("content", content[i], Store.YES));
doc.add(new StringField("city", city[i], Store.YES));
writer.addDocument(doc);
}
System.out.println("init ok");
writer.close();
}
/*
* 获取indexWriter对象
*/
private IndexWriter getWriter() throws IOException {
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);
return new IndexWriter(dir, iwc);
}
}

 

 

package l002;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class IndexSearch1 {
public void search() throws IOException{
String pathFile = "D://lucene//index";
Directory dir = FSDirectory.open(new File(pathFile));
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Term term = new Term("content","i");
TermQuery query = new TermQuery(term);
TopDocs topDocs = searcher.search(query, 5);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("查询结果总数----"+topDocs.totalHits+" 最大的评分----"+topDocs.getMaxScore());
for (int i = 0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.println("content"+document.get("content"));
System.out.println("id--"+doc+"---scors--"+scoreDocs[i].score+"----idx----"+scoreDocs[i].shardIndex);
}
reader.close();
};
public static void main(String[] args) throws Exception {
// IndexFile1 file = new IndexFile1();
// file.init();
IndexSearch1 search = new IndexSearch1();
search.search();
}
}

lucene学习笔记(一),布布扣,bubuko.com

lucene学习笔记(一)

标签:Lucene   c   class   java   a   ext   

原文地址:http://www.cnblogs.com/mrgong/p/3770577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!