package com.jiaoyiping.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.IOException;
import java.nio.file.Paths; /**
* Created with IntelliJ IDEA.
* User: 焦一平
* Date: 2015/3/21
* Time: 15:40
* To change this template use File | Settings | File Templates.
*/
public class TestLuceneIndex {
private static String indexPath = "d:\\lucene";
@Test
public void testIndex() throws IOException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
IndexWriter writer = new IndexWriter(directory,new IndexWriterConfig(new StandardAnalyzer()));
Document document = new Document();
//Field有多种实现类可选,不同的实现类有不同的索引策略
document.add(new TextField("name","张三", Field.Store.YES));
document.add(new IntField("age",23,Field.Store.YES));
writer.addDocument(document);
writer.commit();
writer.close();
}
@Test
public void testSearch() throws IOException, ParseException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser queryParser = new QueryParser("name",new StandardAnalyzer());
Query query = queryParser.parse("张三");
TopDocs tds = searcher.search(query, 10);
ScoreDoc[] sds = tds.scoreDocs;
for(ScoreDoc sd: sds){
Document document = searcher.doc(sd.doc);
System.out.println(document.get("name"));
System.out.println(document.get("age"));
}
}
}

以上是Lucene最基本的索引和搜索的使用(基于Lucene5.0版本)

执行构建索引操作之后会在索引存放目录下生成索引文件。执行搜索的时候由IndexReader来读取这些索引文件

Lucene中最简单的索引和搜索示例的更多相关文章

  1. 使用Lucene.NET实现简单的站内搜索

    使用Lucene.NET实现简单的站内搜索 导入Lucene.NET 开发包 Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和 ...

  2. 用Lucene.net对数据库建立索引及搜索<转>

    用Lucene.net对数据库建立索引及搜索 最近我一直在研究 Lucene.net ,发现Lucene.net对数据库方面建索引的文章在网上很少见,其实它是可以对数据库进行索引的,我闲着没事,写了个 ...

  3. 在Hadoop分布式文件系统的索引和搜索

    FROM:http://www.drdobbs.com/parallel/indexing-and-searching-on-a-hadoop-distr/226300241?pgno=3 在今天的信 ...

  4. (二)Luence——代码实现索引及搜索

    完成需求:使用Lucene完成对数据库中图书信息的索引和搜索功能. 1. 环境准备及工程搭建 1.1 环境准备 mysql5.5+java8+lucene4.10.3(目前最新7.0.1,这里够用就好 ...

  5. 理解Lucene索引与搜索过程中的核心类

    理解索引过程中的核心类 执行简单索引的时候需要用的类有: IndexWriter.ƒDirectory.ƒAnalyzer.ƒDocument.ƒField 1.IndexWriter IndexWr ...

  6. Lucene.net 从创建索引到搜索的代码范例

    关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...

  7. lucene简介 创建索引和搜索初步

    lucene简介 创建索引和搜索初步 一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引 ...

  8. 用Lucene对文档进行索引搜索

    问题 现在给出很多份文档,现在对某个搜索词感兴趣,想找到相关的文档. 简单搜索 一种简单粗暴的做法是: 1.读取每个文档:2.找到其中含有搜索词的文档:3.对找到的文档中搜索词出现的次数统计:4.根据 ...

  9. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

随机推荐

  1. css 3 制作水波状进度条

    效果图如下 : 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  2. tp-01 搭建过程

    1:拷贝ThinkPHP框架系统文件夹自己的www目录中的tp-shop文件夹中 2:新建自己的项目文件(比如:shop)夹与ThinkPHP框架系统文件夹在同一级目录(当然也可以不同) 3: 在tp ...

  3. gpio irq

    /***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...

  4. tftp32作为dhcp服务器

    /******************************************************************* * tftp32作为dhcp服务器 * 每次使用tftp进行文 ...

  5. c# combobox 绑定报错

    comboBoxPlanResult.DataSource =new BindingSource(o,null);comboBoxPlanResult.DisplayMember ="Key ...

  6. 帝国cms的list.var中使用php函数

    $r[title] = esub($r[title],8,'...'); //截取前8个字符,多出部分用...代替 $r[title] = str_replace("lhj",&q ...

  7. 转载:15个最受欢迎的Python开源框架

    出自:http://python.jobbole.com/72306/?replytocom=57112 15个最受欢迎的Python开源框架 Django: Python Web应用开发框架 Dja ...

  8. php对gzip的使用(实例)

    代码如下: if (!function_exists('gzdecode')) { function gzdecode ($data) { $flags = ord(substr($data, 3, ...

  9. 函数preg_replace()与str_replace()

    如截图,preg_replace()的用法 <?php $str="as2223adfsf0s4df0sdfsdf"; echo preg_replace("/0/ ...

  10. path与classpath的差别

    1.path的作用     path是系统用来指定可运行文件的完整路径.即使不在path中设置JDK的路径也可运行JAVA文件,但必须把完整的路径写出来,如C:\Program Files\Java\ ...