public void createIndex(LibDocument rec)
        {
            using (Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48))
            {
                IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_48, analyzer);
                using (var writer = new IndexWriter(Directory, config))
                {
                    var document = new Document();

                    document.Add(new Field("Id", rec.id.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Name", rec.Name.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Path", rec.Path.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Desc2", rec.Desc2.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Desc1", rec.Desc1.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("FullText",
                                           string.Format("{0} {1} {2} {3}", rec.Name, rec.Path, rec.Desc2, rec.Desc1)
                                           , Field.Store.YES, Field.Index.ANALYZED
                                           ));

                    writer.AddDocument(document);
                    // }

                    //writer.Optimize();
                    writer.Commit();
                }
            }
        }
        public void DeleteFromIndex(LibDocument doc)
        {
            var directory = FSDirectory.Open(
                AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\LuceneIndexes"
                );
            IndexReader indexReader = DirectoryReader.Open(directory);

            using (Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48))
            {
                IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_48, analyzer);
                using (var writer = new IndexWriter(directory, config))
                {
                    var term = new Term("Id", doc.id.ToString());
                    writer.DeleteDocuments(term);
                    writer.DeleteUnusedFiles();
                    writer.ForceMergeDeletes();
                    writer.Commit();
                }
            }
        }
Beispiel #3
0
        public void createIndex(LibDocument rec)
        {
            // FSDirectory directory = FSDirectory.Open(
            //                  AppDomain.CurrentDomain.BaseDirectory + @"\App_Data\LuceneIndexes"
            //                   //AppDomain.CurrentDomain.BaseDirectory + @"/App_Data/LuceneIndexes" //For Linux
            //               );

            //var recs=db.Documents.ToList();


            using (Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_48))
            {
                //IndexWriter writer = new IndexWriter(index, config);
                IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_48, analyzer);
                using (var writer = new IndexWriter(Directory, config))
                {
                    // the writer and analyzer will popuplate the directory with documents

                    // foreach (var rec in recs)
                    // {
                    var document = new Document();
                    // if(rec.Desc2==null)
                    // rec.Desc2="";

                    document.Add(new Field("Id", rec.id.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Name", rec.Name.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Path", rec.Path.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Desc2", rec.Desc2.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    document.Add(new Field("Desc1", rec.Desc1.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
////

                    //document.Add(new StringField("Id",rec.id.ToString(),Field.Store.YES,));
                    // document.Add(new StringField("Name",rec.Name.ToString(),Field.Store.YES));
                    // document.Add(new StringField("Path", rec.Path.ToString(), Field.Store.YES));
                    // document.Add(new StringField("Desc2", rec.Desc2.ToString(), Field.Store.YES));
                    //  document.Add(new StringField("FullText",
                    //     string.Format("{0} {1} {2}",rec.Name, rec.Path, rec.Desc2)
                    //     , Field.Store.YES));
////

                    //document.Add(new StringField("Desc2", rec.Desc2.ToString(), StringField.Store.YES, Field.Index.ANALYZED));
                    //document.Add(new Field("BirthDate", row["BirthDate"].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                    //document.Add(new Field("ID", row["ID"].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                    //var date = row["BirthDate"].ToString();

                    //date = string.Format("{0} {1}", date, date.Replace("/", " / "));

                    document.Add(new Field("FullText",
                                           string.Format("{0} {1} {2} {3}", rec.Name, rec.Path, rec.Desc2, rec.Desc1)
                                           , Field.Store.YES, Field.Index.ANALYZED
                                           ));

                    writer.AddDocument(document);
                    // }

                    //writer.Optimize();
                    writer.Commit();
                }
            }
        }