Monday, 15 February 2010

c# - Search Lucene Index -



c# - Search Lucene Index -

i using lucene.net version 2.3.2.1. when phone call index() method, index document in c:\index. when phone call searchlucene() method, nil coming out.

here index() , searchlucene() methods :

public void index(string strhash, string filepath, string encryptedpath, string password, string filename) { string indexfilelocation = @"c:\index"; lucene.net.store.directory dir = lucene.net.store.fsdirectory.getdirectory(indexfilelocation, false); //create analyzer process text lucene.net.analysis.analyzer analyzer = new lucene.net.analysis.standard.standardanalyzer(); //create index author directory , analyzer defined. lucene.net.index.indexwriter indexwriter = new lucene.net.index.indexwriter(dir, analyzer);/*true create new index*/ //create document, add together in single field lucene.net.documents.document doc = new lucene.net.documents.document(); doc.add(new field("keywordhash", strhash, field.store.yes, field.index.tokenized)); doc.add(new field("keywordpath", filepath, field.store.yes, field.index.no)); doc.add(new field("keywordencpath", encryptedpath, field.store.yes, field.index.tokenized)); doc.add(new field("keywordpassword", password, field.store.yes, field.index.tokenized)); //doc.add(new field("keywordencryptedfile", encryptedfile, field.store.yes, field.index.analyzed)); doc.add(new field("keywordfilename", filename, field.store.yes, field.index.no)); //write document index indexwriter.adddocument(doc); //optimize , close author indexwriter.optimize(); indexwriter.close(); } public void lucenesearch() { hashalg hashalg = new hashalg(); string keywordlower = tbsearchenc.text.tolower(); string keywordhash; if(rbmd5search.checked == true) { keywordhash = hashalg.generatehashmd5(keywordlower); } else { keywordhash = hashalg.generatehashsha1(keywordlower); } string indexfilelocation = @"c:\index"; //lucene.net.store.directory dir = lucene.net.store.fsdirectory.getdirectory(indexfilelocation, false); //lucene.net.analysis.analyzer analyzer = new lucene.net.analysis.standard.standardanalyzer(); lucene.net.search.indexsearcher searcher = new lucene.net.search.indexsearcher(indexfilelocation); //create index searcher perform search //lucene.net.search.indexsearcher searcher = new lucene.net.search.indexsearcher(dir); //build query object //query query = new termquery(new term("keywordhash", keywordhash)); lucene.net.index.term searchterm = new lucene.net.index.term("keywordhash", keywordhash); lucene.net.search.query query = new lucene.net.search.termquery(searchterm); //execute query lucene.net.search.hits hits = searcher.search(query); int result = hits.length(); //iterate on results. (int = 0; < result; i++) { document doc = hits.doc(i); string hashvalue = doc.get("keywordhash"); console.writeline(hashvalue); string path = doc.get("keywordpath"); console.writeline(path); string encpath = doc.get("keywordencpath"); console.writeline(encpath); string filename = doc.get("keywordfilename"); console.writeline(filename); listboxsearch.items.add(encpath); console.writeline(hashvalue + " " + path + " " + encpath + " " + filename); } }

i think need phone call indexwriter.commit(), , shouldn't bother optimize() method unless have special requirement.

c# search lucene.net

No comments:

Post a Comment