• 需求:模糊搜索。
  • 前提:  本例中使用lucene 5.3.0
package com.shyroke.lucene;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths; 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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.queries.function.valuesource.DualFloatFunction;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory; public class Indexer {
// 写索引
private IndexWriter indexWriter; /**
* 实例化写索引
*
* @param dir
* 保存索引的目录
* @throws IOException
*/
public Indexer(String dir) throws IOException {
Directory indexDir = new SimpleFSDirectory(Paths.get(dir)); /**
* IndexWriterConfig实例化该类的时候如果是空的构造方法,那么默认 public IndexWriterConfig() { this(new
* StandardAnalyzer()); }
*/ Analyzer analyzer=new StandardAnalyzer(); //分词器
IndexWriterConfig conf = new IndexWriterConfig(analyzer); indexWriter = new IndexWriter(indexDir, conf);
} /**
* 索引文件
*/ public void index(File file) throws Exception {
System.out.println("被索引的文件为:" + file.getCanonicalPath());
Document document = getDocument(file);
indexWriter.addDocument(document); } /**
* 从文件中获取文档
*
* @param file
* @return
* @throws IOException
*/
private Document getDocument(File file) throws IOException {
Document document = new Document(); Field contentField = new TextField("fileContents", new FileReader(file));
/**
* Field.Store.YES表示把该Field的值存放到索引文件中,提高效率,一般用于文件的标题和路径等常用且小内容小的。
*/
Field fileNameField = new TextField("fileName", file.getName(), Field.Store.YES);
Field filePathField = new TextField("filePath", file.getCanonicalPath(), Field.Store.YES); document.add(contentField);
document.add(fileNameField);
document.add(filePathField); return document;
} /**
* 创建索引
*
* @param dataFile 数据文件所在的目录
* @return 索引文件的数量
* @throws Exception
*/
public int CreateIndex(String dataFile, FileFilter filter) throws Exception { File[] files = new File(dataFile).listFiles(); for (File file : files) {
/**
* 被索引文件必须不能是 1.目录 2.隐藏 3. 不可读 4.不是txt文件,
* 否则不被索引
*/ if (!file.isDirectory() && !file.isHidden() && file.canRead() && filter.accept(file)) {
index(file);
} } return indexWriter.numDocs();
} /**
* 关闭写索引
*
* @throws IOException
*/
public void close() throws IOException {
indexWriter.close(); } }
  • 这个类用来遍历数据文件夹,生成索引文件。
  • 对特定项搜索

public class SearchTest {

    private IndexWriter writer;
private IndexSearcher search;
private IndexReader reader;
private String indexDir = "E:\\\\lucene4\\\\index";
private String dataDir = "E:\\\\lucene4\\\\data"; @Before
public void setUp() throws Exception {
Indexer indexer = new Indexer(indexDir);
indexer.CreateIndex(dataDir, new FileFilter());
/**
* 一定要把IndexWriter实例关闭,否则segments_1文件不会生成。
*/
indexer.close(); Directory indexDirectory = FSDirectory.open(Paths.get(indexDir));
reader = DirectoryReader.open(indexDirectory);
search = new IndexSearcher(reader);
} @After
public void tearDown() throws Exception {
reader.close();
} /**
* 对特定项搜索
* @throws IOException
*/
@Test
public void textTermQuery() throws IOException {
System.out.println("--------------------");
String key = "particular";
Term t = new Term("fileContents", key);
Query query = new TermQuery(t);
TopDocs hits = search.search(query, 10);
System.out.println("匹配 '" + key + "',总共查询到" + hits.totalHits + "个文档"); for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = search.doc(scoreDoc.doc);
System.out.println(doc.get("filePath"));
}
} }
  • 注意:上述代码中的橙色标注代码,一定要把IndexWriter实例关闭,否则segments_1文件不会生成。

结果:

  • 解析:对特定项搜索的方法是以搜索关键字作为单位查询,如果把关键字key改为key="particul" ,则结果如下,无法匹配到particular:

  • 解析查询表达式

/**
* 解析查询表达式,在要搜索的关键字中可以使用AND OR ~ * ?等
* AND 与 OR 或 ~相近
* AND和OR只能大写
* @throws ParseException
* @throws IOException
*/
@Test
public void testQueryParse() throws ParseException, IOException {
System.out.println("--------------------");
Analyzer analyzer=new StandardAnalyzer();
QueryParser parser=new QueryParser("fileContents", analyzer);
String key="Source* AND Derivati*";
Query query=parser.parse(key);
TopDocs hits =search.search(query, 10); System.out.println("匹配 '" + key + "',总共查询到" + hits.totalHits + "个文档"); for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = search.doc(scoreDoc.doc);
System.out.println(doc.get("filePath"));
}
}

结果:

  • 查看LICENSE.txt文档,

最新文章

  1. [2014.01.27]wfPng 水印贴图组件 2.1
  2. 【C语言入门教程】2.1 数据类型(5种基本数据类型),聚合类型与修饰符
  3. Javascript中String对象的的简单学习
  4. PHP安装pthreads多线程扩展教程[windows篇]
  5. java整数类型
  6. cygwin and its host machine
  7. 第一次使用easyUI
  8. wap_supplicant介绍
  9. [iOS基础控件 - 5.4] 广告分页代码(UIScrollView制作)
  10. UNDO 100%
  11. iOS 8.0正式公布啦
  12. AutoMapper 创建嵌套对象映射(原创)
  13. hibernate学习之持久化对象
  14. SpringCloud学习系列之六 ----- 路由网关Zuul基础使用教程
  15. 【Javascript系列】变量作用域
  16. mysql-备份数据库脚本
  17. shell编程 之 ssh远程连接
  18. MapReduce原理
  19. PHP pthread 多线程 案例
  20. 安装MACOS操作步骤详解

热门文章

  1. Idea 运行测试NoSuchMethodError Junit5
  2. BitmapFactory之Options
  3. [MongoDB教程] 2.MongoDB的安装与使用
  4. xshell6破解4窗口限制
  5. python调用shell命令
  6. 案例一:利于Python调用JSON对象来实现对XENA流量测试仪的灵活发包测试,能够适应Pair,Rotate,1-to-Many等多种拓扑模型
  7. 查询 ip占用导致ip不通的 问题 查IP对应的mac地址
  8. 搭建sqli靶场
  9. Composer 笔记
  10. windows下进程与线程