lucene .NET 搜索图片 功能实现
关于搜索部分
1想建立索引。构建jpg图片解析器,在索引时将jpg图片的exif信息及其文本信息如名称,存放路径,大小,日期等等加入索引!具体实现代码如下:
public void BulidIndex(string path)//创建索引
{
DateTime biStart = DateTime.Now;//创建索引开始
DirectoryInfo[] ChildDirectory;//子目录集
FileInfo[] files;//当前所有文件
DirectoryInfo FatherDirectory = new DirectoryInfo(path); //当前目录
ChildDirectory = FatherDirectory.GetDirectories("*.*"); //得到子目录集
files = FatherDirectory.GetFiles("*.jpg");//得到jpg文件集,可以进行操作
Analyzer analyzer = new MyAnalyzer();//声明一个分词器,
IndexWriter indexWriter = new IndexWriter("index", analyzer, true);/*建立一个IndexWriter的实例,这个类是负责创建索引的,有很多构造函数,这里使用的是其中的一个。三个参数分别是:索引建立到哪个目录,用什么分词器,还有就是是否创建。如果是否创建为false,那么就是以增量的方式来创建。*/
for (int i = ; i < files.Length; i++)
{
string maker = "unkown", explord = "unkown",
iso = "unkown", aperture = "unkown", focalLength="unkown";
Document doc = new Document();//声明一个document并将图片的名称,存放路径,大小,日期,
相机制造商,曝光度,ISO,焦距,光圈值依次通过field 添加入document中。
FindExifinfo(files[i].FullName, ref maker, ref explord, ref iso, ref focalLength, ref aperture);
doc.Add(new Field("Name", files[i].Name, Field.Store.YES, Field.Index.TOKENIZED));
doc.Add(new Field("FullName", files[i].FullName, Field.Store.YES, Field.Index.NO));
doc.Add(new Field("Length", files[i].Length.ToString(), Field.Store.YES, Field.Index.NO));
doc.Add(new Field("LastWriteTime", files[i].LastWriteTime.ToString(), Field.Store.YES, Field.Index.NO));
doc.Add(new Field("CreationTime", files[i].CreationTime.ToString(), Field.Store.YES, Field.Index.NO));
doc.Add(new Field("Maker", maker, Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("Explord", explord, Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("ISO", iso, Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("FocalLength", focalLength, Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(new Field("Aperture", aperture, Field.Store.YES, Field.Index.UN_TOKENIZED));
indexWriter.AddDocument(doc); /*调用了AddDocument方法,在AddDocument方法中,先组织一个Docuement对象,然后把这个对象交给IndexWriter*/
}
indexWriter.Optimize();//Optimize优化索引
indexWriter.Close();//最后关闭创建过程
DateTime biStop = DateTime.Now;//创建索引结束
this.status1.Text = "创建索引用时:" + (biStop - biStart).TotalSeconds + "秒";
}
执行搜索并获取结果:
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
Hits hits = null;
Query query = null;
Analyzer analyzer = new MyAnalyzer();
DateTime Start = DateTime.Now;//索引开始时间
string TEXT= this.tbkey.Text;
BooleanQuery BQ = new BooleanQuery( ); //使用Boolean 查询
if (TEXT == "")
return;
try
{
switch (this.comboBox1.SelectedIndex)
{
case :
QueryParser parser = new QueryParser("Name", analyzer);
query = parser.Parse(tbkey.Text);
break;
case : //使用Boolean 查询 含有某个关键词或其他关键词 should 表示“或”的关系
Term T2 = new Term("Maker", TEXT);
TermQuery q2 = new TermQuery(T2);
BQ.Add(q2, BooleanClause.Occur.SHOULD);
break;
case : //按照片的ISO速率进行搜索
Term T3 = new Term("ISO", TEXT);
TermQuery q3 = new TermQuery(T3);
BQ.Add(q3, BooleanClause.Occur.SHOULD);
break;
case : //按照片的 曝光度进行搜索
Term T4 = new Term("Explord", TEXT);
TermQuery q4 = new TermQuery(T4);
BQ.Add(q4, BooleanClause.Occur.SHOULD);
break;
case : //按照片的 焦距进行搜索
Term T5 = new Term("FocalLength", TEXT);
TermQuery q5 = new TermQuery(T5);
BQ.Add(q5, BooleanClause.Occur.SHOULD);
break;
case : //按照片的光圈进行搜索
Term T6 = new Term("Aperture", TEXT);
TermQuery q6 = new TermQuery(T6);
BQ.Add(q6, BooleanClause.Occur.SHOULD);
break;
default: break;
}
}
catch (Exception)
{
throw;
}
IndexSearcher searcher = new IndexSearcher("index");
if (searcher != null)
{
if( == this.comboBox1.SelectedIndex) //使用if语句对搜索方式进行分类,如果是按照相片或图片名称进行搜索则进行关键字匹配查询
hits = searcher.Search(query);
else
hits = searcher.Search(BQ); //如果不是按名称搜索,则进行严格匹配查询搜索!
if (hits.Length() > )
{
this.status1.Text = " 共检索 " + hits.Length().ToString() + "个对象";
}
this.imageList.Images.Clear();
for (int i = ; i < hits.Length(); i++)
{ //将搜索结果分别添加入listview和imagelist中,此过程比较耗时间!无奈!!!
Document doc = hits.Doc(i);
int itemNumber = this.listView1.Items.Count;
//string fullname = doc.Get("FullName");
string[] subItem = { doc.Get("Name"), doc.Get("FullName"), (Convert.ToInt32(doc.Get("Length")) >>).ToString() + "KB", doc.Get("LastWriteTime") }; //使用右移 加快程序的执行速度!
this.imageList.Images.Add(doc.Get("FullName"), Bitmap.FromFile(doc.Get("FullName")));
this.listView1.Items.Add(new ListViewItem(subItem, doc.Get("FullName")));//显示结果较慢的元凶!
}
}
else
{
this.status1.Text = " 共检索 0 个对象";
}
DateTime Stop = DateTime.Now;//索引完成时间
this.status1.Text += " 搜索用时:" + (Stop - Start).TotalSeconds + "秒";
http://blog.csdn.net/yang073402/article/details/5470153
lucene .NET 搜索图片 功能实现的更多相关文章
- lucene全文搜索之一:lucene的主要功能和基本结构(基于lucene5.5.3)
前言:lucene并不是像solr或elastic那样提供现成的.直接部署可用的系统,而是一套jar包,提供了一些常见语言分词.构建索引和创建搜索器等等功能的API,我们常用到的也就是分词器.索引目录 ...
- Lucene.net常用功能说明
Lucene.net是一个.net下的全文检索类库.配置简单,功能丰富,比较成熟.我在项目中用Lucene.net有一段时间了,这里我把常用一些功能写出来,与大家一起分享. Lucene.net用的是 ...
- Lucene.net常见功能实现知识汇总
在开发SearchEasy Site SearchEngine(搜易站内搜索引擎)的时候,经常会遇到一些搜索引擎的常见功能如何实现的问题,比如实现相关度百分比显示?如何实现在结果中搜索等等诸如此类常见 ...
- 如何使用 Lucene 做网站高亮搜索功能?
现在基本上所有网站都支持搜索功能,现在搜索的工具有很多,比如Solr.Elasticsearch,它们都是基于 Lucene 实现的,各有各的使用场景.Lucene 比较灵活,中小型项目中使用的比较多 ...
- 【Lucene】Apache Lucene全文检索引擎架构之搜索功能3
上一节主要总结了一下Lucene是如何构建索引的,这一节简单总结一下Lucene中的搜索功能.主要分为几个部分,对特定项的搜索:查询表达式QueryParser的使用:指定数字范围内搜索:指定字符串开 ...
- tantivy&lucene功能,写入性能对比
硬件概述:cpu:24,内存:20g,磁盘:10*2.7T. 写入性能:(不对ip进行添加geo信息). 写入性能对比 速度 Commit耗时(秒) 500*1000条 Bulk耗时(秒) 1000条 ...
- Lucene 4.x Spellcheck使用说明
Spellcheck是Lucene新版本的功能,在介绍spellcheck之前,我们需要弄清楚Spellcheck支持几种数据源.Spellcheck构造函数需要传入Dictionary接口: pac ...
- lucene字典实现原理
http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到该te ...
- lucene字典实现原理——FST
转自:http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到 ...
随机推荐
- SQL基本语句(3) LOAD DATA INFILE
使用LOAD语句批量录入数据 语法: LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNOR ...
- ASPXGridView用法
一.ASPXGridView外观显示 属性: Caption----列的标题( KeyFieldName----数据库字段 SEOFriendly 是否启用搜索引擎优化 Summary 指定分页汇总信 ...
- linux 内存使用
# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 1.9G 45G 5% / tmpfs 1.9G 0 1.9G 0% ...
- IIS7下,flush无效,解决方案
打开文件 C:\Windows\System32\inetsrv\config\applicationHost.config ,注意如果你的是64位系统,这个文件就必须用64位的编辑软件打开,32位的 ...
- ASP.NET Session丢失的情况
正常操作情况下会有ASP.NET Session丢失的情况出现.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成60分钟,不会这么快就超时的. 现在我就把原 ...
- openstack简介
OpenStack是一个开源的云计算管理平台项目,由几个主要的组件组合起来完成具体工作.OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管理平台 ...
- javascritp日期函数总结
getDate()与getDay()的区别: obj.getDate()返回一个月中的某一天:obj.getDay()返回一个星期中的某一天. getYear()与getFullYear()的区别: ...
- N层电梯只停一层情况下,求所有人爬楼层数最少
一.题目: 石家庄铁道大学基础教学楼一共有四部电梯,每层都有人上下,电梯在每一层都停.信1201-1班张一东每层都停有点儿不耐烦.如果在上下课高峰时刻电梯从一层上行,但只允许停留在某一层.每个人选择自 ...
- DBLINK 创建与小结
1.DBLINK 的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数 ...
- 关于js一般对象与标配对象
当一个js函数对象被创建时,Function 构造器产生的函数对象会运行类似这样的一些代码 this.prototype={constructor:this} 新函数被赋予了一个prototype属性 ...