Index document file
- public ActionResult IndexDocuments()
- {
- // index path Ex: c:/index
- string strIndexDir = ConfigurationManager.AppSettings["Lucene_Index_Search_Path"];
- // Lucene.net index Files directory path set Inside project Folder
- // eg. d:/document files folder
- string strdirectorypath = Server.MapPath(ConfigurationManager.AppSettings["Lucene_Index_DirectoryFile_Path"]);
- try
- {
- Lucene.Net.Store.Directory indexDir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir));
- Analyzer std = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
- System.DateTime start = System.DateTime.Now;
- IndexWriter idxw = new IndexWriter(indexDir, std, true, IndexWriter.MaxFieldLength.UNLIMITED);
- string strpath = strdirectorypath;
- string[] filePaths = System.IO.Directory.GetFiles(strpath, "*", SearchOption.AllDirectories);
- foreach(var path in filePaths)
- {
- using(PdfReader reader = new PdfReader(path))
- {
- StringBuilder text = new StringBuilder();
- for (int i = 1; i <= reader.NumberOfPages; i++)
- {
- Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
- text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
- // input = input.Substring(input.IndexOf("/"));
- doc.Add(new Field("path", path, Field.Store.YES,
- Field.Index.ANALYZED));
- doc.Add(new Field("content", text.ToString(), Field.Store.YES,
- Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
- idxw.AddDocument(doc);
- idxw.Optimize();
- }
- }
- } // writing new document to the index
- idxw.Close();
- System.DateTime end = System.DateTime.Now;
- //Response.Output.Write(end.Ticks - start.Ticks + " total milliseconds------Index sucess");
- ViewData["message"] = end.Ticks - start.Ticks + "----Total Milliseconds";
- ViewData["m"] = "success";
- }
- catch
- {
- Response.Output.Write("Not Indexing");
- }
- return View();
- }
- public JsonResult searchjsondocument(string searchtext)
- {
- string result = "";
- EmployeeModel model = new EmployeeModel();
- model.List = new List < SearchResults > ();
- ViewBag.UserList = model.List;
- // view Textbox Pass Value
- string strName = Convert.ToString(searchtext);
- TempData["result"] = "";
- if (searchtext != null || searchtext != "")
- {
- //<!--Lucene_Index_Search_Path-->
- string strIndexDir = ConfigurationManager.AppSettings["Lucene_Index_Search_Path"];
- Lucene.Net.Store.Directory indexDir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(strIndexDir));
- Lucene.Net.Index.IndexReader reader = IndexReader.Open(indexDir, true);
- IndexSearcher searcher = new IndexSearcher(reader);
- Analyzer std = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
- QueryParser parser = new QueryParser(Version.LUCENE_29, "content", std);
- Query query = parser.Parse(strName);
- TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
- searcher.Search(query, collector);
- ScoreDoc[] hits = collector.TopDocs().scoreDocs;
- for (int i = 0; i < hits.Length; i++)
- {
- Document hitDoc = searcher.Doc(hits[i].doc); // getting actual document
- Response.Output.Write("path: " + hitDoc.Get("path") + "<br>" + "Content: " + hitDoc.Get("content") + "<br>");
- result = result + hitDoc.Get("path") + "#";
- searcher.Close();
- }
- if (result.Length > 1)
- {
- TempData["result"] = result;
- }
- else
- {
- TempData["result"] = "";
- }
- }
- return Json(result, JsonRequestBehavior.AllowGet); //return "Search" view to the user
- }
