基于3.5
package com.supben;
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
/**
* 索引建立类
*
*/
public class IndexMaker {
private static File INDEX_DIR = new File("./index");// 索引文件夹
public static void make(String id, String name, String desc) {
try {
// 字段ID,可查询,不分词
Field ID = new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED);
// 字段NAME,可查询,分词
Field NAME = new Field("name", name, Field.Store.YES, Field.Index.ANALYZED);
// 字段描述,可查询,分词
Field DESC = new Field("desc", desc, Field.Store.YES, Field.Index.ANALYZED);
Document d = new Document();
d.add(ID);
d.add(NAME);
d.add(DESC);
IndexWriter iw = new IndexWriter(FSDirectory.open(INDEX_DIR), new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
iw.addDocument(d);
iw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
make("1", "shen yuhan", "god is a girl");
make("2", "shen chenglong", "this is man");
make("3", "li supben", "war3 tower rpg man");
make("4", "hu jintao", "is a dog");
}
}
package com.supben;
import java.io.File;
import org.apache.lucene.document.Document;
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.FSDirectory;
public class Searcher {
private static File INDEX_DIR = new File("./index");// 索引文件夹
public static void getAll() {
try {
IndexSearcher searcher = new IndexSearcher(FSDirectory.open(INDEX_DIR));
TermQuery query = new TermQuery(new Term("name", "shen"));
TopDocs topdocs = searcher.search(query, 10);
ScoreDoc[] hits = topdocs.scoreDocs;
for (ScoreDoc hit : hits) {
Document doc = searcher.doc(hit.doc);
String id = doc.get("id");
String name = doc.get("name");
System.out.println("查询的文档的id是:" + id + ",名称是:" + name);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getAll();
}
}
- 大小: 8.2 KB
分享到:
相关推荐
详细介绍了Lucene的原理及使用方法,配合代码,适合初学者使用
NULL 博文链接:https://navylee.iteye.com/blog/740126
.net的一个全文检索小例子 基于lucune 和盘古分词器
主要包含Lucene.net 学习笔记和 Lucene.net 系列的代码,一直一些简单的程序
建立搜索引擎四步: 抓取数据---》解析数据----》创建索引-----》执行搜索 今天做了一个简单示例,用的lucune,跟教科书上的有些不同,但教科书上的示例都能正常执行。
Lucene笔记