参考文档: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. 动态规划初步--最长上升子序列(LIS)

    一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ...

  2. 77 最长公共子序列 (lintcode)

    注意:因为开的空间是length+1的,对于字符串的下标计算要-1 class Solution { public: /* * @param A: A string * @param B: A str ...

  3. tp5 -- 微信公众号支付

    近来期间比较忙, 忙完之后发现最近有挺多的东西没有整理,于是乎.就将以前用到的一些小东西整理了一下. 如果对您有帮助,则是我最大的幸运. 本篇主要是说了一下整合TP5的微信公众号支付. 不过由于最近T ...

  4. Bootstrap历练实例:危险样式按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  5. js函数式编程(三)-compose和pointFree

    compose即函数嵌套组合 组合compose在第一篇已经初见端倪,可以感受一下.compose函数的实现用闭包的方法.不完善实现如下: const compose = (f, g) => { ...

  6. 经典的7种排序算法 原理C++实现

    排序是编程过程中经常遇到的操作,它在很大程度上影响了程序的执行效率. 7种常见的排序算法大致可以分为两类:第一类是低级排序算法,有选择排序.冒泡排序.插入排序:第二类是高级排序算法,有堆排序.排序树. ...

  7. Fortran学习笔记5(数组Array)

    数组的声明方式 一维数组 二维数组 多维数组 数组索引值的改变 自定义类型的数组定义 对数组内容的设置 利用隐含式循环设置数组初值 对整个数组操作 对部分数组的操作 where函数 Forall函数 ...

  8. HDU-1217-Arbitrage(SPFA)

    这题和以往的求最短路的题目略微有点不一样,以往求的都是最小的,这题求的是大的,而且还是乘法. 我们求的时候初始化的时候就要进行相反的初始化了,把它们初始化为0,然后比较大的就更新. 因为这题的点少边多 ...

  9. hihoCoder-1089-Floyd

    我们读入的时候,要考虑重边的问题,我们只取边的最小值就可以了. #include <cstdio> #include <cstring> const int INF = 0x3 ...

  10. easyUI之datagrid绑定后端返回数据的两种方式

    先来看一下某一位大佬留下的easyUI的API对datagrid绑定数据的两种方式的介绍. 虽然精简,但是,很具有“师傅领进门,修行靠个人”的精神,先发自内心的赞一个. 但是,很多人和小编一样,第一次 ...