Lucene基本用法

来源:互联网 发布:飞鸟淘宝客源码下载 编辑:程序博客网 时间:2024/06/07 02:29
public class IndexManager {    @Test    public void createIndex() {        DaoImpl dao = new DaoImpl();        List<Document> docList = new ArrayList<Document>();// 创建文档对象集合        List<Student> stuList = dao.query();// 获取学生对象集合        for (Student student : stuList) {            // 1.创建文档对象            Document doc = new Document();            // 2.创建文本域对象,将域名与内容存入            // Store.NO:该域内容可用于检索,但是不能获取(即 被存入了索引库中,但是没被存入文档中)            // Store.Yes:该域内容可用于检索,也能被获取            Field id = new TextField("id", student.getId().toString(),                    Store.YES);            Field name = new TextField("name", student.getName(), Store.YES);            Field sex = new TextField("sex", student.getSex(), Store.YES);            Field birth = new TextField("birth", student.getBirth().toString(),                    Store.YES);            Field department = new TextField("department",                    student.getDepartment(), Store.YES);            Field address = new TextField("address", student.getAddress(),                    Store.NO);            // 3.将域对象放入文档中            doc.add(id);            doc.add(name);            doc.add(sex);            doc.add(birth);            doc.add(department);            doc.add(address);            docList.add(doc);        }        try {            // 4.创建分词器            Analyzer analyzer = new IKAnalyzer();            // 5.创建IndexWriter配置对象(1.版本信息入 2.分词器)            IndexWriterConfig cfg = new IndexWriterConfig(Version.LATEST,                    analyzer);            // 6.指定索引库的地址            File indexFile = new File("C:\\Users\\Administrator\\Desktop\\1");            // 7.打开索引库(地址)            Directory directory = FSDirectory.open(indexFile);            // 8.创建一个索引写入流(1.索引库 2.配置)            IndexWriter writer = new IndexWriter(directory, cfg);            // 9.通过索引写入流将文档内容写入索引            for (Document doc : docList) {                writer.addDocument(doc);            }            // 10.必须关流!!!!!!!!!!            writer.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

Store.YES 保存 可以查询 可以打印内容

Store.NO 不保存 可以查询 不可打印内容 由于不保存内容所以节省空间

Store.COMPRESS 压缩保存 可以查询 可以打印内容 可以节省生成索引文件的空间,Field

public class IndexSearch {    public static void main(String[] args) {        try {            // 1.创建一个查询分析器            QueryParser parser = new QueryParser("id", new CJKAnalyzer());            // 2.通过查询分析器获得一个查询对象            Query query = parser.parse("address:北京");            // 3.获取索引文件位置            File file = new File("C:\\Users\\Administrator\\Desktop\\1");            // 4.打开索引库,将索引文件载入Directory对象中            Directory dir = FSDirectory.open(file);            // 5.将Directory对象放入索引读取流中;            IndexReader reader = DirectoryReader.open(dir);            // 6.创建一个索引内容搜索器,从读取流中搜索信息;            IndexSearcher searcher = new IndexSearcher(reader);            // 7.从索引目录中通过Query条件获取前N条索引;            TopDocs topDocs = searcher.search(query, 10);            // 获取最终查询到的记录总数;            // System.out.println(topDocs.totalHits);            // 8.从索引中获取scoreDoc数组,scoreDoc对象中包含了 相关度得分与文档编号等信息            ScoreDoc[] scoreDoc = topDocs.scoreDocs;            // 9.遍历scoreDoc数组,获取每个文档的 相关度得分与编号等信息            for (ScoreDoc s : scoreDoc) {                // 10.获取文档编号                int id = s.doc;                // 11.通过编号在流中获取文档(7-10的目的仅仅是获取文档ID)                Document doc = searcher.doc(id);                // 12.通过文档中域的名字,获得域中的内容                System.out.println(doc.get("id"));                System.out.println(doc.get("name"));                System.out.println(doc.get("birth"));            }            reader.close();        } catch (Exception e) {            e.printStackTrace();        }    }
0 0
原创粉丝点击