lucene学习笔记(一)

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();
}
}

转载于:https://www.cnblogs.com/mrgong/p/3770577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值