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. codeforces 664A Complicated GCD

    水题..[a,b]区间数的最大公约数. a==b输出a 否则输出1 #include<cstdio> #include<cstring> #include<iostrea ...

  2. HW2.25

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

  3. 15个实用的Linux find命令示例

    妈咪,我找到了! -- 15个实用的Linux find命令示例 http://www.oschina.net/translate/15-practical-linux-find-command-ex ...

  4. 解决android锁屏或解锁后activity重启的问题

    If your target build version is Honeycomb 3.2 (API Level 13) or higher you must put the screenSize f ...

  5. JAVA的反射机制原理

    http://www.cnblogs.com/hongxinlaoking/p/4684652.html 一  反射机制的概念: 指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于 ...

  6. java中用线程解决进出水问题

    //进水 class Inflow implements Runnable{ //水对象 Water wat; public Inflow(Water w){ this.wat = w; } @Ove ...

  7. ConversionException: No value specified for 'Date'的解决版本

    DateConverter converter = new DateConverter(defaultValue); ConvertUtils.register(converter, java.uti ...

  8. MySQL check the manual that corresponds to your MySQL server version for the right syntax错误

    地化GO的时候一直遇到一个错误就是check the manual that corresponds to your MySQL server version for the right syntax ...

  9. 【28】避免返回handles指向对象内部成分

    1.为什么? 很简单,你指向箱子里面的一个物品,使用这个物品.但是箱子不受你控制,箱子销毁了,里面的物品也会随之销毁.那么这种情况下,你指向的就是一堆垃圾,你还在使用这个物品,导致未定义的行为.

  10. Spring mvc Data Redis—Pub/Sub(附Web项目源码)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...