Today, I did not have a lot of work. I saw Lucene while I was visiting the garden. net, so I wrote a demo by referring to others' blog posts. (although many of them are references, it is easier to remember them by yourself !). Not to mention nonsense. First, I used the previous figure and several jquery plug-ins I saw earlier (jquery keyword high brightness, jquery paging). I don't remember the address, no author's website is left.
Step 1: Download the DLL file. Here I use version 2.9. It seems that it will not be updated. Step 2: reference the DLL to your project, and then generate the index file.
1 // <summary> 2 // execute the query statement, return sqldatareader 3 /// </Summary> 4 /// <Param name = "strsql"> query statement </param> 5 /// <returns> sqldatareader </returns> 6 public static sqldatareader executereader (string strsql) 7 {8 sqlconnection connection = new sqlconnection (connectionstring); 9 sqlcommand cmd = new sqlcommand (strsql, connection); 10 try11 {12 connection. open (); 13 sqldatareader myreader = cmd. executereader (); 14 return myreader; 15} 16 catch (system. data. sqlclient. sqlexception e) 17 {18 throw new exception (E. message); 19} 20 21}
1 // index the fields in the database 2 public static indexwriter createindex (sqldatareader myred) 3 {4 5 indexwriter writer = new indexwriter ("D:/index /", new standardanalyzer (), true); // index storage location 6 try 7 {8 while (myred. read () 9 {10 document DOC = new document (); 11 Doc. add (new field ("ID", myred ["ID"]. tostring (), field. store. yes, field. index. un_tokenized); // where ID, name, and add are all the field names in the database. This should be clear. 12 Doc. add (new field ("name", myred ["name"]. tostring (), field. store. yes, field. index. tokenized); 13 Doc. add (new field ("add", myred ["add"]. tostring (), field. store. yes, field. index. tokenized); 14 writer. adddocument (DOC); 15} 16 writer. optimize (); // optimize the Index 17 writer. close (); 18 myred. close (); 19} 20 catch (exception e) 21 {22 myred. close (); 23} 24 return writer; 25}
Now, the index file has been created.
Step 3: query the data birds we need.
1 stopwatch Sw = new stopwatch (); 2 SW. start (); // time consumed by the test program 3 string Path = "D:/index /"; // index storage directory 4 sort = new sort (New sortfield ("ID", sortfield. doc, false); // sort 5 6 indexsearcher searcher = new indexsearcher (PATH); 7 queryparser q = new queryparser ("name", new standardanalyzer ()); 8 query = Q. parse (strwhere); 9 hits = searcher. search (query, sort); 10 pcount = hits. length (); 11 if (Hits. length ()> 0) 12 {13 int num = pagesize + pages * pagesize; 14 int emdnum = pages * pagesize; // paging is used here, the method starts from the number of entries and returns the number of entries to the page. The larger the number of pages, the worse the performance. It takes 4 MB to redirect data entries to the last page, please advise 15 if (hits. length () <num) 16 {17 num = hits. length (); 18 19} 20 for (INT I = emdnum; I <num; I ++) 21 {22 document DOC = hits. DOC (I); 23 STR + = "<p> <a href = \" show. aspx? Id = "+ Doc. get ("ID") + "\" Title = \ "" + Doc. get ("name") + "\"> "+ Doc. get ("name") + "</a> </P>"; 24} 25} 26 searcher. close (); 27 SW. stop (); 28 STR + = "<p> the search result is" + hits. length () + "Time consumed:" + SW. elapsedmilliseconds. tostring () + "millisecond </P> ";
In this way, the basic query function is OK. Write a blog record to give yourself a better impression. The built-in word segmentation is not very good if you have time tomorrow.
Code writing is messy, or a download is provided. (Click to download)