Esempio n. 1
0
        public virtual IEnumerable <ISearchable> Search(string filter, int maxResults = SearchDefaults.DefaultResults)
        {
            Initialize();

            var results = new List <ISearchable>();

            lock (_lockObject)
            {
                try
                {
                    Searching.SafeInvoke(this, new SearchEventArgs(filter, results));

                    if (filter.IsValidOrcSearchFilter())
                    {
                        using (var analyzer = new StandardAnalyzer(LuceneDefaults.Version))
                        {
                            var queryAsText = _searchQueryService.GetSearchQuery(filter, GetSearchableMetadata());

                            var parser = new QueryParser(LuceneDefaults.Version, string.Empty, analyzer);
                            var query  = parser.Parse(queryAsText);

                            using (var searcher = new IndexSearcher(_indexDirectory))
                            {
                                var search = searcher.Search(query, maxResults);
                                foreach (var scoreDoc in search.ScoreDocs)
                                {
                                    var score = scoreDoc.Score;
                                    var docId = scoreDoc.Doc;
                                    var doc   = searcher.Doc(docId);

                                    var index = int.Parse(doc.Get(IndexId));
                                    results.Add(_indexedObjects[index]);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "An error occurred while searching, returning default reuslts");
                }
                finally
                {
                    Searched.SafeInvoke(this, new SearchEventArgs(filter, results));
                }
            }

            return(results);
        }
        public virtual IEnumerable <ISearchable> Search(string filter, int maxResults = SearchDefaults.DefaultResults)
        {
            Initialize();

            var results = new List <ISearchable>();

            lock (_lockObject)
            {
                try
                {
                    Searching.SafeInvoke(this, new SearchEventArgs(filter, results));

                    Query finalQuery = null;

                    // Note: There are two issues with using regex here
                    //       1. Lucene uses lower case interpretation of each string for indexing.
                    //          That means in regular expression we can use only lower case characters
                    //       2. escape sequences do not work. Not sure why
                    //
                    //       In order to fix (1), we have to force Lucene to index differently. Probably we need to have two
                    //       versions if indeces. One for regular search and another for regex
                    //var regexString = filter.ExtractRegexString();
                    //if (!string.IsNullOrWhiteSpace(regexString))
                    //{
                    //    var searchableMetadatas = GetSearchableMetadata();

                    //    var booleanQuery = new BooleanQuery();
                    //    foreach (var searchableMetadata in searchableMetadatas)
                    //    {
                    //        var query = new RegexQuery(new Term(searchableMetadata.SearchName, regexString));
                    //        var booleanClause = new BooleanClause(query, Occur.SHOULD);

                    //        booleanQuery.Add(booleanClause);
                    //    }

                    //    if (booleanQuery.Any())
                    //    {
                    //        finalQuery = booleanQuery;
                    //    }
                    //}

                    // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                    if (finalQuery == null && filter.IsValidOrcSearchFilter())
                    {
                        using (var analyzer = new StandardAnalyzer(LuceneDefaults.Version))
                        {
                            var queryAsText = _searchQueryService.GetSearchQuery(filter, GetSearchableMetadata());

                            var parser = new QueryParser(LuceneDefaults.Version, string.Empty, analyzer);
                            finalQuery = parser.Parse(queryAsText);
                        }
                    }

                    if (finalQuery != null)
                    {
                        using (var searcher = new IndexSearcher(_indexDirectory))
                        {
                            var search = searcher.Search(finalQuery, maxResults);
                            foreach (var scoreDoc in search.ScoreDocs)
                            {
                                var score = scoreDoc.Score;
                                var docId = scoreDoc.Doc;
                                var doc   = searcher.Doc(docId);

                                var index = int.Parse(doc.Get(IndexId));
                                results.Add(_indexedObjects[index]);
                            }
                        }
                    }
                }
                catch (ParseException ex)
                {
                    Log.Warning(ex, "Failed to parse search pattern");
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "An error occurred while searching, returning default results");
                }
                finally
                {
                    Searched.SafeInvoke(this, new SearchEventArgs(filter, results));
                }
            }

            return(results);
        }