예제 #1
0
        public bool SetApprovalState(ApprovalState approvalState, Guid rowId)
        {
            var doc = GetDocument(rowId);

            if (doc == null)
            {
                return(false);
            }
            doc.RemoveField(ApprovalField);
            doc.Add(new LuceneField(ApprovalField, approvalState.ToString(), LuceneField.Store.YES, LuceneField.Index.NOT_ANALYZED));

            var writer = GetIndexWriter();

            writer.UpdateDocument(new Term(IdField, rowId.ToString()), doc);
            writer.Close();

            return(true);
        }
예제 #2
0
        private Result GetSearchResults(string searchQuery, string[] searchFields, string sortField, bool sortDescending, int count, int skip, ApprovalState approvalState = ApprovalState.Any)
        {
            var reader   = GetIndexReader();
            var searcher = GetIndexSearcher(reader);

            string sortFieldName;

            if (string.IsNullOrWhiteSpace(sortField))
            {
                sortField      = sortFieldName = CreatedField;
                sortDescending = true;
            }
            else if (sortField == CreatedField)
            {
                sortFieldName = CreatedField;
            }
            else
            {
                sortFieldName = FieldNameForSorting(sortField);
            }

            Query query;

            if (string.IsNullOrWhiteSpace(searchQuery) == false && searchFields != null && searchFields.Any())
            {
                //searchQuery = searchQuery.Replace("*", "").Replace(" ", "* ") + "*";
                if (searchFields.Count() > 1)
                {
                    var parser = new MultiFieldQueryParser(Version.LUCENE_29, searchFields, GetAnalyzer());
                    parser.SetDefaultOperator(QueryParser.Operator.AND);
                    Log.Info("searchQuery =" + searchQuery, null);

                    try
                    {
                        query = parser.Parse(searchQuery.Trim());
                    }
                    catch (ParseException ex)
                    {
                        Log.Error(ex, ex.Message, null);

                        query = parser.Parse(QueryParser.Escape(searchQuery.Trim()));
                    }
                }
                else
                {
                    query = new QueryParser(Version.LUCENE_29, searchFields.FirstOrDefault(), GetAnalyzer()).Parse(searchQuery);
                }
            }
            else
            {
                query = approvalState == ApprovalState.Any
                                        ? new MatchAllDocsQuery()
                                        : (Query) new TermQuery(new Term(ApprovalField, approvalState.ToString()));
            }

            var docs = searcher.Search(
                query,
                null, reader.MaxDoc(),
                new Sort(new SortField(sortFieldName, SortField.STRING, sortDescending))
                );

            var scoreDocs = docs.ScoreDocs;

            var rows = new List <Row>();

            for (var i = skip; i < (skip + count) && i < scoreDocs.Length; i++)
            {
                if (reader.IsDeleted(scoreDocs[i].doc))
                {
                    continue;
                }
                var doc = searcher.Doc(scoreDocs[i].doc);
                var row = ParseRow(doc);
                rows.Add(row);
            }

            searcher.Close();
            reader.Close();

            return(new Result(scoreDocs.Count(), rows, sortField, sortDescending));
        }