关于搜索部分
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 = 0; 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 + "秒"; } 2 执行搜索并获取结果: 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 0: QueryParser parser = new QueryParser("Name", analyzer); query = parser.Parse(tbkey.Text); break; case 1: //使用Boolean 查询 含有某个关键词或其他关键词 should 表示“或”的关系 Term T2 = new Term("Maker", TEXT); TermQuery q2 = new TermQuery(T2); BQ.Add(q2, BooleanClause.Occur.SHOULD); break; case 2: //按照片的ISO速率进行搜索 Term T3 = new Term("ISO", TEXT); TermQuery q3 = new TermQuery(T3); BQ.Add(q3, BooleanClause.Occur.SHOULD); break; case 3: //按照片的 曝光度进行搜索 Term T4 = new Term("Explord", TEXT); TermQuery q4 = new TermQuery(T4); BQ.Add(q4, BooleanClause.Occur.SHOULD); break; case 4: //按照片的 焦距进行搜索 Term T5 = new Term("FocalLength", TEXT); TermQuery q5 = new TermQuery(T5); BQ.Add(q5, BooleanClause.Occur.SHOULD); break; case 5: //按照片的光圈进行搜索 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( 0 == this.comboBox1.SelectedIndex) //使用if语句对搜索方式进行分类,如果是按照相片或图片名称进行搜索则进行关键字匹配查询 hits = searcher.Search(query); else hits = searcher.Search(BQ); //如果不是按名称搜索,则进行严格匹配查询搜索! if (hits.Length() > 0) { this.status1.Text = " 共检索 " + hits.Length().ToString() + "个对象"; } this.imageList.Images.Clear(); for (int i = 0; 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")) >>10).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