# Create and match region with the new dictionary ////// Create and match the new dictionary ///Private void CreateIndexData () {// index library folder FSDirectory dir = FSDirectory. open (new DirectoryInfo (path), new NativeFSLockFactory (); // check whether the index library bool has = IndexReader exists. indexExists (dir); if (has) {// unlock the locked index library if (IndexWriter. isLocked (dir) {IndexWriter. unlock (dir) ;}// index database write object IndexWriter iw = new IndexWriter (dir, new PanGuAnalyzer (),! Has, IndexWriter. MaxFieldLength. UNLIMITED); List
List = javasebll. instance. get (); foreach (var o in list) {Document d = new Document (); // Field. store. YES: stores the Field value (the Field value before word segmentation) // Field. store. NO: NO storage. The storage has nothing to do with the index. // Field. store. COMPRESS: Compressed Storage, used for long text or binary files, but with impaired performance // Field. index ANALYZED; create an Index using Word Segmentation // Field. index ANALYZED_NO_NORMS; Word Segmentation compression Index creation // Field. index NO; // Field. index NOT_ANALYZED; create an Index without word segmentation // Field. index NOT_ANALYZED_NO_NORMS; no word segmentation compression Index creation // Field. termVector NO; // Field. termVector WITH_OFFSETS; storage offset // Field. termVector WITH_POSITIONS; storage location // Field. termVector WITH_POSITIONS_OFFSETS; storage location and offset // Field. termVector YES; the TermVector d that stores this field for each Document. add (new Field ("Id", o. id. toString (), Field. store. YES, Field. index. NOT_ANALYZED); d. add (new Field ("Title", o. title, Field. store. YES, Field. index. ANALYZED, Field. termVector. WITH_POSITIONS_OFFSETS); d. add (new Field ("Message", o. message, Field. store. YES, Field. index. ANALYZED, Field. termVector. WITH_POSITIONS_OFFSETS); iw. addDocument (d);} iw. optimize (); // release the file iw. close (); dir. close () ;}# endregion
# Region Query ////// Query //////Keyword to be queried///Page index (starting from 1)///Number of data entries per page///
Private List
Search (string str, int index = 1, int count = 3) {List
List = null; // The index library folder FSDirectory dir = FSDirectory. open (new DirectoryInfo (path), new NativeFSLockFactory (); // check whether the index library bool has = IndexReader exists. indexExists (dir); // if (! Has) {// create the index database CreateIndexData () ;}// index database read object IndexReader ir = IndexReader. open (dir, true); // index database query object IndexSearcher searcher = new IndexSearcher (ir); // Search Condition PhraseQuery query = new PhraseQuery (); // word segmentation {Segment segment = new Segment (); ICollection
Words = segment. doSegment (str); foreach (var o in words) {query. add (new Term ("Message", o. toString ();} // specify the maximum distance between keywords and query. setSlop (100); // the container that holds the query result. TopScoreDocCollector collector = TopScoreDocCollector. create (index * count, false); // query searcher Based on query conditions. search (query, null, collector); ScoreDoc [] docs = collector. topDocs (count * (index-1), count * (index-1) + count ). scoreDocs; if (docs. length> 0) {list = new List
(); Foreach (var o in docs) {Document document = searcher.Doc(o.doc); incluemodel m = new incluemodel (); m. id = Convert. toInt32 (document. get ("Id"); m. title = document. get ("Title"); m. message = document. get ("Message"); list. add (m) ;}// release the file ir. close (); searcher. close (); dir. close (); return list;} # endregion