Lucene使用(一)
来源:互联网 发布:大司马杂货铺淘宝店 编辑:程序博客网 时间:2024/05/17 08:12
/** * 增加索引 * @throws IOException */@Testpublic void addIndex() throws IOException {//1.初始化索引StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);Directory dir = FSDirectory.open(new File("/root/testindex"));IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);IndexWriter writer = new IndexWriter(dir,iwc);//2.添加数据String[] ids = new String[]{"1","2","3","4","5",};String[] names = new String[]{"需要开启","需响应正文返回需要","调用适合","正文转换","增加了新注解 "};for(int i = 0 ; i< ids.length; i++){Document d = new Document();Field name = new Field("name", names[i], Store.YES, Index.ANALYZED);Field id = new Field("id", ids[i], Store.YES, Index.ANALYZED);d.add(name);d.add(id);writer.addDocument(d);}//3.保存数据writer.close();}
/** * 查询索引 * @throws IOException * @throws ParseException */@Testpublic void queryIndex() throws IOException, ParseException{String key = "需"; //搜索关键字//1.打开索引Directory dir = FSDirectory.open(new File("/root/testindex"));IndexReader reader = IndexReader.open(dir); //打开索引//2.初始化查询组建IndexSearcher searcher = new IndexSearcher(reader); //查询器StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); //分词器QueryParser parser = new QueryParser(Version.LUCENE_36,"name", analyzer); //对name查询解析器//3.查询Query query = parser.parse(QueryParser.escape(key)); //根据关键字返回封装的查询对象TopDocs docs = searcher.search(query, 10); //查询ScoreDoc[] scoreDocs = docs.scoreDocs;for(ScoreDoc s : scoreDocs){ Document doc = searcher.doc(s.doc);//获得docuemnt的id String id = doc.getFieldable("id").stringValue(); String name = doc.getFieldable("name").stringValue(); System.out.println("id :" + id +" name :" + name);}reader.close();searcher.close();}
0 0
- Lucene的使用(一)
- Lucene使用(一)
- Lucene 使用(一)简介
- Lucene学习使用小结(一)
- ASP.NET中使用Lucene.net(一)
- Lucene使用入门指南 (一)
- Lucene简单使用一之文件索引
- Lucene 实例教程(一)初识Lucene
- Lucene 实例教程(一)初识Lucene
- lucene使用
- Lucene-使用
- 使用Lucene进行全文检索(一)---处理索引
- 使用lucene建立搜索引擎学习记录(一)
- Lucene.Net学习 TermQuery,NumericRangeQuery,TermRangeQuery的使用(一)
- 全文检索引擎lucene的研究和使用(一)
- Lucene.NET使用入门(一)【实现快速搜索】
- Lucene使用(一)简单索引的创建和检索
- Lucene.net 系列一
- Iterator的作用以及遍历LinkedList双向链表的效率
- Hive over HBase和Hive over HDFS性能比较分析
- 初当项目经理-表示要学习的东西还很多
- android 为TextView添加边框
- C++回调函数
- Lucene使用(一)
- 剑指offer 面试题18 判断二叉树B是否是A的子结构
- nginx: [error] invalid PID number "" in "/usr/local/webserver/nginx/logs/nginx.pid"
- php模拟post提交数据
- 在vs2013中使用正则表达式查找和替换文本内容
- CentOS SVN安装与配置
- 数据库的CMD操作
- uva 350 Pseudo-Random Numbers
- Construct Binary Tree from Preorder and Inorder Traversal