参考文档: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. 数据库:SQL Server自增长列的编号

    SQL Server表中的自动编号ID重新开始排列 说法一: 有两种方法: 方法1: truncate table 你的表名 --这样不但将数据删除,而且可以重新置位identity属性的字段. 方法 ...

  2. SpringMVC+Spring+Mybatis整合程序之整合

    因为每个人思路不一样,所以我在这边先分享自己的思路对于mybatis开发持久层(DAO:DataBase Access Object 持久层访问对象)有两种.第一种:传统的开发持久层方式即需要程序员开 ...

  3. Apache Commons Configuration的应用

    Apache Commons Configuration的应用 Commons Configuration是一个java应用程序的配置管理工具.可以从properties或者xml文件中加载软件的配置 ...

  4. 高德定位腾讯定位在APP上无法开启定位权限的解决方案

    [备注]公司项目中遇到的问题,如果你在团队工作其中定有不少配合方面的问题,其中的思路是可以借鉴的,因为这也许正是你们现在遇到的问题,总结的不好的地方还请多多指教 因为项目需求的确定,定位成了必不可少的 ...

  5. C++系统学习之五:表达式

    表达式由一个或多个运算对象组成,对表达式求值将得到一个结果.字面值和变量是最简单的表达式,其结果就是字面值和变量的值.把一个运算符和一个或多个运算对象组合起来可以生成较复杂的表达式. 基础 1.基本概 ...

  6. (63)zabbix low-level discover zabbix批量部署必备

    1. 概述 <zabbix发现配置>server通过配置好的规则,自动添加host.group.template <zabbix Active agent自动注册>与disco ...

  7. perl学习之:shift/unshift

    perl中shift 和unshift 操作 2008-02-02 11:18:04|  分类: Perl语言|举报|字号 订阅     ############################### ...

  8. perl的bareword

    perl的bareword可能被认为:label  . 句柄 .函数 . 普通字符串. 上下文不同,解释器有歧义. 最好用 use strict; use warning;

  9. 《linux设备驱动开发详解》笔记——6字符设备驱动

    6.1 字符设备驱动结构 先看看字符设备驱动的架构: 6.1.1 cdev cdev结构体是字符设备的核心数据结构,用于描述一个字符设备,cdev定义如下: #include <linux/cd ...

  10. day22面向对象

    面向对象编程: 1.什么是面向对象 面向过程(编程思想): 过程,解决问题的步骤,流程即第一步做什么,第二步做什么 将复杂问题,拆成若干小问题,按照步骤一一解决,将复杂问题流程化(为其制定固定的实现流 ...