ackage com.zxf.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader; 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.StringField;
import org.apache.lucene.document.TextField;
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.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.util.Version; public class LuceneForuDemo {
static final String INDEXPATH = System.getProperty("user.dir") + "\\index";
static final String DATAPATH = System.getProperty("user.dir") + "\\data"; public static void main(String[] args) {
try{
LuceneForuDemo.indexDirectory();
LuceneForuDemo.search();
}catch (Exception e) {
e.printStackTrace();
}
} /**
* 建立索引
* @throws Exception
*/
public static void indexDirectory() throws Exception{
File indexDir = new File(LuceneForuDemo.INDEXPATH);
File dataDir = new File(LuceneForuDemo.DATAPATH); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
Directory dir = FSDirectory.open(indexDir);
IndexWriter indexWriter = new IndexWriter(dir, iwc); File[] dataFiles = dataDir.listFiles();
for (File file : dataFiles) {
FileInputStream inStream = new FileInputStream(file);
Document doc = new Document();
Field fullFileName = new StringField("fullFileName", file.getCanonicalPath(),Field.Store.YES);
doc.add(fullFileName);
doc.add(new TextField("contents", new BufferedReader(
new InputStreamReader(inStream, "UTF-8")
)
)
);
indexWriter.addDocument(doc);
}
indexWriter.close();
} /*搜索*/
public static void search() throws Exception{
String field = "contents";
String queryStr = "test"; //搜索的字符串
File indexDir = new File(LuceneForuDemo.INDEXPATH);
IndexReader reader = DirectoryReader.open(FSDirectory.open(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
Query query = parser.parse(queryStr);
System.out.println("Searching for: " + query.toString(field));
TopDocs results = searcher.search(query, 50);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
System.out.println(numTotalHits + " total matching in documents");
for(ScoreDoc sd:hits){
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("fullFileName"));
} }
}

lucene 4.4 demo的更多相关文章

  1. 基于Lucene的文件检索Demo

    通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...

  2. Lucene搜索引擎例子demo

    一.导入相应的jar包 KAnalyzer3.2.0Stable.jar lucene-analyzers-3.0.1.jar lucene-core-3.0.1.jar lucene-highlig ...

  3. 一个Lucene.Net的Demo

    今天突然想来看一下全文检索,于是就了解了一下Lucene.Net,然后把公司目前的产品表拿来练手,写了这么个Demo. 先看一下Demo的代码 public class ProductReposito ...

  4. lucene简单搜索demo

    方法类 package com.wxf.Test; import com.wxf.pojo.Goods; import org.apache.lucene.analysis.standard.Stan ...

  5. lucene简单使用demo

    测试结构目录: 1.索引库.分词器 Configuration.java package com.test.www.web.lucene; import java.io.File; import or ...

  6. Lucene站内搜索的设计思路

    正好近期部门有一个小需求需要做商品的搜索,虽然最终由于工作量等原因先做数据库搜索,我依然用刚接触的Lucene弄了一套自嗨. 首先看需求:搜索:根据商品标题和内容搜索 没错,就这么简单! 我想了想,数 ...

  7. Lucene.Net 入门级实例 浅显易懂。。。

    Lucene.Net 阅读目录 开始 Lucene简介 效果图 Demo文件说明 简单使用 重点类的说明 存在问题 调整后 Lucene.Net博文与资源下载 做过站内搜索的朋友应该对Lucene.N ...

  8. 【转】lucene4.3.0 配置与调试

    lucene4.3.0 配置与调试 demo lucene的最新版本是4.3.0, http://www.apache.org/dyn/closer.cgi/lucene/java/4.3.0 luc ...

  9. lucene 索引 demo

    核心util /** * Alipay.com Inc. * Copyright (c) 2004-2015 All Rights Reserved/ */ package com.lucene.de ...

随机推荐

  1. Oracle.ManagedDataAccessDTC.dll 使用

    ODP.NET, Managed Driver Setup This section explains the setup and configuration steps required for u ...

  2. 通过js获取计算机内网ip,计算机名,mac地址

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. Hadoop学习记录(2)|HDFS shell命令|体系结构

    HDFS的shell 调用文件系统(FS)shell命令使用hadoop fs的形式 所有的FS shell命令使用URI路径作为参数. URI格式是scheme://authority/path.H ...

  4. HW5.7

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. HW2.7

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. Windows Server 2008 R2 配置AD(Active Directory)域控制器 -zhai zi wangluo

    http://files.cnblogs.com/zhongweiv/Windows_Server_2008_R2_%E9%85%8D%E7%BD%AEActive_Directory%E5%9F%9 ...

  7. PC-如何提高计算机的启动和关机的速度?

    如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...

  8. 框架技术--S2SH框架整合(spring部分)No 3--声明式事务

    声明式事务:就是讲事务的处理,通过配置进行配置. 几种传播特性  1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启(比较常用)  2. PROPA ...

  9. 性能优化-列表类型转换(ConvertList<TSource, TResult>)

    之前,在项目中看到过一段通用列表类型转换的代码,直接的实现便是用反射.大概看了下,它用在领域模型转DTO和SOA接口中契约实体的转换等等.首先,它使用了反射,其次,还是在循环中使用,便有了优化的想法. ...

  10. IOS GCD 使用(三)单例模式

    一  Dispatch_once函数简介      使用dispatch_once提价的代码块,即便你提交多次,只能执行一次.    void dispatch_once(dispatch_once_ ...