1. 案例:

Article.java

package cn.toto.lucene.quickstart;

publicclassArticle
{

privateintid;

private
Stringtitle;

private
Stringcontent;

/**

*
@return the id

*/

publicint
getId() {

returnid;

}

/**

*
@param id the id to set

*/

publicvoid
setId(int id) {

this.id
= id;

}

/**

*
@return the title

*/

public
String getTitle() {

returntitle;

}

/**

*
@param title the title to set

*/

publicvoid
setTitle(String title) {

this.title
= title;

}

/**

*
@return the content

*/

public
String getContent() {

returncontent;

}

/**

*
@param content the content to set

*/

publicvoid
setContent(String content) {

this.content
= content;

}

}

工具类Configuration.java

package cn.toto.lucene.util;

import java.io.File;

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

/**

* @brief Configuration.java配置对象,提供Lucene需要  索引目录,分词器

* @attention

* @author toto

* @date 2014-12-7

* @note begin modify by涂作权

*/

public class Configuration {

private static Directory directory;

private static Analyzer analyzer;

private static Version version;

static {

try {

//设置磁盘目录,表示的是本地index目录

directory = FSDirectory.open(new File("index"));

} catch (Exception e) {

e.printStackTrace();

}

//表示LUCENE版本

version = Version.LUCENE_36;

//表示使用版本

analyzer = new StandardAnalyzer(version);

}

//提供目录

public static Directory getDirectory()

{

return directory;

}

//提供分词器

public static Analyzer getAnalyzer()

{

return analyzer;

}

//获取版本

public static Version getVersion()

{

return version;

}

}

工具类LuceneUtils.java

package cn.toto.lucene.util;

import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.search.IndexSearcher;

// lucene工具类

public class LuceneUtils {

private static IndexWriter indexWriter;

static {

//
索引目录位置

try {

//
写入索引

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(

Configuration.getVersion(), Configuration.getAnalyzer());

indexWriter = new IndexWriter(Configuration.getDirectory(),

indexWriterConfig);

//
绑定虚拟机退出事件,关闭IndexWriter

Runtime.getRuntime().addShutdownHook(new Thread() {

@Override

public void run() {

try {

indexWriter.close();

} catch (CorruptIndexException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

//
提供获取IndexWriter对象

public static IndexWriter getIndexWriter() {

return indexWriter;

}

//
获得查询IndexSeacher对象

public static IndexSearcher getIndexSearcher() throws Exception {

return new IndexSearcher(IndexReader.open(Configuration.getDirectory()));

}

}

LuceneTest.java

package cn.toto.lucene.api;

import java.io.File;

importjava.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.Field.Store;

import org.apache.lucene.index.CorruptIndexException;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.queryParser.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.apache.lucene.store.LockObtainFailedException;

import org.apache.lucene.store.RAMDirectory;

import org.apache.lucene.util.Version;

import org.junit.Test;

import cn.toto.lucene.quickstart.Article;

import cn.toto.lucene.util.LuceneUtils;

// API详细分析

publicclass
LuceneTest {

@Test

//使用LuceneUtils解决
IndexWriter并发问题

@SuppressWarnings("unused")

publicvoid
testLock2() {

IndexWriter indexWriter2 = LuceneUtils.getIndexWriter();

IndexWriter indexWriter1 = LuceneUtils.getIndexWriter();

}

@Test

@SuppressWarnings("all")

//使用两个IndexWrtier报错,锁使用问题

publicvoid
testLock()throws CorruptIndexException,

LockObtainFailedException,
IOException {

//索引目录位置

Directory directory = FSDirectory.open(new
File("index"));//当前工程index目录

//分词器

Analyzer analyzer =
new StandardAnalyzer(Version.LUCENE_36);

//写入索引

IndexWriterConfig indexWriterConfig =
new IndexWriterConfig(

Version.LUCENE_36,
analyzer);

IndexWriter indexWriter =
new IndexWriter(directory, indexWriterConfig);

IndexWriterConfig indexWriterConfig2 =
new IndexWriterConfig(

Version.LUCENE_36,
analyzer);

IndexWriter indexWriter2 =
new IndexWriter(directory,

indexWriterConfig2);

}

上面的运行结果如下:

@Test

//测试Store和
Index

/*

* Store.YES
存储、Store.NO不存储
Index.NO不建立索引 Index.ANALYZED分词建立索引

* Index.NOT_ANALYZED
不分词建立索引Index.ANALYZED_NO_NORMS
分词建立索引,不存放权重信息

* Index.NOT_ANALYZED_NO_NORMS
不分词建立索引,不存放权重信息

*/

publicvoid
testIndex()throws Exception {

//需要建立索引目标数据

Article article =
new Article();

article.setId(100);

article.setTitle("学习全文检索");

article.setContent("lucene是搜索引擎开发技术,lucene并不是一个现成的产品,由Apache提供");

//将索引数据转换
Document对象(lucene要求)

Document document =
new Document();

document.add(new
Field("id", article.getId() +
"", Store.YES,

Field.Index.NOT_ANALYZED));//对于id通常不分词

document.add(new
Field("title", article.getTitle(), Store.YES,

Field.Index.ANALYZED_NO_NORMS));

document.add(new
Field("content", article.getContent(), Store.YES,

Field.Index.ANALYZED));

//建立索引库

//索引目录位置

Directory directory = FSDirectory.open(new
File("index"));//当前工程index目录

//分词器

Analyzer analyzer =
new StandardAnalyzer(Version.LUCENE_36);

//写入索引

IndexWriterConfig indexWriterConfig =
new IndexWriterConfig(Version.LUCENE_36,
analyzer);

IndexWriter indexWriter =
new IndexWriter(directory, indexWriterConfig);

//将document数据写入索引库

indexWriter.addDocument(document);

indexWriter.close();

}

上面的单元测试的结果

@Test

//查询索引库
,查看norms效果

publicvoid
testQuery()throws Exception {

//建立Query对象
---- 根据标题

String queryStrng =
"检索";

Analyzer analyzer =
new StandardAnalyzer(Version.LUCENE_36);

QueryParser queryParser =
new QueryParser(Version.LUCENE_36,"title",

analyzer);

Query query = queryParser.parse(queryStrng);

//根据Query查找

Directory directory = FSDirectory.open(new
File("index"));

IndexSearcher indexSearcher =
new IndexSearcher(

IndexReader.open(directory));

//执行查询获得满足结果前多少条记录

TopDocs topDocs = indexSearcher.search(query, 100);//查询满足结果前100条数据

System.out.println("满足结果记录条数:"
+ topDocs.totalHits);

//获得每个结果

ScoreDoc[] scoreDocs = topDocs.scoreDocs;

for
(int i = 0; i < scoreDocs.length;
i++) {

//打印得分

System.out.println("得分:"
+ scoreDocs[i].score);

//获得Document下标

int
docID = scoreDocs[i].doc;

Document document = indexSearcher.doc(docID);

System.out.println("id:"
+ document.get("id"));

System.out.println("title:"
+ document.get("title"));

System.out.println("content:"
+ document.get("content"));

}

indexSearcher.close();

}

上面的运行单元测试之后的结果如下:

@SuppressWarnings("unused")

@Test

//测试路径写法

publicvoid
testDirectory()throwsIOException
{

//磁盘路径

FSDirectory.open(new
File("index"));//当前工程index目录,相对路径

FSDirectory.open(new
File("d:\\index"));//绝对路径

//类路径
WEB-INF/classes

FSDirectory.open(new
File(LuceneTest.class.getResource("/").getFile()));

//内存路径

Directory directory =
new RAMDirectory();

}

}

4.Lucene3.案例介绍,创建索引,查询等操作验证的更多相关文章

  1. java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作

    官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...

  2. 关于怎么C#控制台窗口中怎么创建连接查询数据库操作

    首先需要新建一张表,为了测试随建了一张学生表 新建号一张表之后就可以对数据库进行操作了 列举了常用的增删改查 操作 static void Main(string[] args)        { s ...

  3. Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!

    一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...

  4. MySQL索引详解(优缺点,何时需要/不需要创建索引,索引及sql语句的优化)

     一.什么是索引? 索引是对数据库表中的一列或多列值进行排序的一种结构,使用索引可以快速访问数据库表中的特定信息. 二.索引的作用? 索引相当于图书上的目录,可以根据目录上的页码快速找到所需的内容,提 ...

  5. Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

    2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程 2014-12-07 23:39 2623人阅读 评论(0) ...

  6. SQL Server 查询性能优化——创建索引原则(二)

    三:索引的建立原则 一般来说,建立索引要看数据使用的场景,换句话来说哪些访问数据的SQL语句是常用的,而这些语句是否因为缺少索引(也有可能是索引过多)变的效率低下.但绝不是所有的SQL语句都要建立索引 ...

  7. SQL Server 查询性能优化——创建索引原则

    索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索引 ...

  8. mysql索引原理及创建与查询

    索引介绍 一:为什么要有索引 索引是用来优化查询效率(速度)的 没有索引的话,对于大数据的表,就只能每次都遍历一遍,数据量越大,耗时越多有索引的话,可以提升好几个数量级的速度 一般的应用系统,读写比例 ...

  9. ArcGIS Engine 创建索引(属性索引)——提高查询效率

    转自原文 ArcGIS Engine 创建索引(属性索引)——提高查询效率 众所周知,建立索引可以提高查询的效率,当对FeatureClass中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...

随机推荐

  1. bzoj 4518: [Sdoi2016]征途

    Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...

  2. HDU2256(矩阵)

    求(sqrt(2) + sqrt(3)) ^ 2n MOD 1024 强行盗图- -,这公式推得 .. #include <iostream> #include <cstdio> ...

  3. hdu 5645 DZY Loves Balls

    DZY Loves Balls  Accepts: 659  Submissions: 1393  Time Limit: 4000/2000 MS (Java/Others)  Memory Lim ...

  4. POJ 3045 Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  5. c语言第二次作业2

    ---恢复内容开始--- (一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. 源程序 对源程序进行编译 错误信息1: 错误原因:stdio.h输入错误 改正方法:i和d互换位置 错误信息2: ...

  6. php+xdebug+dbgp远程调试(多人)

    目录 创建 DBGP 服务 配置 调试 创建 DBGP 服务 到 下载页面,下载 python 版本的 dbgp 到服务器上. 解压后执行 pydbgpproxy. 如果提示找不到 dbgp 模块,则 ...

  7. JSON.NET的Self referencing loop detected with type的原因以及解决办法

    模型中有循环引用是很常见的.例如,以下模型显示双向导航属性: : public class Category : { : public Category() : { : Products = new ...

  8. JNI 方法注册与签名+BufferedReader使用readLine问题

    最近了解了关于JavaJNI接口的一些关于方法注册与签名相关的知识,在此进行一下总结. 使用JNI接口时,我们首先需要把Java方法声明为native: public native void f(); ...

  9. js页面(页面上无服务端控件,且页面不刷新)实现请求一般处理程序下载文件方法

    对于js页面来说,未使用服务端控件,点击下载按钮时不会触发服务端事件,且不会提交数据到服务端页面后台进行数据处理,所以要下载文件比较困难.且使用jQ的post来请求一般处理程序也不能实现文件的下载,根 ...

  10. Unity中使用射线查询MeshCollider背面的方法

    之前遇到一个问题要从MeshCollider背面方向发出射线,直至检测到该射线与MeshCollider的相交点为止. 后来我用双面MeshCollider的方法解决了http://www.cnblo ...