public ActionResult Create(Buyers buyers) { try { if (ModelState.IsValid) { buyers.ID = service.AddBuyer(buyers.Description, buyers.FirstName, buyers.LastName); if (buyers.ID != 0) { searching.AddUpdateLuceneIndex(buyers); } return RedirectToAction("Index"); } } catch (DataException) { ModelState.AddModelError("", "Something wrong!"); } return View(buyers); }
// add/update/clear search index data public void AddUpdateLuceneIndex(Buyers data) { AddUpdateLuceneIndex(new List<Buyers> { data }); }
public void _addToLuceneIndex(Buyers data, IndexWriter writer) { // remove older index entry var searchQuery = new TermQuery(new Term("Id", data.ID.ToString())); writer.DeleteDocuments(searchQuery); // add new index entry var doc = new Document(); // add lucene fields mapped to db fields doc.Add(new Field("Id", data.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("FirstName", Validate(data.FirstName), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("LastName", Validate(data.LastName), Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("Description", Validate(data.Description), Field.Store.YES, Field.Index.ANALYZED)); // add entry to index writer.AddDocument(doc); }