package hello;

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.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory; public class Lucene5_5_5_demo { public static void main(String[] args) throws IOException, ParseException {
Analyzer analyzer = new StandardAnalyzer(); // Store the index in memory:
Directory directory = new RAMDirectory();
// To store an index on disk, use this instead:
//Directory directory = FSDirectory.open("/tmp/testindex");
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);
Document doc = new Document();
String text = "This is the text to be indexed.";
doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close(); // Now search the index:
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
// Parse a simple query that searches for "text":
QueryParser parser = new QueryParser("fieldname", analyzer);
Query query = parser.parse("text");
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
assert(1 == hits.length);
// Iterate through the results:
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.get("fieldname"));
}
ireader.close();
directory.close();
} }

最新文章

  1. Ubuntu手动设置DSL连接
  2. Exchange 基本命令累计
  3. yii gii自动生成的curd添加批量删除实例
  4. loadrunner agent 中删除失效的mmdrv进程
  5. [转]Java Spring的Ioc控制反转Java反射原理
  6. jboss7 添加虚拟目录 上传文件路径
  7. 160829、Java加解密与数字签名
  8. 一个极其简洁的Python网页抓取程序,自己主动从雅虎財经抓取股票数据
  9. install webapp2 on Linux outside google app engine.
  10. JQuery的插件开发——重点
  11. Caused by: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual tha
  12. Python2和Python3中的字符串编码问题解决
  13. java.text.DateFormat 多线程并发问题
  14. Qt应用程序中设置字体
  15. C# 字符串转byte数组
  16. Python——lambda函数
  17. 一个时代的终结:为什么是时候放弃ITOM四大巨头了?这对IT领导者来说意味着什么?
  18. mysql数据库忘记密码时如何修改
  19. 漫画:什么是CAS机制
  20. linux常用命令:Linux 文件类型与扩展名

热门文章

  1. Eclipse编译无响应
  2. 航空售票系统设计分析(Markdownpad2图片服务器上传无法显示)
  3. go语言学习之路 一:开发环境配置
  4. 源码编译安装php
  5. SQL存储过程实例详解
  6. Intel Edision —— 开发环境选择一贴通
  7. vue - 前置工作 - 目录功能介绍
  8. grunt 试用笔记
  9. 算法和流程控制 --《高性能JavaScript》
  10. 李洪强iOS开发之-入门指南