Ejemplo n.º 1
0
        public static void AddUpdateLuceneIndex(IEnumerable <LucenePageModel> PageData)
        {
            // init lucene
            var analyzer = new StandardAnalyzer(_version);

            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // add data to lucene search index (replaces older entry if any)
                foreach (LucenePageModel data in PageData)
                {
                    _addToLuceneIndex(data, writer);
                }

                // close handles
                analyzer.Close();
                writer.Optimize();
                writer.Commit();
                writer.Dispose();
            }
        }
Ejemplo n.º 2
0
        public ActionResult Cut(string str)
        {
            //一元分词-简单分词
            StringBuilder sb = new StringBuilder();

            StandardAnalyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            TokenStream tokenStream = analyzer.TokenStream("", new StringReader(str));

            ITermAttribute item = tokenStream.GetAttribute <ITermAttribute>();

            while (tokenStream.IncrementToken())

            {
                sb.Append(item.Term + "|");
            }
            tokenStream.CloneAttributes();
            analyzer.Close();
            return(Content(sb.ToString()));
        }
Ejemplo n.º 3
0
 public static bool ClearLuceneIndex()
 {
     try
     {
         var analyzer = new StandardAnalyzer(Version.LUCENE_30);
         using (var writer = new IndexWriter(_directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
         {
             // remove older index entries
             writer.DeleteAll();
             // close handles
             analyzer.Close();
             writer.Dispose();
         }
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 4
0
 public static bool Clear()
 {
     try
     {
         var _analyzer = new StandardAnalyzer(CurrentVersion);
         using (var _writer = new IndexWriter(Directory, _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
         {
             // remove older index entries
             _writer.DeleteAll();
             // close handles
             _analyzer.Close();
             _writer.Dispose();
         }
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 5
0
        public static IEnumerable <HashTag> Search(string searchQuery,
                                                   string searchField, int hitsLimit = 1000)
        {
            if (String.IsNullOrEmpty(searchQuery) || String.IsNullOrEmpty(searchField))
            {
                return(new List <HashTag>());
            }
            IEnumerable <HashTag> results = null;

            using (var searcher = new IndexSearcher(HashtagDirectory, false))
            {
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);
                var parser   = new QueryParser(Version.LUCENE_30, searchField, analyzer);
                var query    = ParseQuery(searchQuery, parser);
                var hits     = searcher.Search(query, hitsLimit).ScoreDocs;
                results = MapLuceneToDataList(hits, searcher);
                analyzer.Close();
            }
            return(results);
        }
Ejemplo n.º 6
0
        public bool ClearLuceneIndex()
        {
            try
            {
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);

                using (var writer = new IndexWriter(Directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    writer.DeleteAll();
                    analyzer.Close();
                    writer.Dispose();
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Performs the search.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="hitLimit">The hit limit.</param>
        /// <param name="searchQuery">The search query.</param>
        /// <returns></returns>
        private SearchResult <TSearchData> PerformSearch(Version luceneVersion, FSDirectory fsDirectory, int hitLimit, string searchQuery)
        {
            var result = new SearchResult <TSearchData>();

            string[] fields = null;

            if (string.IsNullOrEmpty(searchQuery.Replace("*", string.Empty).Replace("?", string.Empty)))
            {
                return(result);
            }

            fields = searchQuery.Contains(':') ? this.allSearchFields.ToArray() : this.defaultSearchFields.ToArray();

            using (var indexSearcher = new IndexSearcher(fsDirectory, false))
            {
                var analyzer = new StandardAnalyzer(luceneVersion);

                var queryParser = new MultiFieldQueryParser(luceneVersion, fields, analyzer)
                {
                    DefaultOperator = QueryParser.Operator.AND
                };

                var query = this.searchQueryParser.ParseQuery(searchQuery, queryParser);

                var topFieldCollector = TopFieldCollector.Create(new Sort(), hitLimit, false, true, true, true);

                indexSearcher.Search(query, topFieldCollector);

                var topDocs = topFieldCollector.TopDocs();

                result.TotalHitCount = topDocs.TotalHits;

                var hits = topDocs.ScoreDocs;

                result.Results = this.MapDocumentsToSearchData(hits, indexSearcher);

                analyzer.Close();

                return(result);
            }
        }
Ejemplo n.º 8
0
        private static IEnumerable <SampleData> _search
            (string searchQuery, string searchField = "")
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            {
                return(new List <SampleData>());
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false))
            {
                var hits_limit = 1000;
                var analyzer   = new StandardAnalyzer(Version.LUCENE_30);

                // search by single field
                //if (!string.IsNullOrEmpty(searchField))
                //{
                //    var parser = new QueryParser(Version.LUCENE_30, searchField, analyzer);
                //    var query = parseQuery(searchQuery, parser);
                //    var hits = searcher.Search(query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
                //    var results = _mapLuceneToDataList(hits, searcher);
                //    analyzer.Close();
                //    searcher.Dispose();
                //    return results;
                //}
                // search by multiple fields(ordered by RELEVANCE)
                //else
                //{
                var parser = new MultiFieldQueryParser
                                 (Version.LUCENE_30, new[] { "FileName", "Title", "Content" }, analyzer);
                var query = parseQuery(searchQuery, parser);
                var hits  = searcher.Search
                                (query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
                var results = _mapLuceneToDataList(hits, searcher);
                analyzer.Close();
                searcher.Dispose();
                return(results);
                //}
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Adds or updates the search index
        /// </summary>
        /// <param name="messageList">The message list.</param>
        public void AddUpdateSearchIndex(IEnumerable <SearchMessage> messageList)
        {
            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            var indexWriter = new IndexWriter(
                Directory,
                analyzer,
                !IndexReader.IndexExists(Directory),
                new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));

            indexWriter.SetMergeScheduler(new ConcurrentMergeScheduler());
            indexWriter.SetMaxBufferedDocs(YafContext.Current.Get <YafBoardSettings>().ReturnSearchMax);

            foreach (var message in messageList)
            {
                AddToSearchIndex(message, indexWriter);
            }

            analyzer.Close();
            indexWriter.Dispose();
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Clears the index record with the given id.
        /// </summary>
        /// <remarks>Call this method whenever a record is deleted from the repository.</remarks>
        /// <param name="recordId">Record id.</param>
        public void ClearLuceneIndexRecord(Int32 recordId)
        {
            // Initialize Lucene
            var analyzer = new StandardAnalyzer(Version.LUCENE_30);

            using (var writer = new IndexWriter(Directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // remove older index entry
                var searchQuery = new TermQuery(new Term(nameof(SampleData.Id), recordId.ToString()));
                try
                {
                    writer.DeleteDocuments(searchQuery);
                }
                finally
                {
                    // close handles
                    writer.Dispose();
                    analyzer.Close();
                }
            }
        }
        public void RebuildRamIndex(IEnumerable <SampleDataFileRow> dataToIndex)
        {
            var analyzer = new StandardAnalyzer(Version.LUCENE_30, ListStopWords);

            using (var writer = new IndexWriter(_RAMdirectory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                writer.DeleteAll();
                writer.Commit();
                foreach (var sampleDataFileRow in dataToIndex)
                {
                    Document doc = new Document();
                    doc.Add(new Field("LineNumber", sampleDataFileRow.LineNumber.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
                    doc.Add(new Field("LineText", sampleDataFileRow.LineText, Field.Store.YES, Field.Index.ANALYZED));
                    writer.AddDocument(doc);
                }
                analyzer.Close();
                writer.Optimize();
                //writer.Flush(true, false, true);
                writer.Dispose();
            }
        }
Ejemplo n.º 12
0
        public bool BuildIndex(SearchContext context, IList <DocumentData> documentDataList)
        {
            _searchContext = context;
            try
            {
                if (System.IO.Directory.Exists(context.IndexPath))
                {
                    System.IO.Directory.Delete(context.IndexPath, true);
                }

                var indexDirectory = FSDirectory.Open(context.IndexPath);
                var analyser       = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                var indexWriter    = new IndexWriter(indexDirectory, analyser, IndexWriter.MaxFieldLength.UNLIMITED);
                var indexSearcher  = new IndexSearcher(indexDirectory, true);

                foreach (var doc in documentDataList)
                {
                    Document document  = new Document();
                    Field    nameField = new Field("name", doc.FileName, Field.Store.YES, Field.Index.ANALYZED);
                    Field    pathField = new Field("path", doc.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED);
                    document.Add(nameField);
                    document.Add(pathField);
                    indexWriter.AddDocument(document);
                    Trace.WriteLine(string.Format("Added {0} to Index", doc.FilePath));
                    //System.IO.Path.GetDirectoryName(doc.FilePath), doc.FileName));
                }
                indexWriter.Optimize();
                analyser.Close();
                indexWriter.Dispose();
                indexDirectory.Dispose();
            }
            catch (Exception ex)
            {
                return(false);
            }



            return(true);
        }
Ejemplo n.º 13
0
        public static void CreateIndexFromDb()
        {
            using (LuceneDirectory)
            {
                //var analyzer = new RuAnalyzer();
                var analyzer = new StandardAnalyzer(Version);
                using (var writer = new IndexWriter(LuceneDirectory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    writer.SetRAMBufferSizeMB(20);

                    // add data to lucene search index (replaces older entries if any)
                    using (var db = new SQLDataAccess())
                    {
                        //Replace by Evgeni to increase serach abilities
                        // db.cmd.CommandText = "select productId,ArtNo, Name from Catalog.Product";
                        //db.cmd.CommandText = "select productId, (Name + ' ' + ArtNo + ' ' + Description) as Name from Catalog.Product where Enabled = 1";
                        db.cmd.CommandText = "select productId,ArtNo, (Name +  ' ' + ISNULL(ManufactureArtNo,'') + ' '  + ISNULL(EAN,'') + ' '+ Replace(ArtNo,'.','') +' '+ + Replace(ArtNo,'-','')  + ' ' + SUBSTRING ( artno ,1 , 1 )+'-' +SUBSTRING ( artno ,2 , 3 ) + '-' + SUBSTRING ( artno ,5 , 3 ) +'-' +SUBSTRING ( artno ,8 , 3 ) + ' ' ) as Name from Catalog.Product where Enabled = 1";

                        db.cmd.CommandType = CommandType.Text;
                        db.cnOpen();
                        using (var reader = db.cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Thread.Sleep(0);
                                AddToLuceneIndex(new SampleData(SQLDataHelper.GetInt(reader["productId"]), SQLDataHelper.GetString(reader, "ArtNo"), SQLDataHelper.GetString(reader["Name"])), writer);
                            }
                        }
                        db.cnClose();
                    }
                    // close handles
                    analyzer.Close();
                    writer.Optimize();
                    writer.Close();
                    writer.Dispose();
                }
                LuceneDirectory.Close();
            }
        }
Ejemplo n.º 14
0
        public bool ClearLuceneIndex()
        {
            try
            {
                var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                lock (_lock)
                {
                    using (var writer = new IndexWriter(_directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        writer.SetMergeScheduler(new SerialMergeScheduler());

                        writer.DeleteAll();

                        analyzer.Close();
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 15
0
        public bool ClearLuceneIndex()
        {
            try
            {
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);
                using (var writer = new IndexWriter(Directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    // remove older index entries
                    writer.DeleteAll();

                    // close handles
                    analyzer.Close();
                    writer.Dispose();
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat(" ClearLuceneIndex - error [{0}] - \r\n {1} \r\n\r\n", ex.Message, ex.StackTrace);

                return(false);
            }
            return(true);
        }
Ejemplo n.º 16
0
        private static PagedList <LuceneSearchModel> _search(string searchQuery, int pageIndex, int pageSize)
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            {
                return(null);
            }
            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false))
            {
                const int hitsLimit = 1000;
                var       analyzer  = new StandardAnalyzer(Version.LUCENE_30);

                var parser = new MultiFieldQueryParser(Version.LUCENE_30, new[] { AppConstants.LucId, AppConstants.LucTopicName, AppConstants.LucPostContent }, analyzer);
                var query  = parseQuery(searchQuery, parser);
                searcher.SetDefaultFieldSortScoring(true, true);
                var hits    = searcher.Search(query, null, hitsLimit, Sort.INDEXORDER).ScoreDocs;
                var results = _mapLuceneToDataList(hits, searcher, pageIndex, pageSize);
                analyzer.Close();
                searcher.Dispose();
                return(results);
            }
        }
        public static List <Advertisement> GetByIndexRecords(string name1, int one, string name2, int two)
        {
            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false)) {
                var hits_limit = 1000;
                var analyzer   = new StandardAnalyzer(Version.LUCENE_30);

                // search by single field
                var parser = new MultiFieldQueryParser
                                 (Version.LUCENE_30, new[] { name1, name2 }, analyzer);
                Query query1 = new TermQuery(new Term(name1, one.ToString()));
                Query query2 = new TermQuery(new Term(name2, two.ToString()));

                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.Add(query1, Occur.MUST);
                booleanQuery.Add(query2, Occur.MUST);

                var hits    = searcher.Search(booleanQuery, hits_limit).ScoreDocs;
                var results = _mapLuceneToDataList(hits, searcher);
                analyzer.Close();
                searcher.Dispose();
                return(results);
            }
        }
        public bool AddDocumentToIndex(string fullFilePath, SearchContext context)
        {
            using (var indexDirectory = FSDirectory.Open(context.IndexPath))
            {
                using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                {
                    using (var indexWriter = new IndexWriter(indexDirectory, analyser, false, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        Document document = new Document {
                        };
                        DocumentData doc  = new DocumentData
                        {
                            FileName  = Path.GetFileNameWithoutExtension(fullFilePath),
                            Extention = Path.GetExtension(fullFilePath),
                            FilePath  = fullFilePath
                        };

                        Field nameField = new Field("name", doc.FileName, Field.Store.YES, Field.Index.ANALYZED);
                        Field extField  = new Field("ext", doc.Extention, Field.Store.YES, Field.Index.NOT_ANALYZED);
                        Field pathField = new Field("path", doc.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED);
                        document.Add(nameField);
                        document.Add(extField);
                        document.Add(pathField);

                        indexWriter?.AddDocument(document);
                        indexWriter?.Optimize();
                        DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                            Data = string.Format("File Created: {0}", doc.FilePath)
                        });
                    }
                    analyser.Close();
                }
            }

            return(true);
        }
Ejemplo n.º 19
0
        public bool ClearLuceneIndexRecord(int record_id)
        {
            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);

            using (var writer = new IndexWriter(_directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                try
                {
                    var searchQuery = new TermQuery(new Term("Id", record_id.ToString()));
                    writer.DeleteDocuments(searchQuery);
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
                finally
                {
                    analyzer.Close();
                    writer.Close();
                    writer.Dispose();
                }
            }
        }
Ejemplo n.º 20
0
        private static IEnumerable <Building> _search(string searchQuery, string searchField = "")
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            {
                return(new List <Building>());
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, true))
            {
                var hits_limit = 1000;
                var analyzer   = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);


                var parser  = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, " Street", analyzer);
                var query   = parseQuery(searchQuery, parser);
                var hits    = searcher.Search(query, hits_limit).ScoreDocs;
                var results = _mapLuceneToDataList(hits, searcher);
                analyzer.Close();
                searcher.Dispose();
                return(results);
            }
        }
        public bool BuildIndex(SearchContext context)
        {
            _searchContext = context;

            try
            {
                if (System.IO.Directory.Exists(context.IndexPath))
                {
                    System.IO.Directory.Delete(context.IndexPath, true);
                }

                using (var indexDirectory = FSDirectory.Open(context.IndexPath))
                {
                    using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                    {
                        using (var indexWriter = new IndexWriter(indexDirectory, analyser, IndexWriter.MaxFieldLength.UNLIMITED))
                        {
                            //yield return call
                            foreach (var doc in _fileScanner.GetFileListWithFullPath(context.ScanPath))
                            {
                                if (doc == null)
                                {
                                    continue;
                                }

                                Document document  = new Document();
                                Field    nameField = new Field("name", doc.FileName, Field.Store.YES, Field.Index.ANALYZED);
                                Field    extField  = new Field("ext", doc.Extention, Field.Store.YES, Field.Index.NOT_ANALYZED);
                                Field    pathField = new Field("path", doc.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED);

                                document.Add(nameField);
                                document.Add(pathField);
                                document.Add(extField);
                                indexWriter?.AddDocument(document);
                                //Trace.WriteLine(string.Format("Added {0} to Index", doc.FileName));
                                //currStatus = string.Format("Added {0} to Index", doc.FilePath);

                                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                                    Data = string.Format("Indexing: {0}", doc.FilePath)
                                });
                            }
                            indexWriter.Optimize();
                        }
                        analyser.Close();
                    }
                }

                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                    Data = "Indexing Complete !!"
                });
            }
            catch (Exception ex)
            {
                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                    Data = string.Format("Error: {0}", ex.Message)
                });

                return(false);
            }
            return(true);
        }
        /*
         * 分词函数
         * @srcdata:待分词的文本
         * 返回值:按照学长格式定义的分词结果的string表示
         * 即{<分词1>}{<分词2>}...{<分词n>}
         */

        //	这个函数是核心
        //	输入是待分词的内容
        //	输出是分词结果
        //	分词结果的格式是{<word>}
        //	这个格式是学长定义的,我们为了不破坏既定的接口,沿用了这个格式
        //	这个函数的工作原理主要是调用了Lucene.Net.Analysis和Lucene.China的接口
        //	调用这两个接口的配置工作很简单:1.在引用中加入dll文件 2.在可执行程序的目录下放置一个data文件夹,文件夹内有两个文件,分别是sDict和sNoise
        //	存放词库和噪声

        /*private bool isChineseWord(string word)
         * {
         *  if (word == null)
         *  {
         *      return false;
         *  }
         *  for (int i = 0; i < word.Length; i++)
         *  {
         *      char chr = word[i];
         *      if (!(chr >= 0x4E00 && chr <= 0x9FFF))
         *      {
         *          return false;
         *      }
         *  }
         *
         *  return true;
         * }*/

        /*private string word_seg(string srcdata)
         * {
         *  //StringBuilder sb = new StringBuilder();
         *  //sb.Remove(0, sb.Length);
         *  string t1 = "";
         *  ChineseAnalyzer analyzer = new Lucene.China.ChineseAnalyzer();
         *  //string FilePath = @"C:\Users\梁亦清\Documents\Visual Studio 2013\Projects\中科院分词简例\1.htm";
         *
         *  StringReader sr = new StringReader(srcdata);
         *  //Console.WriteLine(sr.ToString());
         *  //Environment.Exit(0);
         *  TokenStream stream = analyzer.TokenStream("", sr);
         *
         *  //long begin = System.DateTime.Now.Ticks;
         *  Lucene.Net.Analysis.Token t = stream.Next();
         *  while (t != null)
         *  {
         *      /*
         *      t1 = t.ToString();   //显示格式: (关键词,0,2) ,需要处理
         *      t1 = t1.Replace("(", "");
         *      char[] separator = { ',' };
         *      t1 = t1.Split(separator)[0];
         *      if (isChineseWord(t1))
         *      {
         *          sb.Append("{<" + t1 + ">}");
         *      }
         *      t = stream.Next();
         *  }
         *  //return sb.ToString()
         * }*/



        //	这个函数是学长代码的对外接口,我们沿用了这个接口,但使用的分词方法不是朴素贝叶斯

        /*public string DoWordSegment(string strIn)
         * {
         *  return word_seg(strIn);
         *
         * }*/

        public List <string> cutwords(string words, string analyzer = "Lucene.China.ChineseAnalyzer")
        {
            List <string> results = new List <string>();

            switch (analyzer)
            {
            case "Lucene.Net.Analysis.SimpleAnalyzer":
                SimpleAnalyzer            analyzerInstance0 = new SimpleAnalyzer();
                TokenStream               ts0 = analyzerInstance0.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token0;
                while ((token0 = ts0.Next()) != null)
                {
                    results.Add(token0.TermText());
                }
                ts0.Close();
                analyzerInstance0.Close();
                break;

            case "Lucene.Net.Analysis.KeywordAnalyzer":
                KeywordAnalyzer           analyzerInstance1 = new KeywordAnalyzer();
                TokenStream               ts1 = analyzerInstance1.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token1;
                while ((token1 = ts1.Next()) != null)
                {
                    results.Add(token1.TermText());
                }
                ts1.Close();
                analyzerInstance1.Close();
                break;

            case "Lucene.Net.Analysis.StopAnalyzer":
                StopAnalyzer analyzerInstance2 = new StopAnalyzer();
                TokenStream  ts2 = analyzerInstance2.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token2;
                while ((token2 = ts2.Next()) != null)
                {
                    results.Add(token2.TermText());
                }
                ts2.Close();
                analyzerInstance2.Close();
                break;

            case "Lucene.Net.Analysis.WhitespaceAnalyzer":
                WhitespaceAnalyzer        analyzerInstance3 = new WhitespaceAnalyzer();
                TokenStream               ts3 = analyzerInstance3.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token3;
                while ((token3 = ts3.Next()) != null)
                {
                    results.Add(token3.TermText());
                }
                ts3.Close();
                analyzerInstance3.Close();
                break;

            case "Lucene.Net.Analysis.PanGu.PanGuAnalyzer":
                PanGu.Segment.Init(@"G:\CProjects\Pipeline\pipeline\Pipeline\bin\Release\PanGu.xml");
                PanGuAnalyzer             analyzerInstance4 = new PanGuAnalyzer();
                TokenStream               ts4 = analyzerInstance4.TokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token4;
                while ((token4 = ts4.Next()) != null)
                {
                    results.Add(token4.TermText());
                }
                ts4.Close();
                analyzerInstance4.Close();
                break;

            case "Lucene.Net.Analysis.Standard.StandardAnalyzer":
                StandardAnalyzer          analyzerInstance5 = new StandardAnalyzer();
                TokenStream               ts5 = analyzerInstance5.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token5;
                while ((token5 = ts5.Next()) != null)
                {
                    results.Add(token5.TermText());
                }
                ts5.Close();
                analyzerInstance5.Close();
                break;

            case "Lucene.China.ChineseAnalyzer":
            default:
                ChineseAnalyzer           analyzerInstance6 = new ChineseAnalyzer();
                TokenStream               ts6 = analyzerInstance6.ReusableTokenStream("", new StringReader(words));
                Lucene.Net.Analysis.Token token6;
                while ((token6 = ts6.Next()) != null)
                {
                    results.Add(token6.TermText());
                }
                ts6.Close();
                analyzerInstance6.Close();
                break;
            }
            return(results);
        }
Ejemplo n.º 23
0
        public void IndexSelectedReviews(ISet <string> reviewIds)
        {
            StandardAnalyzer analyzer = null;
            IndexWriter      writer   = null;

            try
            {
                analyzer = new StandardAnalyzer(Version.LUCENE_30);
                writer   = new IndexWriter(_dirLocation, analyzer,
                                           IndexWriter.MaxFieldLength.UNLIMITED);

                var tableManager = new TableManager();

                var reviewList = tableManager.GetReviewsById(GenerateListFromSet(reviewIds));

                foreach (var id in reviewIds)
                {
                    if (reviewList.ContainsKey(id))
                    {
                        Trace.TraceInformation("Adding {0} to the index", id);

                        var reviewEntity = reviewList[id];

                        // delete entry if exists
                        var searchQuery = new TermQuery(new Term(Constants.Constants.Field_Id, id));
                        writer.DeleteDocuments(searchQuery);

                        // add to index again
                        var doc = new Document();
                        doc.Add(new Field(Constants.Constants.Field_Id, reviewEntity.ReviewId, Field.Store.YES,
                                          Field.Index.NOT_ANALYZED));

                        doc.Add(new Field(Constants.Constants.Field_EntityType, Constants.Constants.Field_EntityType_Reviews, Field.Store.YES,
                                          Field.Index.NOT_ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_ReviewerName, reviewEntity.ReviewerName, Field.Store.YES,
                                          Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_EntityType_ReviewText, reviewEntity.Review, Field.Store.YES, Field.Index.ANALYZED));

                        writer.AddDocument(doc);
                    }
                    else
                    {
                        Trace.TraceWarning("movie {0} not present in db", id);
                    }
                }
            }
            catch (Exception err)
            {
                Trace.TraceError("Failed to build index {0}", err);
            }
            finally
            {
                if (analyzer != null)
                {
                    analyzer.Close();
                }
                if (writer != null)
                {
                    writer.Dispose();
                }
            }
        }
Ejemplo n.º 24
0
        private void MakeSearch(SearchModel model)
        {
            string query = model.SearchText + "~";

            DateTime      datastart = DateTime.Now;
            Directory     directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("~/Data/Index")));
            IndexSearcher searcher  = new IndexSearcher(directory, true);
            Analyzer      analyzer  = new StandardAnalyzer(Version.LUCENE_29);

            List <string> strings = new List <string>();

            foreach (var checkBox in model.CheckBoxes)
            {
                if (checkBox.IsChecked)
                {
                    if (checkBox.SearchType == SearchType.Courses)
                    {
                        //make filtration here...
                        strings.Add("Name");
                        strings.Add("Content");
                    }
                    if (checkBox.SearchType == SearchType.Topics)
                    {
                        //make filtration here...
                        strings.Add("Topic");
                    }
                    if (checkBox.SearchType == SearchType.Users)
                    {
                        //make filtration here...
                        strings.Add("User");
                    }
                    if (checkBox.SearchType == SearchType.Disciplines)
                    {
                        //make filtration here...
                        strings.Add("Discipline");
                    }
                    //make filtration here...
                }
            }

            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                Version.LUCENE_29,
                strings.ToArray(),
                //new String[] { "Name", "Content", "Discipline", "User", "Group", "Topic" },
                analyzer
                );

            ScoreDoc[] scoreDocs = searcher.Search(queryParser.Parse(query), 100).scoreDocs;

            Hits hit   = searcher.Search(queryParser.Parse(query));
            int  total = hit.Length();

            List <Discipline>       disciplines123 = _CurriculmService.GetDisciplines(_UserService.GetCurrentUser()).ToList();
            List <Course>           courses123     = _CourseService.GetCourses(_UserService.GetCurrentUser()).ToList();
            List <TopicDescription> topics123      = _CurriculmService.GetTopicsAvailableForUser(_UserService.GetCurrentUser()).ToList();

            //List<Discipline> topics123 = _CurriculmService.GetDisciplinesWithTopicsOwnedByUser(_UserService.GetCurrentUser()).ToList();
            //foreach(Discipline curr in disciplines123){
            //    topics123.InsertRange(topics123.Count - 1, _CurriculmService.GetTopicsByDisciplineId(curr.Id));
            //}

            List <ISearchResult> results = new List <ISearchResult>();
            Stopwatch            sw      = new Stopwatch();

            sw.Start();

            foreach (ScoreDoc doc in scoreDocs)
            {
                ISearchResult result;
                Document      document = searcher.Doc(doc.doc);
                String        type     = document.Get("Type").ToLower();

                switch (type)
                {
                case "node":

                    Node node = new Node();
                    node.Id       = Convert.ToInt32(document.Get("NodeID"));
                    node.Name     = document.Get("Name");
                    node.CourseId = Convert.ToInt32(document.Get("NodeCourseID"));
                    node.IsFolder = Convert.ToBoolean(document.Get("isFolder"));

                    result = new NodeResult(node, _CourseService.GetCourse(node.CourseId).Name, document.Get("Content"), _CourseService.GetCourse(node.CourseId).Updated.ToString());
                    results.Add(result);
                    break;

                case "course":

                    Course course = new Course();
                    course.Id   = Convert.ToInt32(document.Get("CourseID"));
                    course.Name = document.Get("Name");
                    foreach (Course cour in courses123)
                    {
                        if (cour.Id == course.Id)
                        {
                            result = new CourseResult(course, _CourseService.GetCourse(course.Id).Updated.ToString(), _CourseService.GetCourse(course.Id).Owner);
                            results.Add(result);
                            break;
                        }
                    }
                    break;

                case "discipline":

                    Discipline discipline = new Discipline();
                    discipline.Id    = Convert.ToInt32(document.Get("DisciplineID"));
                    discipline.Name  = document.Get("Discipline");
                    discipline.Owner = document.Get("Owner");

                    string str = _CurriculmService.GetDiscipline(discipline.Id).Owner;
                    foreach (Discipline curr in disciplines123)
                    {
                        if (curr.Owner.Equals(discipline.Owner))
                        {
                            result = new DisciplineResult(discipline, _CurriculmService.GetDiscipline(discipline.Id).Updated.ToString());
                            results.Add(result);
                            break;
                        }
                    }
                    break;

                case "user":

                    User user = new User();
                    user.Id   = Guid.Parse(document.Get("UserID"));
                    user.Name = document.Get("User");
                    //user.Roles
                    /*user.RoleId = Convert.ToInt32(document.Get("RoleId"));*/

                    result = new UserResult(user);
                    results.Add(result);
                    break;

                case "group":

                    Group group = new Group();
                    group.Id   = int.Parse(document.Get("GroupID"));
                    group.Name = document.Get("Group");
                    result     = new GroupResult(group);
                    results.Add(result);
                    break;

                case "topic":

                    Topic topic = new Topic();
                    topic.Id   = Convert.ToInt32(document.Get("TopicID"));
                    topic.Name = document.Get("Topic");
                    if (document.Get("CourseRef") == "null")
                    {
                        topic.CourseRef = null;
                    }
                    else
                    {
                        topic.CourseRef = Convert.ToInt32(document.Get("CourseRef"));
                    }

                    foreach (TopicDescription themdesc in topics123)
                    {
                        if (themdesc.Topic.Id == topic.Id)
                        {
                            result = new TopicResult(topic, _CourseService.GetCourse(topic.CourseRef.Value).Name);
                            results.Add(result);
                            break;
                        }
                    }
                    break;

                default:
                    throw new Exception("Unknown result type");
                }
            }
            sw.Stop();

            DateTime dataend = DateTime.Now;

            analyzer.Close();
            searcher.Close();
            directory.Close();

            //ViewData["SearchString"] = query;
            //ViewData["score"] = Math.Abs(dataend.Millisecond - datastart.Millisecond); //sw.ElapsedMilliseconds.ToString();
            //ViewData["total"] = total;


            model.SearchResult = results;

            model.Total = total;
            model.Score = Math.Abs(dataend.Millisecond - datastart.Millisecond);
        }
Ejemplo n.º 25
0
        private static IEnumerable <LuceneSearchData> _search
            (string searchQuery, string searchField = "", string role = null)
        {
            // validation
            if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
            {
                return(new List <LuceneSearchData>());
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(_directory, false))
            {
                var hits_limit = 1000;
                var analyzer   = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

                // search by single field
                if (!string.IsNullOrEmpty(searchField))
                {
                    var parser  = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, analyzer);
                    var query   = parseQuery(searchQuery, parser);
                    var hits    = searcher.Search(query, hits_limit).ScoreDocs;
                    var results = _mapLuceneToDataList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
                // search by multiple fields (ordered by RELEVANCE)
                else
                {
                    var parser = new MultiFieldQueryParser
                                     (Lucene.Net.Util.Version.LUCENE_30, new[] {
                        "Name",
                        "name_en",
                        "name_zh",
                        "name_cn",
                        "Description",
                        "desc_en",
                        "desc_zh",
                        "desc_cn",
                    }, analyzer);
                    var query = parseQuery(searchQuery, parser);

                    BooleanQuery bq = new BooleanQuery();
                    bq.Add(query, Occur.MUST);

                    if (role == "trading")
                    {
                        var role_parser = new MultiFieldQueryParser
                                              (Lucene.Net.Util.Version.LUCENE_30, new[] {
                            "is_trading",
                            "is_member",
                            "is_visitor",
                        }, analyzer);

                        role_parser.DefaultOperator = QueryParser.AND_OPERATOR;

                        var role_query = parseQuery("1", role_parser);
                        bq.Add(role_query, Occur.MUST);
                    }
                    else if (role == "member")
                    {
                        var role_parser = new MultiFieldQueryParser
                                              (Lucene.Net.Util.Version.LUCENE_30, new[] {
                            "is_member",
                            "is_visitor",
                        }, analyzer);

                        role_parser.DefaultOperator = QueryParser.AND_OPERATOR;

                        var role_query = parseQuery("1", role_parser);
                        bq.Add(role_query, Occur.MUST);
                    }
                    else
                    {
                        var role_parser = new MultiFieldQueryParser
                                              (Lucene.Net.Util.Version.LUCENE_30, new[] {
                            "is_visitor",
                        }, analyzer);

                        role_parser.DefaultOperator = QueryParser.AND_OPERATOR;

                        var role_query = parseQuery("1", role_parser);
                        bq.Add(role_query, Occur.MUST);
                    }


                    var        scorer    = new QueryScorer(bq);
                    var        hits      = searcher.Search(bq, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
                    IFormatter formatter = new SimpleHTMLFormatter("<span style=\"font-weight:bold; background-color:yellow;\">", "</span>");

                    SimpleFragmenter fragmenter = new SimpleFragmenter(1000);

                    Highlighter highlighter = new Highlighter(formatter, scorer);
                    highlighter.TextFragmenter = fragmenter;

                    var results = _mapLuceneToDataList(hits, searcher, highlighter, analyzer);
                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Searches the index.
        /// </summary>
        /// <param name="totalHits">The total hits.</param>
        /// <param name="forumId">The forum identifier.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="searchQuery">The search query.</param>
        /// <param name="searchField">The search field.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>
        /// Returns the Search results
        /// </returns>
        private List <SearchMessage> SearchIndex(
            out int totalHits,
            int forumId,
            int userId,
            string searchQuery,
            string searchField = "",
            int pageIndex      = 1,
            int pageSize       = 1000)
        {
            if (searchQuery.Replace("*", string.Empty).Replace("?", string.Empty).IsNotSet())
            {
                totalHits = 0;
                return(new List <SearchMessage>());
            }

            // Insert forum access here
            var userAccessList = this.GetRepository <vaccess>().Get(v => v.UserID == userId);

            // filter forum
            if (forumId > 0)
            {
                userAccessList = userAccessList.FindAll(v => v.ForumID == forumId);
            }

            var searcher = this.GetSearcher();

            var hitsLimit = this.Get <YafBoardSettings>().ReturnSearchMax;

            // 0 => Lucene error;
            if (hitsLimit == 0)
            {
                hitsLimit = pageSize;
            }

            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            var         formatter  = new SimpleHTMLFormatter("<mark>", "</mark>");
            var         fragmenter = new SimpleFragmenter(hitsLimit);
            QueryScorer scorer;

            // search by single field
            if (searchField.IsSet())
            {
                var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, analyzer);
                var query  = ParseQuery(searchQuery, parser);
                scorer = new QueryScorer(query);

                var hits = searcher.Search(query, hitsLimit).ScoreDocs;
                totalHits = hits.Length;

                var highlighter = new Highlighter(formatter, scorer)
                {
                    TextFragmenter = fragmenter
                };

                var results = this.MapSearchToDataList(
                    highlighter,
                    analyzer,
                    searcher,
                    hits,
                    pageIndex,
                    pageSize,
                    userAccessList);

                analyzer.Close();
                searcher.Dispose();

                return(results);
            }
            else
            {
                var parser = new MultiFieldQueryParser(
                    Lucene.Net.Util.Version.LUCENE_30,
                    new[] { "Message", "Topic", "Author" },
                    analyzer);

                var query = ParseQuery(searchQuery, parser);
                scorer = new QueryScorer(query);

                // sort by date
                var sort = new Sort(new SortField("Posted", SortField.STRING, true));
                var hits = searcher.Search(query, null, hitsLimit, sort).ScoreDocs;

                totalHits = hits.Length;
                var highlighter = new Highlighter(formatter, scorer)
                {
                    TextFragmenter = fragmenter
                };

                var results = this.MapSearchToDataList(
                    highlighter,
                    analyzer,
                    searcher,
                    hits,
                    pageIndex,
                    pageSize,
                    userAccessList);

                analyzer.Close();
                searcher.Dispose();
                return(results);
            }
        }
Ejemplo n.º 27
0
        public ActionResult Search(String query)
        {
            if (query == "")
            {
                return(RedirectToAction("Index"));
            }

            query = query + "~";

            DateTime      datastart = DateTime.Now;
            Directory     directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("~/Data/Index")));
            IndexSearcher searcher  = new IndexSearcher(directory, true);
            Analyzer      analyzer  = new StandardAnalyzer(Version.LUCENE_29);

            MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                Version.LUCENE_29,
                new String[] { "Name", "Content", "Curriculum", "User", "Group", "Theme" },
                analyzer
                );


            ScoreDoc[] scoreDocs = searcher.Search(queryParser.Parse(query), 100).scoreDocs;

            Hits hit   = searcher.Search(queryParser.Parse(query));
            int  total = hit.Length();



            List <ISearchResult> results = new List <ISearchResult>();
            Stopwatch            sw      = new Stopwatch();

            sw.Start();
            foreach (ScoreDoc doc in scoreDocs)
            {
                ISearchResult result;
                Document      document = searcher.Doc(doc.doc);
                String        type     = document.Get("Type").ToLower();

                switch (type)
                {
                case "node":
                    Node node = new Node();
                    node.Id       = Convert.ToInt32(document.Get("ID"));
                    node.Name     = document.Get("Name");
                    node.CourseId = Convert.ToInt32(document.Get("CourseID"));
                    node.IsFolder = Convert.ToBoolean(document.Get("isFolder"));

                    result = new NodeResult(node, _CourseService.GetCourse(node.CourseId).Name, document.Get("Content"), _CourseService.GetCourse(node.CourseId).Updated.ToString());

                    break;

                case "course":

                    Course course = new Course();
                    course.Id   = Convert.ToInt32(document.Get("ID"));
                    course.Name = document.Get("Name");

                    result = new CourseResult(course, _CourseService.GetCourse(course.Id).Updated.ToString(), _CourseService.GetCourse(course.Id).Owner);

                    break;

                case "curriculum":

                    Curriculum curriculum = new Curriculum();
                    curriculum.Id   = Convert.ToInt32(document.Get("ID"));
                    curriculum.Name = document.Get("Curriculum");

                    result = new CurriculumResult(curriculum, _CurriculmService.GetCurriculum(curriculum.Id).Updated.ToString());

                    break;

                case "user":

                    User user = new User();
                    user.Id     = Guid.Parse(document.Get("ID"));
                    user.Name   = document.Get("User");
                    user.RoleId = Convert.ToInt32(document.Get("RoleId"));

                    result = new UserResult(user, _UserService.GetRole(user.RoleId).ToString());

                    break;

                case "group":

                    Group group = new Group();
                    group.Id   = int.Parse(document.Get("ID"));
                    group.Name = document.Get("Group");

                    result = new GroupResult(group);

                    break;

                case "theme":

                    Theme theme = new Theme();
                    theme.Id        = Convert.ToInt32(document.Get("ID"));
                    theme.Name      = document.Get("Theme");
                    theme.CourseRef = Convert.ToInt32(document.Get("CourseRef"));

                    result = new ThemeResult(theme, _CourseService.GetCourse(theme.CourseRef).Name);

                    break;

                default:
                    throw new Exception("Unknown result type");
                }

                results.Add(result);
            }
            sw.Stop();

            DateTime dataend = DateTime.Now;

            analyzer.Close();
            searcher.Close();
            directory.Close();

            ViewData["SearchString"] = query;
            ViewData["score"]        = Math.Abs(dataend.Millisecond - datastart.Millisecond); //sw.ElapsedMilliseconds.ToString();
            ViewData["total"]        = total;

            return(View(results));
        }
Ejemplo n.º 28
0
        public IPagedList <ClassifiedAdList> GetClassifiedAdListSearchEngine(int stringIdCategory, string stringIdSubCategory, string searchString = null, int?pageNumber = 1, string searchCategory = "ALL",
                                                                             string searchOnlyOption = "All Ads", string minPrice   = null, string maxPrice      = null, string minMile         = null, string maxMile = null, string modelName = null, string minYear = null, string maxYear = null,
                                                                             string modEngineSize    = null, string modelMake       = null, string modelBodyType = null, string modelDrivetrain = null, string modelTransmission = null, string modelCondition = null, string modelColour = null,
                                                                             string modelJobType     = null, string modelSalaryInfo = null, string modelBedrooms = null, string modelBathrooms  = null, string modelFurnished    = null, string CountryId      = null, string RegionId    = null,
                                                                             string minSize          = null, string maxSize = null, string modelBreed = null, string modelSpecies = null, string minAge = null, string maxAge = null, string ageType = null)
        {
            // LUCENE
            // validation
            // Setup query
            BooleanQuery bq     = new BooleanQuery();
            var          sortBy = new Sort();

            sortBy.SetSort(new SortField("EditTimeStampTicks", SortField.LONG, true), SortField.FIELD_SCORE);
            var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            // Get only Open ads
            bq.Add(new TermQuery(new Lucene.Net.Index.Term("Status", "0")), Occur.MUST);

            if (!String.IsNullOrEmpty(searchString))
            {
                bq.Add(LuceneSearch.parseQuery(searchString, new MultiFieldQueryParser
                                                   (Lucene.Net.Util.Version.LUCENE_30, new[] {
                    "Title", "HtmlFreeDescription", "Country", "Region",
                    "Make", "Model", "Mileage", "Year", "Engine Size", "Condition", "Colour", "Fuel Type", "Transmission", "Drivetrain", "Body Type",
                    "Company Name", "Job Type",
                    "Species", "Breed", "Gender", "Age", "Pet's Name"
                }, analyzer))
                       , Occur.MUST);
                sortBy = Sort.RELEVANCE;
            }

            if (searchOnlyOption.Equals("All Ads"))
            {
                if (stringIdCategory != -100)
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else if (!String.IsNullOrEmpty(stringIdSubCategory))
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else
                {
                    if (!searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
            }
            else if (searchOnlyOption.Equals("Top Ads"))
            {
                if (stringIdCategory != -100)
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("TopAdStatus", "true")), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("TopAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else if (!String.IsNullOrEmpty(stringIdSubCategory))
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("TopAdStatus", "true")), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("TopAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else
                {
                    if (!searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("TopAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
            }
            else if (searchOnlyOption.Equals("Urgent Ads"))
            {
                if (stringIdCategory != -100)
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("UrgentAdStatus", "true")), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("CategoryId", stringIdCategory.ToString())), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("UrgentAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else if (!String.IsNullOrEmpty(stringIdSubCategory))
                {
                    if (searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("UrgentAdStatus", "true")), Occur.MUST);
                    }
                    else
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("SubCategoryStringId", stringIdSubCategory)), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("UrgentAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
                else
                {
                    if (!searchCategory.Equals("ALL"))
                    {
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("UrgentAdStatus", "true")), Occur.MUST);
                        bq.Add(new TermQuery(new Lucene.Net.Index.Term("AdType", searchCategory)), Occur.MUST);
                    }
                }
            }

            //
            // FILTERS
            //max /min price
            if (minPrice != null)
            {
                if (minPrice != "" && maxPrice == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Price", Int32.Parse(minPrice.Replace(",", "")), Int32.Parse(minPrice.Replace(",", "")), true, true), Occur.MUST);
                }
            }

            if (maxPrice != null)
            {
                if (maxPrice != "" && minPrice == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Price", Int32.Parse(maxPrice.Replace(",", "")), Int32.Parse(maxPrice.Replace(",", "")), true, true), Occur.MUST);
                }
            }

            if (maxPrice != null && minPrice != null)
            {
                if (maxPrice != "" && minPrice != "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Price", Int32.Parse(minPrice.Replace(",", "")), Int32.Parse(maxPrice.Replace(",", "")), true, true), Occur.MUST);
                }
            }


            //location
            if (!String.IsNullOrEmpty(CountryId))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("CountryId", CountryId)), Occur.MUST);
            }


            if (!String.IsNullOrEmpty(RegionId))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("RegionId", RegionId)), Occur.MUST);
            }

            //model make
            if (!String.IsNullOrEmpty(modelMake))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Make", modelMake)), Occur.MUST);
            }

            //max / min mileage
            if (minMile != null)
            {
                if (minMile != "" && maxMile == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Mileage", Int32.Parse(minMile.Replace(",", "")), Int32.Parse(minMile.Replace(",", "")), true, true), Occur.MUST);
                }
            }

            if (maxMile != null)
            {
                if (maxMile != "" && minMile == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Mileage", Int32.Parse(maxMile.Replace(",", "")), Int32.Parse(maxMile.Replace(",", "")), true, true), Occur.MUST);
                }
            }

            if (maxMile != null && minMile != null)
            {
                if (maxMile != "" && minMile != "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Mileage", Int32.Parse(minMile.Replace(",", "")), Int32.Parse(maxMile.Replace(",", "")), true, true), Occur.MUST);
                }
            }

            //model name
            if (!String.IsNullOrEmpty(modelName))
            {
                bq.Add(new FuzzyQuery(new Lucene.Net.Index.Term("Model", modelName)), Occur.MUST);
            }


            //max / min year
            if (minYear != null && maxYear == null)
            {
                if (minYear != "" && maxYear == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Year", Int32.Parse(minYear), Int32.Parse(minYear), true, true), Occur.MUST);
                }
            }

            if (maxYear != null && minYear == null)
            {
                if (maxYear != "" && minYear == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Year", Int32.Parse(maxYear), Int32.Parse(maxYear), true, true), Occur.MUST);
                }
            }

            if (maxYear != null && minYear != null)
            {
                if (maxYear != "" && minYear != "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Year", Int32.Parse(minYear), Int32.Parse(maxYear), true, true), Occur.MUST);
                }
            }

            //model body type
            if (!String.IsNullOrEmpty(modelBodyType))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Body Type", modelBodyType)), Occur.MUST);
            }

            //model drivetrain
            if (!String.IsNullOrEmpty(modelDrivetrain))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Drivetrain", modelDrivetrain)), Occur.MUST);
            }

            //model transmission
            if (!String.IsNullOrEmpty(modelTransmission))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Transmission", modelTransmission)), Occur.MUST);
            }

            //model condition
            if (!String.IsNullOrEmpty(modelCondition))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Condition", modelCondition)), Occur.MUST);
            }

            //model colour
            if (!String.IsNullOrEmpty(modelColour))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Colour", modelColour)), Occur.MUST);
            }

            //model engine size
            if (!String.IsNullOrEmpty(modEngineSize))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Engine Size", modEngineSize)), Occur.MUST);
            }

            //------JOBS---------
            //model job type
            if (!String.IsNullOrEmpty(modelJobType))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Job Type", modelJobType)), Occur.MUST);
            }

            //model salary type
            if (!String.IsNullOrEmpty(modelSalaryInfo))
            {
                bq.Add(new TermQuery(new Lucene.Net.Index.Term("Salary Type", modelSalaryInfo)), Occur.MUST);
            }

            //-----REAL ESTATE----
            //model min/max size
            if (minSize != null)
            {
                if (minSize != "" && maxSize == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Size", Int32.Parse(minSize), Int32.Parse(minSize), true, true), Occur.MUST);
                }
            }

            if (maxSize != null)
            {
                if (maxSize != "" && minSize == "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Size", Int32.Parse(maxSize), Int32.Parse(maxSize), true, true), Occur.MUST);
                }
            }

            if (maxSize != null && minSize != null)
            {
                if (maxSize != "" & minSize != "")
                {
                    bq.Add(NumericRangeQuery.NewIntRange("Size", Int32.Parse(minSize), Int32.Parse(maxSize), true, true), Occur.MUST);
                }
            }

            //model bedrooms
            if (modelBathrooms != null)
            {
                if (modelBedrooms != "")
                {
                    bq.Add(new TermQuery(new Lucene.Net.Index.Term("Bedrooms", modelBedrooms)), Occur.MUST);
                }
            }

            //model bathrooms
            if (modelBathrooms != null)
            {
                if (modelBathrooms != "")
                {
                    bq.Add(new TermQuery(new Lucene.Net.Index.Term("Bathrooms", modelBathrooms)), Occur.MUST);
                }
            }

            //model furnished
            if (modelFurnished != null)
            {
                if (modelFurnished != "")
                {
                    bq.Add(new TermQuery(new Lucene.Net.Index.Term("Furnished", modelFurnished)), Occur.MUST);
                }
            }

            //-----Pets--------
            ///////////////////
            if (modelSpecies != null)
            {
                if (modelSpecies != "")
                {
                    bq.Add(new TermQuery(new Lucene.Net.Index.Term("Species", modelSpecies)), Occur.MUST);
                }
            }

            if (modelBreed != null)
            {
                if (modelBreed != "")
                {
                    bq.Add(new TermQuery(new Lucene.Net.Index.Term("Breed", modelBreed)), Occur.MUST);
                }
            }

            //model min/max age
            if (minAge != null && maxAge == null)
            {
                if (minAge != "" && maxAge == "")
                {
                    bq.Add(new TermRangeQuery("Age", minAge, minAge, true, true), Occur.MUST);
                }
            }

            if (maxAge != null && minAge == null)
            {
                if (maxAge != "" && minAge == "")
                {
                    bq.Add(new TermRangeQuery("Age", maxAge, maxAge, true, true), Occur.MUST);
                }
            }

            if (maxAge != null && minAge != null)
            {
                if (maxAge != "" && minAge != "")
                {
                    bq.Add(new TermRangeQuery("Age", minAge, maxAge, true, true), Occur.MUST);
                }
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(LuceneSearch._getDir, false))
            {
                var hits_limit = 1000;
                var hits       = searcher.Search(bq, null, hits_limit, sortBy).ScoreDocs;
                var results    = LuceneSearch._mapLuceneToDataList(hits, searcher);
                analyzer.Close();
                searcher.Dispose();

                var temppaged = new PagedList <ClassifiedAdList>(results, pageNumber.Value, RecordsPerPage.recordsPerPage);

                return(temppaged);
            }
        }
Ejemplo n.º 29
0
        public void IndexSelectedMovies(ISet <string> movieIds)
        {
            StandardAnalyzer analyzer = null;
            IndexWriter      writer   = null;

            try
            {
                analyzer = new StandardAnalyzer(Version.LUCENE_30);
                writer   = new IndexWriter(_dirLocation, analyzer,
                                           IndexWriter.MaxFieldLength.UNLIMITED);

                var tableManager = new TableManager();

                var movieList = tableManager.GetMoviesByid(GenerateListFromSet(movieIds));

                foreach (var id in movieIds)
                {
                    if (movieList.ContainsKey(id))
                    {
                        Trace.TraceInformation("Adding {0} to the index", id);

                        var movieEntity = movieList[id];

                        // delete entry if exists
                        var searchQuery = new TermQuery(new Term(Constants.Constants.Field_Id, id));
                        writer.DeleteDocuments(searchQuery);

                        // add to index again
                        var doc = new Document();
                        doc.Add(new Field(Constants.Constants.Field_Id, movieEntity.MovieId, Field.Store.YES,
                                          Field.Index.NOT_ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_Name, movieEntity.Name, Field.Store.YES,
                                          Field.Index.NOT_ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_AltNames, movieEntity.AltNames, Field.Store.NO, Field.Index.ANALYZED));

                        doc.Add(new Field(Constants.Constants.Field_Actors, movieEntity.Posters, Field.Store.NO,
                                          Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_Directors, movieEntity.Ratings, Field.Store.YES,
                                          Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_MusicDirectors, movieEntity.Casts,
                                          Field.Store.YES, Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_Name, movieEntity.Name, Field.Store.YES,
                                          Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_Producers, movieEntity.Synopsis, Field.Store.YES,
                                          Field.Index.ANALYZED));
                        doc.Add(new Field(Constants.Constants.Field_MovieSynopsis, movieEntity.Synopsis, Field.Store.YES,
                                          Field.Index.ANALYZED));

                        writer.AddDocument(doc);
                    }
                    else
                    {
                        Trace.TraceWarning("movie {0} not present in db", id);
                    }
                }
            }
            catch (Exception err)
            {
                Trace.TraceError("Failed to build index {0}", err);
            }
            finally
            {
                if (analyzer != null)
                {
                    analyzer.Close();
                }
                if (writer != null)
                {
                    writer.Dispose();
                }
            }
        }
Ejemplo n.º 30
0
        private IEnumerable <Product> MainSearch(string searchTerms, string searchField = "")
        {
            // validation
            if (string.IsNullOrEmpty(searchTerms.Replace("*", "").Replace("?", "")))
            {
                return(null);
            }

            // '*' or '?' not allowed as first character in WildcardQuery
            var firstCharacter = searchTerms.Substring(0, 1);

            if (firstCharacter == "*" || firstCharacter == "?")
            {
                searchTerms = searchTerms.Substring(1, searchTerms.Length - 1);
            }

            // set up lucene searcher
            using (var searcher = new IndexSearcher(Directory, false))
            {
                var analyzer = new StandardAnalyzer(Version.LUCENE_30);

                // search by single field
                if (!string.IsNullOrEmpty(searchField))
                {
                    var mainQuery = new BooleanQuery();

                    var languageParser = new QueryParser(Version.LUCENE_30, "Language", analyzer);
                    var languageQuery  = languageParser.Parse(Thread.CurrentThread.CurrentCulture.ThreeLetterISOLanguageName);

                    mainQuery.Add(new BooleanClause(languageQuery, Occur.MUST));

                    var searchParser = new QueryParser(Version.LUCENE_30, searchField, analyzer);
                    var searchQuery  = searchParser.Parse(searchTerms);

                    mainQuery.Add(new BooleanClause(searchQuery, Occur.MUST));

                    var hits    = searcher.Search(mainQuery, MaxNoOfHits).ScoreDocs;
                    var results = MapLuceneToDataList(hits, searcher);

                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
                // search by multiple fields (ordered by RELEVANCE)
                else
                {
                    var mainQuery = new BooleanQuery();

                    var languageParser = new QueryParser(Version.LUCENE_30, "Language", analyzer);
                    var languageQuery  = languageParser.Parse(Thread.CurrentThread.CurrentCulture.ThreeLetterISOLanguageName);

                    mainQuery.Add(new BooleanClause(languageQuery, Occur.MUST));

                    var searchParser = new MultiFieldQueryParser(Version.LUCENE_30, new[] { "AllContent" }, analyzer);
                    var searchQuery  = searchParser.Parse(searchTerms);

                    mainQuery.Add(new BooleanClause(searchQuery, Occur.MUST));

                    var hits = searcher.Search(mainQuery, null, MaxNoOfHits,
                                               new Sort(new SortField(DefaultSortField, SortField.STRING))).ScoreDocs;
                    var results = MapLuceneToDataList(hits, searcher);
                    analyzer.Close();
                    searcher.Dispose();
                    return(results);
                }
            }
        }