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

lucene 5可以运行的demo的更多相关文章

  1. Apcahe Shiro学习笔记(一):简介及运行官方Demo

    一.Apache Shrio: apache shiro 是一个功能强大和易于使用的Java安全框架,为开发人员提供一个直观而全面的的解决方案的认证,授权,加密,会话管理. 支持认证跨一个或多个数据源 ...

  2. 安装meteor运行基本demo发生错误。

    bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...

  3. Windows上配置Mask R-CNN及运行示例demo.ipynb

    最近做项目需要用到Mask R-CNN,于是花了几天时间配置.简单跑通代码,踩了很多坑,写下来分享给大家. 首先贴上官方Mask R-CNN的Github地址:https://github.com/m ...

  4. Lucene 6.5.0 入门Demo

    Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...

  5. 支付宝支付接口-运行支付宝demo

    运行deme 提供了 支付  查询 退款 交易关闭几个简单的接口demo 下载 https://docs.open.alipay.com/270/106291/ 转为mave项目 1.创建一个空的ma ...

  6. 搭建 AngularJS+Ionic+Cordova 开发环境并运行一个demo

    目前的手机APP有三类:原生APP,WebAPP,HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Cordova就是一个中间件,让我们把WebAPP打包成Hybrid ...

  7. VS2010+ICE3.5运行官方demo报错----std::bad_alloc

    纠结了一晚上,在release版本下运行没问题,一到debug就报错,卡在 Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWith ...

  8. Lucene 6.5.0 入门Demo(2)

    参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description 对于path路径不是很 ...

  9. C# 定时器 一个简单 并且可以直接运行的Demo

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. Linux shell中的竖线(|)——管道符号

    管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为comma ...

  2. java对接网银支付案例

    =================提交表单jsp,http://www.cnblogs.com/qgc88================================ <%@page con ...

  3. 使用nginx时,让web取得原始请求地址

    问题描述 当使用nginx配置proxy_pass参数以后,后端web取得的Request.Uri是proxy_pass中配置的地址,而不是client访问的原始地址 举例说明: 假设nginx配置文 ...

  4. Croc Champ 2013 - Round 2 C. Cube Problem

    问满足a^3 + b^3 + c^3 + n = (a+b+c)^3 的 (a,b,c)的个数 可化简为 n = 3*(a + b) (a + c) (b + c) 于是 n / 3 = (a + b ...

  5. 全卷积网络FCN详解

    http://www.cnblogs.com/gujianhan/p/6030639.html CNN能够对图片进行分类,可是怎么样才能识别图片中特定部分的物体? (图像语义分割) FCN(Fully ...

  6. error LNK2019 无法解析的外部符号------类模板和内敛函数

    今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...

  7. iOS开发之创建颜色渐变视图View

    在iOS开发中有时需要自己自定义一个视图view的背景,而网上有人提出的在循环中不断alloc的方法设置其背景色渐变,会耗费很多内存和资源,极其不明智,而在CALayer中早就提供有图层渐变的类和相应 ...

  8. 安卓自己定义View进阶-Canvas之绘制基本形状

    Canvas之绘制基本形状 作者微博: @GcsSloop [本系列相关文章] 在上一篇自己定义View分类与流程中我们了解自己定义View相关的基本知识,只是,这些东西依然还是理论,并不能拿来(zh ...

  9. LeetCode——Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  10. JMeter 系列之—-02 创建数据库测试计划

    Jmeter创建数据库测试计划,包括如下步骤: 1. 添加数据库jar包 使用不同的数据库,要引入不同的jar包.主要有两种方式: 方式1:直接将jar包复制到jmeter的lib目录 方式2:通过测 ...