热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

lucene实例2

为什么80%的码农都做不了架构师?packagecom.lucene;importjava.io.File;importjava.io.FileReader;i

为什么80%的码农都做不了架构师?>>>   hot3.png

package com.lucene;

import java.io.File;
import java.io.FileReader;
import java.util.Date;

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.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

/**
* 简单的搜索
*
* 给指定的文件夹下的文件建立索引 ,为指定的的文件创建索引 基本搜索功能
*
* @author dengyang
*
*/
public class TestIndex2 {

// static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);//内置分词器
static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

public static void createIndexByPath(File indexDir, File dataDir) {
try {
Directory dir = new SimpleFSDirectory(indexDir);
IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_35,analyzer);
iwConf.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir, iwConf);
long startTime = new Date().getTime();
File[] files = dataDir.listFiles();
for (int i = 0; i if (files[i].isFile()) {
// System.out.println(files[i].getCanonicalPath());
// System.out.println(files[i].getName());
Document document = new Document();
document.add(new Field("path", files[i].getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("filename", files[i].getName(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("contents", new FileReader(files[i])));
indexWriter.addDocument(document);
}
}
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("创建索引功耗时:" + (endTime - startTime) / 1000 + "s");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void searchIndexByString(String searchStr, String fileName,File indexDir) {
try {
// fileName:根据某个域搜索 new MultiFieldQueryParser 可以用多个域
QueryParser queryParser = new QueryParser(Version.LUCENE_35,fileName, analyzer);
Query query = queryParser.parse(searchStr);
System.out.println("query = "+query);
IndexReader reader = IndexReader.open(new SimpleFSDirectory(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
long startTime = new Date().getTime();
TopDocs topDocs = searcher.search(query, 10);//默认的搜索方法
// searcher.setDefaultFieldSortScoring(true, false);//评分
// TopDocs topDocs = searcher.search(query, null, 20, Sort.RELEVANCE);//搜索排序 , 不需要过滤
// Filter filter = new QueryWrapperFilter(new TermQuery(new Term("filename", "stylesheet")));
// TopDocs topDocs = searcher.search(query, filter, 20, Sort.RELEVANCE);//搜索排序 , 过滤
System.out.println("共有文件:" + topDocs.totalHits);
for (int i = 0; i Document document = searcher.doc(topDocs.scoreDocs[i].doc);
System.out.println("=" + i);
System.out.println("path=" + document.get("path"));
System.out.println("filename=" + document.get("filename"));
}
reader.close();
searcher.close();
long endTime = new Date().getTime();
System.out.println("搜索功耗时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @param args
*/
public static void main(String[] args) {
File indexDir = new File("D:\\luceneIndex");
File dataDir = new File("F:\\面试与就业");

createIndexByPath(indexDir, dataDir);
// searchIndexByString("面试与就业", "path", indexDir);
searchIndexByString("面试","filename",indexDir);
}
}


转:https://my.oschina.net/dyyweb/blog/42848



推荐阅读
  • 本文由编程笔记#小编为大家整理,主要介绍了Nutch相关的知识,希望对你有一定的参考价值。 ... [详细]
  • camel_使用Camel在来自不同来源的Solr中索引数据
    camelApacheSolr是建立在Lucene之上的“流行的,快速的开源企业搜索平台”。为了进行搜索(并查找结果),通常需要从不同的源(例如内容管理 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了如何使用python从列表中删除所有的零,并将结果以列表形式输出,同时提供了示例格式。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • centos 编译安装 php 5.5,CentOS 5.5上编译安装 PHP 5.3.6
    编译并安装#make&&makeinstall安装结果摘要,里面有几个主要的安装路径变量libtool:install:warning:remembertorunli ... [详细]
  • 1.0为什么要做这个博客站?  在工作学习中,经常要搜索查找各种各样的资料,每次找到相关资料后都会顺手添加到浏览器书签中,时间一长,书签也就满了。而且下次再点击这个书签时,可能就会忘记当时为什么要添加这个书签了,更有可能书签连接已经无效。这样一来,也就不方便 ... [详细]
  • Imtryingtousethisforabasicsearchwithpagination:我正在尝试使用此分区进行基本搜索:$construct?AND? ... [详细]
  • 对于我们大部分人来说linux系统的开发环境安装部署就是麻烦(确实没window方便)一下是lamp安装步骤apache+mysql+php对于一般开发足够了更新yum:yum-y ... [详细]
  • es的分布式原理?es是如何实现分布式的?
    Elasticsearch设计的理念是分布式搜索引擎,底层其实是基于lucene。核心思 ... [详细]
  • 一:什么是solrSolr是apache下的一个开源项目,使用Java基于lucene开发的全文搜索服务器;Lucene是一个开放源代 ... [详细]
  • mysql+全文检索设计,基于sphinx+mysql全文检索架构设计.doc
    基于sphinxmysql全文检索架构设计.doc还剩2页未读,继续阅读下载文档到电脑,马上远离加班熬夜!亲,喜欢就下载吧& ... [详细]
author-avatar
小妖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有