first publication; long time reader. I apologize for the head if this has already been asked here (I am also new to lucene!). I did a lot of research and could not find a good explanation / example for my question.
First of all, I used IKVM.NET to convert lucene 4.9 java to be included in my .net application. I decided to do this so that I can use the latest version of lucene. No questions.
I am trying to create a basic example to start learning lucene and applying it to my application. I have done countless google searches and read many articles, apache website, etc. My code follows mainly here: http://www.lucenetutorial.com/lucene-in-5-minutes.html p>
My question is: I do not believe that I want to use RAMDirectory. right? Since I will index the database and allow users to search it through the website. I decided to use FSDirectory because I did not think that it should be stored in memory.
When IndexWriter is created, it creates new files every time (.cfe, .cfs, .si, segment.gen, write.lock, etc.) It seems to me that you will create these files once, and then use them while the index will not be rebuilt?
So how do I create an IndexWriter without recreating the index files?
The code:
StandardAnalyzer analyzer; Directory directory; protected void Page_Load(object sender, EventArgs e) { var version = org.apache.lucene.util.Version.LUCENE_CURRENT; analyzer = new StandardAnalyzer(version); if(directory == null){ directory= FSDirectory.open(new java.io.File(HttpContext.Current.Request.PhysicalApplicationPath + "/indexes")); } IndexWriterConfig config = new IndexWriterConfig(version, analyzer); //i found setting the open mode will overwrite the files but still creates new each time config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter w = new IndexWriter(directory, config); addDoc(w, "test", "1234"); addDoc(w, "test1", "1234"); addDoc(w, "test2", "1234"); addDoc(w, "test3", "1234"); w.close(); } private static void addDoc(IndexWriter w, String _keyword, String _keywordid) { Document doc = new Document(); doc.add(new TextField("Keyword", _keyword, Field.Store.YES)); doc.add(new StringField("KeywordID", _keywordid, Field.Store.YES)); w.addDocument(doc); } protected void searchButton_Click(object sender, EventArgs e) { String querystr = ""; String results=""; querystr = searchTextBox.Text.ToString(); Query q = new QueryParser(org.apache.lucene.util.Version.LUCENE_4_0, "Keyword", analyzer).parse(querystr); int hitsPerPage = 100; DirectoryReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; if (hits.Length == 0) { label.Text = "Nothing was found."; } else { for (int i = 0; i < hits.Length; ++i) { int docID = hits[i].doc; Document d = searcher.doc(docID); results += "<br />" + (i + 1) + ". " + d.get("KeywordID") + "\t" + d.get("Keyword") + " Hit Score: " + hits[i].score.ToString() + "<br />"; } label.Text = results; reader.close(); } }