参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description

对于path路径不是很清楚,参考了:http://blog.csdn.net/zhangweiwtmdbf/article/details/7099988

package com.cf.first;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
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.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; /**
* 参考文件:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description
* <p>
* Created by Administrator on 2017/4/26.
*/
public class TestLucence {
// 创建索引
@Test
public void createIndex() throws IOException {
//分词器 (对文本进行分词...)
Analyzer analyzer = new StandardAnalyzer();
File file = new File("temp/");
Path path = file.toPath();
Directory directory = FSDirectory.open(path); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//构建用于操作索引的类
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
Document document = new Document();
IndexableField filed = new TextField("id", Integer.toString(1), Field.Store.YES);
IndexableField title = new StringField("title", "Lucene_百度百科", Field.Store.YES);
IndexableField content = new TextField("content", "Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的...", Field.Store.YES); document.add(filed);
document.add(title);
document.add(content);
indexWriter.addDocument(document);
indexWriter.close();
} @Test
public void searchIndex() throws IOException, ParseException { Analyzer analyzer = new StandardAnalyzer(); File file = new File("temp/");
Path path = file.toPath();
//索引存放的位置....
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
//通过indexSearcher 去检索索引目录...
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//这个是一个搜索条件..,通过定义条件来进行查找
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse("apache");
//搜索先搜索索引目录..
//找到符合query 条件的前面N条记录...
TopDocs topDocs = indexSearcher.search(query, 100);
System.out.println("总记录数是====:" + topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//返回一个击中
for (ScoreDoc scoreDoc : scoreDocs) {
int docId = scoreDoc.doc;
Document document = indexSearcher.doc(docId);
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
} }
   //删除索引
@Test
public void deleteIndex() throws IOException {
// 创建标准分词器
Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
File file = new File("temp/"); Path path = file.toPath();
Directory directory = FSDirectory.open(path);
// 创建indexWriter
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
// 删除全部
indexWriter.deleteAll();
indexWriter.close();
}
}

--

Lucene 6.5.0 入门Demo(2)的更多相关文章

  1. Lucene 6.5.0 入门Demo

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

  2. vue入门 0 小demo (挂载点、模板、实例)

    vue入门 0 小demo  (挂载点.模板) 用直接的引用vue.js 首先 讲几个基本的概念 1.挂载点即el:vue 实例化时 元素挂靠的地方. 2.模板 即template:vue 实例化时挂 ...

  3. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  4. spring web flow 2.0入门(转)

    Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...

  5. 【SSH系列】初识spring+入门demo

    学习过了hibernate,也就是冬天,经过一个冬天的冬眠,当春风吹绿大地,万物复苏,我们迎来了spring,在前面的一系列博文中,小编介绍hibernate的相关知识,接下来的博文中,小编将继续介绍 ...

  6. 基于springboot构建dubbo的入门demo

    之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...

  7. lua入门demo(HelloWorld+redis读取)

    1. lua入门demo 1.1. 入门之Hello World!! 由于我习惯用docker安装各种软件,这次的lua脚本也是运行在docker容器上 openresty是nginx+lua的各种模 ...

  8. netty入门demo(一)

    目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...

  9. canal入门Demo

    关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...

随机推荐

  1. stringstream类的简介和用法

    一.简介 <sstream>类库定义了三种类:istringstream,ostringstream,stringstream.分别用来进行流的输入,流的输出,输入输出操作.在此演示str ...

  2. canvas 在视频中的用法

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Mysql闪回工具之binlog2sql的原理及其使用

    生产上误删数据.误改数据的现象也是时常发生的现象,作为运维这时候就需要出来补锅了,最开始的做法是恢复备份,然后从中找到需要的数据再进行修复,但是这个时间太长了,对于大表少数数据的修复来讲,动作太大,成 ...

  4. CSS3与弹性盒布局

    1.弹性盒布局对齐模式 1.1.弹性盒子 在规定弹性盒子之中的子级元素换行显示之前父级元素必须是弹性盒子模型,也就是设置 display 为 flex 代码如下: <!DOCTYPE html& ...

  5. Asp.Net Core 入门(十)—— 模型绑定和验证

    模型绑定时将Http请求中的数据映射到控制器操作方法上对应的参数,操作方法中的参数可以是简单类型,如整形,字符串等,也可以是复杂类型,如Product,Order等. Asp.Net Core MVC ...

  6. Ubuntu 18.04 上使用 OpenJDK 安装并运行 Tomcat

    在Linux上安装与卸载JDK和JRE,两种常用方法: 一.通过 apt-get 命令在线进行安装与卸载(会自动配置好环境变量) 二.通过下载并解压 .tar.gz 包进行手动安装与手动卸载(需要手动 ...

  7. javaEE(10)_jdbc基本使用

    一.JDBC简介 1.SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC,JDBC(Java Data Base Connectivity,java数据库连接) ...

  8. 两种常见JS面向象写法

    基于构造函数 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...

  9. Python自动化测试框架——数据驱动(从代码中读取)

    今天小编要介绍的是数据驱动最简单和最常用的一种方法,由于只是介绍方法,代码操作后的美观程度略有缺陷,介意者可以自行改动 还是以163邮箱登录为例: 设计一个存放数据的类,这个类的参数是我们需要修改的数 ...

  10. Life is short.,You need Python

    真棒Python  https://awesome-python.com/ 精选的Python框架,库,软件和资源的精选列表. 灵感来自awesome-php. 真棒Python 管理员面板 算法和设 ...