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. powershell学习

    PowerShell 调试器 在开始运行处,输入powershell ISE回车即可 PowerShell 与操作系统版本 powershell在windows server 2008上自带,但最好在 ...

  2. 用生活例子来解释Java synchronized块

    今天满世界的微信小程序的新闻,大家都说对于Android原生程序有构成危险了,我也不想了,以后的事谁知道呢, 我还是好好执行一下今年的计划吧.  项目刚刚上线,最近没啥事,我一直感觉自己的Java基础 ...

  3. 破解密码那些事儿(Hacking Secret Ciphers with Python)

    作者:Al Sweigart   我们在电视和电影里头经常能够看到黑客们兴奋的快速敲击键盘,接着毫无意义的数字就在屏幕上飞奔(比如黑客帝国).然后让大家产生了一种奇妙的错觉,做黑客是一件高大上的事情, ...

  4. ubuntu下arm-linux-gcc安装

    我下载的地址随便找的,4.4.3版本的,地址:http://www.cr173.com/soft/42654.html#address 1.我放在了/work/tools/ 2.sudo tar  x ...

  5. POJ2250 - Compromise(LCS+打印路径)

    题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...

  6. (太强大了) - Linux 性能监控、测试、优化工具

    转: http://www.vpsee.com/2014/09/linux-performance-tools/ Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经 ...

  7. WPF布局系统[转]

    转自:http://www.cnblogs.com/niyw/archive/2010/10/31/1863908.html前言 前段时间忙了一阵子Google Earth,这周又忙了一阵子架构师论文 ...

  8. hdu 3724 Encoded Barcodes

    Trie模板.把所有单词都用字典树存起来,然后给每个节点赋权值表示前缀到该节点出现了几次.然后直接查询就可以了. #include <algorithm> #include <ios ...

  9. Spring源码入门——XmlBeanDefinitionReader解析

    接上篇[] ,我们看到BeanDefinitionReader解决的是从资源文件(xml,propert)到BeanDefinition集合的过程.所以BeanDefinitionReader接口有两 ...

  10. 【转】ST05

    一. SQL Trace 通过SQL跟踪,可以具体查询数据来源于哪些数据库表, 例如:可以查询某个交易(或几个交易)所涉及的数据库表. 为了减少在最终查询结果的工作量,要在屏幕显示你所要显示的数据的前 ...