コード例 #1
0
        public BooleanFilterTests()
        {
            items = new[]
            {
                new GridModel(),
                new GridModel { IsChecked = true, NIsChecked = false },
                new GridModel { IsChecked = false, NIsChecked = true }
            }.AsQueryable();

            filter = new BooleanFilter();
        }
コード例 #2
0
        public static Filter CreateQuery(ISearchCriteria criteria, ISearchFilter filter, Occur clause)
        {
            var values = GetFilterValues(filter);
            if (values == null) return null;

            var query = new BooleanFilter();
            foreach (var value in values)
            {
                var valueQuery = CreateQueryForValue(criteria, filter, value);
                query.Add(new FilterClause(valueQuery, Occur.SHOULD));
            }

            return query;
        }
コード例 #3
0
        public void OperatorPropertyEmpty_MatchesBoolFilterTest()
        {
            var expectedValue = new SubmissionMetaDataModel()
            {
                DisplayName = nameof(String),
                Name        = nameof(String),
                Type        = nameof(String),
                Value       = nameof(String),
            };

            var filterValue = new SearchTermModel()
            {
                FieldName        = FilterConstants.FilterFieldTypes.StringType,
                FieldType        = FilterConstants.FilterFieldTypes.BooleanType,
                FieldValue       = FilterConstants.FilterFieldTypes.StringType,
                OperatorProperty = FilterConstants.CommonFieldOperators.Empty
            };


            Assert.Throws <NotImplementedException>(() => BooleanFilter.MatchesFilter(expectedValue, filterValue));
        }
コード例 #4
0
ファイル: BooleanFilterBuilder.cs プロジェクト: wwb/lucenenet
        public virtual Filter GetFilter(XmlElement e)
        {
            BooleanFilter bf = new BooleanFilter();
            XmlNodeList   nl = e.ChildNodes;

            for (int i = 0; i < nl.Count; i++)
            {
                XmlNode node = nl.Item(i);
                if (node.LocalName.Equals("Clause", StringComparison.Ordinal))
                {
                    XmlElement          clauseElem = (XmlElement)node;
                    BooleanClause.Occur occurs     = BooleanQueryBuilder.GetOccursValue(clauseElem);

                    XmlElement clauseFilter = DOMUtils.GetFirstChildOrFail(clauseElem);
                    Filter     f            = factory.GetFilter(clauseFilter);
                    bf.Add(new FilterClause(f, occurs));
                }
            }

            return(bf);
        }
コード例 #5
0
        public void TestJustNullDocIdSets()
        {
            BooleanFilter booleanFilter = new BooleanFilter();

            booleanFilter.Add(GetNullDISFilter(), BooleanClause.Occur.MUST);
            TstFilterCard(@"A MUST filter that returns a null DIS should never return documents", 0, booleanFilter);
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetNullDISIFilter(), BooleanClause.Occur.MUST);
            TstFilterCard(@"A MUST filter that returns a null DISI should never return documents", 0, booleanFilter);
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetNullDISFilter(), BooleanClause.Occur.SHOULD);
            TstFilterCard(@"A single SHOULD filter that returns a null DIS should never return documents", 0, booleanFilter);
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetNullDISIFilter(), BooleanClause.Occur.SHOULD);
            TstFilterCard(@"A single SHOULD filter that returns a null DISI should never return documents", 0, booleanFilter);
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetNullDISFilter(), BooleanClause.Occur.MUST_NOT);
            TstFilterCard(@"A single MUST_NOT filter that returns a null DIS should be invisible", 5, booleanFilter);
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetNullDISIFilter(), BooleanClause.Occur.MUST_NOT);
            TstFilterCard(@"A single MUST_NOT filter that returns a null DIS should be invisible", 5, booleanFilter);
        }
コード例 #6
0
        public bool VerifyAppAccount(int tenantId, string agentId, string appSecret)
        {
            var result = false;
            //添加去重条件:AgentId、AppSecret
            //思路:根据AgentId过滤多租赁,数据为空,继续根据AppSecret过滤多租赁,数据为空,则没有重复,继续之后的保存、更新操作
            var agentIdFilter = new BooleanFilter()
                                .Must(new TermFilter(AppAccount.AppAccount_AgentId, agentId));
            var appAccountList = CloudDataHelper.GetEntityAllList(AppAccount.MetaName, tenantId, agentIdFilter).ToList();

            if (appAccountList.Count <= 0)
            {
                var appSecretFilter = new BooleanQuery()
                                      .Must(new MatchQuery(AppAccount.AppAccount_AppSecret, appSecret));
                var appAccount = CloudDataHelper.GetEntityAllList(AppAccount.MetaName, tenantId, queryJson: ElasticSearchSerialization.Serialize(appSecretFilter)).ToList();
                if (appAccount.Count <= 0)
                {
                    result = true;
                }
            }

            return(result);
        }
コード例 #7
0
        public void DoesNotMatchBoolFilterTest(string submitValue, string fieldValue, bool expectedResult)
        {
            var expectedValue = new SubmissionMetaDataModel()
            {
                DisplayName = nameof(String),
                Name        = nameof(String),
                Type        = nameof(String),
                Value       = submitValue
            };

            var filterValue = new SearchTermModel()
            {
                FieldName        = FilterConstants.FilterFieldTypes.StringType,
                FieldType        = FilterConstants.FilterFieldTypes.BooleanType,
                FieldValue       = fieldValue,
                OperatorProperty = FilterConstants.CommonFieldOperators.NotEqual
            };

            var result = BooleanFilter.MatchesFilter(expectedValue, filterValue);

            Assert.Equal(expectedResult, result);
        }
コード例 #8
0
        public void TestShouldsAndMustNot()
        {
            BooleanFilter booleanFilter = new BooleanFilter();

            booleanFilter.Add(GetRangeFilter(@"price", @"010", @"020"), Occur.SHOULD);
            booleanFilter.Add(GetRangeFilter(@"price", @"020", @"030"), Occur.SHOULD);
            booleanFilter.Add(GetTermsFilter(@"inStock", @"N"), Occur.MUST_NOT);
            TstFilterCard(@"Shoulds Ored but AndNot", 4, booleanFilter);

            booleanFilter.Add(GetTermsFilter(@"inStock", @"Maybe"), Occur.MUST_NOT);
            TstFilterCard(@"Shoulds Ored but AndNots", 3, booleanFilter);

            // same with a real DISI (no OpenBitSetIterator)
            booleanFilter = new BooleanFilter();
            booleanFilter.Add(GetRangeFilter(@"price", @"010", @"020"), Occur.SHOULD);
            booleanFilter.Add(GetRangeFilter(@"price", @"020", @"030"), Occur.SHOULD);
            booleanFilter.Add(GetWrappedTermQuery(@"inStock", @"N"), Occur.MUST_NOT);
            TstFilterCard(@"Shoulds Ored but AndNot", 4, booleanFilter);

            booleanFilter.Add(GetWrappedTermQuery(@"inStock", @"Maybe"), Occur.MUST_NOT);
            TstFilterCard(@"Shoulds Ored but AndNots", 3, booleanFilter);
        }
コード例 #9
0
        public void TestToStringOfWrappedBooleanFilters()
        {
            BooleanFilter orFilter = new BooleanFilter();

            BooleanFilter stockFilter = new BooleanFilter();

            stockFilter.Add(new FilterClause(GetTermsFilter(@"inStock", @"Y"), Occur.MUST));
            stockFilter.Add(new FilterClause(GetTermsFilter(@"barCode", @"12345678"), Occur.MUST));

            orFilter.Add(new FilterClause(stockFilter, Occur.SHOULD));

            BooleanFilter productPropertyFilter = new BooleanFilter();

            productPropertyFilter.Add(new FilterClause(GetTermsFilter(@"isHeavy", @"N"), Occur.MUST));
            productPropertyFilter.Add(new FilterClause(GetTermsFilter(@"isDamaged", @"Y"), Occur.MUST));

            orFilter.Add(new FilterClause(productPropertyFilter, Occur.SHOULD));

            BooleanFilter composedFilter = new BooleanFilter();

            composedFilter.Add(new FilterClause(orFilter, Occur.MUST));
            assertEquals(@"BooleanFilter(+BooleanFilter(BooleanFilter(+inStock:Y +barCode:12345678) BooleanFilter(+isHeavy:N +isDamaged:Y)))", composedFilter.ToString());
        }
コード例 #10
0
        /// <summary>
        ///     Builds the query.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public virtual object BuildQuery(ISearchCriteria criteria)
        {
            var queryBuilder = new QueryBuilder();
            var queryFilter  = new BooleanFilter();
            var query        = new BooleanQuery();

            queryBuilder.Query  = query;
            queryBuilder.Filter = queryFilter;

            if (criteria.CurrentFilters != null)
            {
                foreach (var filter in criteria.CurrentFilters)
                {
                    // Skip currencies that are not part of the filter
                    if (filter.GetType() == typeof(PriceRangeFilter)) // special filtering
                    {
                        var currency = (filter as PriceRangeFilter).Currency;
                        if (!currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    var filterQuery = LuceneQueryHelper.CreateQuery(criteria, filter, Occur.SHOULD);

                    // now add other values that should also be counted?

                    if (filterQuery != null)
                    {
                        var clause = new FilterClause(filterQuery, Occur.MUST);
                        queryFilter.Add(clause);
                    }
                }
            }

            return(queryBuilder);
        }
コード例 #11
0
        /// <summary>
        /// Searches for similar words
        /// </summary>
        /// <param name="userId">
        /// The user identifier.
        /// </param>
        /// <param name="filter">
        /// The filter.
        /// </param>
        /// <param name="searchQuery">
        /// The search query.
        /// </param>
        /// <param name="searchField">
        /// The search field.
        /// </param>
        /// <returns>
        /// Returns the Search results
        /// </returns>
        private List <SearchMessage> SearchSimilarIndex(int userId, string filter, string searchQuery, string searchField)
        {
            if (searchQuery.Replace("*", string.Empty).Replace("?", string.Empty).IsNotSet())
            {
                return(new List <SearchMessage>());
            }

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

            var searcher = this.GetSearcher();

            if (searcher == null)
            {
                return(new List <SearchMessage>());
            }

            var booleanFilter = new BooleanFilter
            {
                new FilterClause(new TermsFilter(new Term("TopicId", filter)), Occur.MUST_NOT)
            };

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

            var parser = new QueryParser(MatchVersion, searchField, this.standardAnalyzer);
            var query  = ParseQuery(searchQuery, parser);

            var hits = filter.IsSet()
                ? searcher.Search(query, booleanFilter, hitsLimit).ScoreDocs
                : searcher.Search(query, hitsLimit).ScoreDocs;

            var results = MapSearchToDataList(searcher, hits, userAccessList);

            this.searcherManager.Release(searcher);

            return(results);
        }
コード例 #12
0
        /// <summary>
        ///     Builds the query.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        public virtual object BuildQuery(ISearchCriteria criteria)
        {
            var queryBuilder = new QueryBuilder();
            var queryFilter = new BooleanFilter();
            var query = new BooleanQuery();
            queryBuilder.Query = query;
            queryBuilder.Filter = queryFilter;

            if (criteria.CurrentFilters != null)
            {
                foreach (var filter in criteria.CurrentFilters)
                {
                    // Skip currencies that are not part of the filter
                    if (filter.GetType() == typeof(PriceRangeFilter)) // special filtering 
                    {
                        var currency = (filter as PriceRangeFilter).Currency;
                        if (!currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                    }

                    var filterQuery = LuceneQueryHelper.CreateQuery(criteria, filter, Occur.SHOULD);

                    // now add other values that should also be counted?

                    if (filterQuery != null)
                    {
                        var clause = new FilterClause(filterQuery, Occur.MUST);
                        queryFilter.Add(clause);
                    }
                }
            }

            return queryBuilder;
        }
コード例 #13
0
        /// <summary>
        /// Calculates the result count.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="baseDocIdSet">The base doc id set.</param>
        /// <param name="facetGroup">The facet group.</param>
        /// <param name="filter">The filter.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        private int CalculateResultCount(IndexReader reader, DocIdSet baseDocIdSet, FacetGroup facetGroup, ISearchFilter filter, ISearchCriteria criteria)
        {
            var count = 0;

            var values = LuceneQueryHelper.GetFilterValues(filter);

            if (values == null)
            {
                return 0;
            }

            BooleanFilter ffilter = null;
            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(facetGroup.FieldName))
                {
                    if (ffilter == null) ffilter = new BooleanFilter();

                    var q = LuceneQueryHelper.CreateQuery(criteria, f, Occur.SHOULD);
                    ffilter.Add(new FilterClause(q, Occur.MUST));
                }
            }

            foreach (var value in values)
            {
                var queryFilter = new BooleanFilter();
                    
                var valueFilter = LuceneQueryHelper.CreateQueryForValue(Results.SearchCriteria, filter, value);

                if (valueFilter == null) continue;

                queryFilter.Add(new FilterClause(valueFilter, Occur.MUST));
                if(ffilter!=null)
                    queryFilter.Add(new FilterClause(ffilter, Occur.MUST));

                var filterArray = queryFilter.GetDocIdSet(reader);
                var newCount = (int)CalculateFacetCount(baseDocIdSet, filterArray);
                if (newCount == 0) continue;

                var newFacet = new Facet(facetGroup, value.Id, GetDescription(value, Results.SearchCriteria.Locale), newCount);
                facetGroup.Facets.Add(newFacet);
                count += newCount;
            }

            return count;
        }
コード例 #14
0
        private void InitModel()
        {
            if (this.ItemType != null)
            {
                Model          item       = new Model();
                PropertyInfo[] infoArray2 = this.ItemType.GetProperties();
                GridFilters    filters    = new GridFilters();
                filters.MenuFilterText = "搜索";
                filters.ID             = "filters" + ID;
                for (int i = 0; i < infoArray2.Length; i++)
                {
                    Func <ColumnBase, bool> predicate = null;
                    PropertyInfo            property  = infoArray2[i];
                    ModelField field = new ModelField
                    {
                        Name = property.Name
                    };
                    #region  射字段类型
                    string fullName = property.PropertyType.FullName;
                    if (fullName.Contains("System.Int"))
                    {
                        var enumDataTypeAttr = AttributeHelper.GetAttribute <EnumDataTypeAttribute>(property);
                        if (enumDataTypeAttr == null)
                        {
                            field.Type = ModelFieldType.Int;
                        }
                    }
                    else if (fullName.Contains("System.DateTime"))
                    {
                        field.Type = ModelFieldType.Date;
                    }
                    else if (fullName.Contains("System.Single"))
                    {
                        field.Type = ModelFieldType.Float;
                    }
                    else if (fullName.Contains("System.Boolean"))
                    {
                        field.Type = ModelFieldType.Boolean;
                    }
                    else if (fullName.Contains("System.String"))
                    {
                        field.Type = ModelFieldType.String;
                    }
                    else if (!property.PropertyType.IsPrimitive)
                    {
                        field.Type = ModelFieldType.Object;
                    }
                    else
                    {
                        field.Type = ModelFieldType.Auto;
                    }
                    #endregion
                    item.Fields.Add(field);

                    if (predicate == null)
                    {
                        predicate = x => x.DataIndex == property.Name;
                    }
                    ColumnBase column = this.ColumnModel.Columns.FirstOrDefault <ColumnBase>(predicate);
                    if (column == null)
                    {
                        ColumnBase base2;
                        DataGridColumnAttribute displayAttr = AttributeHelper.GetAttribute <DataGridColumnAttribute>(property);
                        FilterAttribute         filterAttr  = AttributeHelper.GetAttribute <FilterAttribute>(property);
                        bool       simpleFilterable         = filterAttr != null && filterAttr.Enabled && filterAttr.FilterType == null;
                        GridFilter filter = null;
                        #region  射列类型
                        if (fullName.Contains("System.Int") ||
                            fullName.Contains("System.Single") ||
                            fullName.Contains("System.Decimal") ||
                            fullName.Contains("System.Double"))
                        {
                            NumberColumn column1 = new NumberColumn
                            {
                                Format = this.GetFormat(displayAttr, "0")
                            };
                            base2 = column1;
                            if (simpleFilterable && filterAttr.FilterType == null)
                            {
                                filter = new NumericFilter();
                            }
                        }
                        else if (fullName.Contains("System.DateTime"))
                        {
                            DateColumn column2 = new DateColumn
                            {
                                Format = this.GetFormat(displayAttr, "Y-m-d")
                            };
                            base2 = column2;
                            if (simpleFilterable)
                            {
                                filter = new DateFilter()
                                {
                                    AfterText  = "在这之后",
                                    BeforeText = "在这之前",
                                    OnText     = "在这天"
                                };
                            }
                        }
                        else if (fullName.Contains("System.Boolean"))
                        {
                            string[]      strArray = this.GetFormat(displayAttr, "是|否").Split(new char[] { '|' });
                            BooleanColumn column4  = new BooleanColumn
                            {
                                TrueText  = strArray[0],
                                FalseText = strArray[1]
                            };
                            base2 = column4;
                            if (simpleFilterable)
                            {
                                filter = new BooleanFilter()
                                {
                                    NoText  = strArray[1],
                                    YesText = strArray[0]
                                };
                            }
                        }
                        else if (fullName.Contains("System.String"))
                        {
                            base2 = new Column();
                            if (simpleFilterable)
                            {
                                filter = new StringFilter();
                            }
                        }
                        else if (!property.PropertyType.IsPrimitive)
                        {
                            base2 = new Column();
                            if (simpleFilterable)
                            {
                                filter = new StringFilter();
                            }
                        }
                        else
                        {
                            base2 = new Column();
                            if (simpleFilterable)
                            {
                                filter = new StringFilter();
                            }
                        }
                        #endregion
                        #region 生成外键过滤器
                        if (filterAttr != null && filterAttr.Enabled && filterAttr.FilterType != null)
                        {
                            Store store = new Store();
                            store.ID = "filterStore" + property.Name;
                            if (filterAttr.FilterType.IsEnum)
                            {
                                var valueType = filterAttr.FilterType;
                                var values    = Enum.GetValues(valueType);
                                var names     = Enum.GetNames(valueType);
                                List <KeyValueModel> enumDict = new List <KeyValueModel>();
                                for (int it = 0; it < values.Length; it++)
                                {
                                    var fieldInfo = valueType.GetField(names[it]);
                                    var descAttr  = AttributeHelper.GetAttribute <DescriptionAttribute>(fieldInfo);
                                    if (descAttr == null)
                                    {
                                        throw new Exception("枚举必须要有Description");
                                    }
                                    int key = (int)values.GetValue(it);
                                    enumDict.Add(new KeyValueModel()
                                    {
                                        Id   = key,
                                        Name = descAttr.Description
                                    });
                                }
                                Model model = new Model();
                                model.Fields.Add(new ModelField("Id", ModelFieldType.Int));
                                model.Fields.Add(new ModelField("Name", ModelFieldType.String));
                                store.Model.Add(model);
                                store.DataSource = enumDict;
                                store.DataBind();
                                filter = new ListFilter()
                                {
                                    StoreID    = store.ID,
                                    Single     = true,
                                    IDField    = "Id",
                                    LabelField = "Name"
                                };
                            }
                            else
                            {
                                var filterObject = Activator.CreateInstance(filterAttr.FilterType);
                                var filterGen    = filterObject as ForeignFilterBase;
                                if (filterGen == null)
                                {
                                    throw new ArgumentException("FilterAttribute中的FilterType的类型必须为ForeignFilterBase的子类或枚举类型");
                                }
                                store.Model.Add(filterGen.GetModel());
                                store.DataSource = filterGen.GetData();
                                store.DataBind();
                                filter = new ListFilter()
                                {
                                    StoreID    = store.ID,
                                    Single     = true,
                                    IDField    = filterGen.IdField,
                                    LabelField = filterGen.LabelField
                                };
                            }
                            Bin.Add(store);
                        }
                        #endregion
                        base2.Text      = (displayAttr == null) ? property.Name : displayAttr.DisplayName;
                        base2.DataIndex = property.Name;
                        if (displayAttr != null && displayAttr.Width != -1)
                        {
                            base2.Width = displayAttr.Width;
                        }
                        if (displayAttr != null && displayAttr.ValueType != null)
                        {
                            Type valueType = displayAttr.ValueType;
                            var  values    = Enum.GetValues(valueType);
                            var  names     = Enum.GetNames(valueType);
                            Dictionary <int, string> enumDict  = new Dictionary <int, string>();
                            List <string>            valueList = new List <string>();
                            for (int it = 0; it < values.Length; it++)
                            {
                                var fieldInfo = valueType.GetField(names[it]);
                                var descAttr  = AttributeHelper.GetAttribute <DescriptionAttribute>(fieldInfo);
                                int key       = (int)values.GetValue(it);
                                enumDict.Add(key, descAttr.Description);
                                valueList.Add("values[" + key + "]=\"" + (descAttr != null ? descAttr.Description : names[it]) + "\";");
                            }

                            base2.Renderer.Fn = "function(){var values=Array();" + string.Join(string.Empty, valueList) + "return values[arguments[0]];}";
                        }
                        if (filter != null)
                        {
                            filter.DataIndex = property.Name;
                            filters.Filters.Add(filter);
                        }
                        column = base2;
                        this.ColumnModel.Columns.Add(base2);
                    }
                    column.TabIndex = (short)i;
                }
                Features.Add(filters);
                if (this.ColumnModel.Columns.Count >= 10)
                {
                    this.AutoScroll = true;
                }
                var list = ColumnModel.Columns.OrderBy(x => x.TabIndex).ToList();
                ColumnModel.Columns.Clear();
                ColumnModel.Columns.AddRange(list);
                this._store.Model.Add(item);
            }
        }
コード例 #15
0
 public void SetUp()
 {
     _column = new ColumnBuilder().Build();
     _filter = new BooleanFilter(_column, false, false, false);
 }
コード例 #16
0
 public void SetFilter(string fieldName, BooleanFilter filterType)
 {
     SetFilter(fieldName, filterType, true, null);
 }
コード例 #17
0
        public virtual async Task <SearchResult> AutoCompleteAsync(string prefix, int languageId = -1, int maxResult = 10, SearchResultSortType orderBy = SearchResultSortType.LastUpDate)
        {
            var result = new SearchResult();

            if (string.IsNullOrWhiteSpace(prefix))
            {
                return(result);
            }

            prefix = prefix.Trim();

            //replace multiple spaces with a single space
            RegexOptions options = RegexOptions.None;
            Regex        regex   = new Regex("[ ]{2,}", options);

            prefix = regex.Replace(prefix, " ");

            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            try
            {
                await Task.Run(() =>
                {
                    using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                    {
                        using (var searcher = new IndexSearcher(directory, readOnly: true))
                        {
                            BooleanFilter filter = null;
                            if (languageId > -1)
                            {
                                filter = new BooleanFilter();
                                filter.Add(new FilterClause(
                                               new QueryWrapperFilter(new TermQuery(new Term("LanguageId",
                                                                                             languageId.ToString()))),
                                               Occur.MUST));
                            }

                            Sort sort = new Sort(SortField.FIELD_SCORE);

                            switch (orderBy)
                            {
                            case SearchResultSortType.NumberOfVisits:
                                sort = new Sort(new SortField("NumberOfVisit", SortField.INT, true));
                                break;

                            case SearchResultSortType.PublishDate:
                                sort = new Sort(new SortField("PublishDate", SortField.LONG, true));
                                break;

                            case SearchResultSortType.LastUpDate:
                                sort = new Sort(new SortField("LastUpDate", SortField.LONG, true));
                                break;
                            }

                            var analyzer = new StandardAnalyzer(Version);
                            var parser   = new QueryParser(Version, "Title", analyzer);
                            var query    = ParseQuery(prefix.Replace(" ", "*") + "*", parser);
                            var hits     = searcher.Search(query, filter, maxResult, sort).ScoreDocs;

                            foreach (var scoreDoc in hits)
                            {
                                var doc = searcher.Doc(scoreDoc.Doc);
                                result.Documents.Add(new SearchResultDocument()
                                {
                                    DocumentId       = int.Parse(doc.Get("ID")),
                                    LanguageId       = int.Parse(doc.Get("LanguageId")),
                                    LanguageIsoCode  = doc.Get("LanguageCode"),
                                    Score            = scoreDoc.Score,
                                    DocumentTitle    = doc.Get("Title"),
                                    DocumentBody     = doc.Get("Description"),
                                    DocumentKeywords = doc.Get("Keywords"),
                                    DocumentTags     = doc.Get("Tags"),
                                });
                            }

                            result.Documents = result.Documents.DistinctBy(p => new { p.DocumentId })
                                               .ToList();
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                result.Error    = ex;
                result.HasError = true;
            }

            watch.Stop();
            result.ElapsedMilliseconds = watch.ElapsedMilliseconds;

            return(result);
        }
コード例 #18
0
 public BooleanFilterViewModel(ICommandBus commandBus, BooleanFilter filter)
     : base(commandBus, filter)
 {
     _filter = filter;
 }
コード例 #19
0
 /// <summary>
 /// Creates the query.
 /// </summary>
 /// <param name="field">The field.</param>
 /// <param name="value">The value.</param>
 /// <returns></returns>
 public static Filter CreateQuery(string field, CategoryFilterValue value)
 {
     var query = new BooleanFilter();
     if (!String.IsNullOrEmpty(value.Outline))
     {
         // workaround since there is no wildcard filter in current lucene version
         var outline = value.Outline.TrimEnd('*');
         var nodeQuery = new PrefixFilter(new Term(field, outline.ToLower()));
         query.Add(new FilterClause(nodeQuery, Occur.MUST));
     }
     return query;
 }
コード例 #20
0
ファイル: JobAdSearchService.cs プロジェクト: formist/LinkMe
        JobAdSearchResults IJobAdSearchService.SearchSuggested(Guid?memberId, JobAdSearchQuery searchQuery)
        {
            const string method = "GetSuggestedJobs";

            try
            {
                if (memberId == null)
                {
                    return(new JobAdSearchResults());
                }

                var reader = GetReader();

                var searcher = new Searcher(reader);

                var member    = _membersQuery.GetMember(memberId.Value);
                var candidate = _candidatesQuery.GetCandidate(memberId.Value);
                if (member == null || candidate == null || candidate.ResumeId == null)
                {
                    return(new JobAdSearchResults());
                }

                var resume = _resumesQuery.GetResume(candidate.ResumeId.Value);

                //Get a MLT query based on the candidate's details
                var mlt = new MoreLikeThis(reader, CreateSimilarity());
                mlt.setAnalyzer(_contentAnalyzer);
                mlt.setMaxNumTokensParsed(10000); //increase for long resumes
                mlt.setFieldNames(new[] { FieldName.Content });
                mlt.setMinWordLen(3);             //exclude UK, BBC and the like
                mlt.setMaxQueryTerms(20);
                //mlt.setBoost(true);
                var candidateMltQuery = GetCandidateMltQuery(mlt, candidate, resume, method);

                mlt.setFieldNames(new[] { FieldName.Title });
                var candidateTitleMltQuery = GetCandidateMltQuery(mlt, candidate, resume, method);

                #region Log

                if (EventSource.IsEnabled(Event.Trace))
                {
                    EventSource.Raise(Event.Trace, method, "Candidate MLT Query",
                                      Event.Arg("Query",
                                                candidateMltQuery == null ? "null" : candidateMltQuery.toString()));
                    EventSource.Raise(Event.Trace, method, "Candidate Title MLT Query",
                                      Event.Arg("Query",
                                                candidateTitleMltQuery == null
                                                    ? "null"
                                                    : candidateTitleMltQuery.toString()));
                }

                #endregion

                var mltQueries   = new BooleanQuery(); // Up to 3 MLT queries ORed together
                var titleQueries = new BooleanQuery();
                // (Optionally) Desired Job Title and up to 3 job titles ORed together
                var combinedFilter = new BooleanFilter();
                // Filtering including exclusion of past applications and salary, location, etc.
                var combinedQuery = new BooleanQuery(); // mltQueries and titleQueries ANDed together

                if (candidateMltQuery != null)
                {
                    mltQueries.add(candidateMltQuery, BooleanClause.Occur.SHOULD);
                }

                if (candidateTitleMltQuery != null)
                {
                    mltQueries.add(candidateTitleMltQuery, BooleanClause.Occur.SHOULD);
                }

                //Get a MLT query based on the candidate's past 3 months of applications

                var jobAdIds = (from a in _memberApplicationsQuery.GetApplications(member.Id)
                                where a.CreatedTime >= DateTime.Now.AddMonths(-3)
                                select a.PositionId).ToList();

                if (jobAdIds.Any())
                {
                    mlt.setFieldNames(new[] { FieldName.Content });
                    var applicationsMltQuery = GetApplicationsMltQuery(mlt, jobAdIds);

                    if (applicationsMltQuery != null)
                    {
                        mltQueries.add(applicationsMltQuery, BooleanClause.Occur.SHOULD);
                    }

                    #region Log

                    if (EventSource.IsEnabled(Event.Trace))
                    {
                        EventSource.Raise(Event.Trace, method, "Applications MLT Query",
                                          Event.Arg("Query",
                                                    applicationsMltQuery == null
                                                        ? "null"
                                                        : applicationsMltQuery.toString()));
                    }

                    #endregion

                    // ensure the jobs that have already been applied for are not in the results
                    var idFilter = new SpecialsFilter(SearchFieldName.Id, false,
                                                      jobAdIds.Select(id => id.ToFieldValue()));
                    combinedFilter.add(new FilterClause(idFilter, BooleanClause.Occur.MUST_NOT));

                    #region Log

                    if (EventSource.IsEnabled(Event.Trace))
                    {
                        EventSource.Raise(Event.Trace, method, "Combined Filter #1",
                                          Event.Arg("Query", combinedFilter.toString()));
                    }

                    #endregion
                }

                if (mltQueries.getClauses().Any())
                {
                    combinedQuery.add(new BooleanClause(mltQueries, BooleanClause.Occur.SHOULD));
                }

                // now add in additional weighting data from the candidate's profile
                if (!string.IsNullOrEmpty(candidate.DesiredJobTitle))
                {
                    //Boost the DesiredJobTitle above the rest of the terms
                    var jobTitleExpression =
                        Expression.Parse(Expression.StripOperatorsAndBrackets(candidate.DesiredJobTitle),
                                         ModificationFlags.AllowShingling);

                    // First match on title
                    var boostQuery =
                        _indexer.GetQuery(new JobAdSearchQuery
                    {
                        AdTitle = jobTitleExpression, IncludeSynonyms = true
                    });
                    boostQuery.setBoost(1.5F);
                    titleQueries.add(boostQuery, BooleanClause.Occur.SHOULD);

                    // Also match general content
                    //boostQuery = _indexer.GetLuceneQuery(new JobAdSearchQuery {Keywords = jobTitleExpression, IncludeSynonyms = true});
                    //boostQuery.setBoost(1.3F);
                    //titleQueries.add(boostQuery, BooleanClause.Occur.SHOULD);

                    #region Log

                    if (EventSource.IsEnabled(Event.Trace))
                    {
                        EventSource.Raise(Event.Trace, method, "Combined Query #1",
                                          Event.Arg("Query", combinedQuery.toString()));
                    }

                    #endregion
                }

                if (resume.Jobs != null)
                {
                    foreach (var jobTitle in resume.Jobs.Select(j => j.Title).Take(3))
                    {
                        if (string.IsNullOrEmpty(jobTitle))
                        {
                            continue;
                        }

                        var jobTitleExpression = Expression.Parse(Expression.StripOperatorsAndBrackets(jobTitle),
                                                                  ModificationFlags.AllowShingling);

                        // First match on title
                        var boostQuery =
                            _indexer.GetQuery(new JobAdSearchQuery
                        {
                            AdTitle = jobTitleExpression, IncludeSynonyms = true
                        });
                        boostQuery.setBoost(1.2F);
                        titleQueries.add(boostQuery, BooleanClause.Occur.SHOULD);

                        // Also match general content
                        //boostQuery = _indexer.GetLuceneQuery(new JobAdSearchQuery { Keywords = jobTitleExpression, IncludeSynonyms = true });
                        //boostQuery.setBoost(1.1F);
                        //titleQueries.add(boostQuery, BooleanClause.Occur.SHOULD);

                        // Also match general content
                        //boostQuery = _indexer.GetLuceneQuery(new JobAdSearchQuery { Keywords = jobTitleExpression, IncludeSynonyms = true });
                        //boostQuery.setBoost(1.1F);
                        //combinedQuery.add(boostQuery, BooleanClause.Occur.SHOULD);
                    }
                }

                // now combine the queries

                if (mltQueries.getClauses().Any())
                {
                    combinedQuery.add(new BooleanClause(mltQueries, BooleanClause.Occur.SHOULD));
                }

                if (titleQueries.getClauses().Any())
                {
                    combinedQuery.add(new BooleanClause(titleQueries, BooleanClause.Occur.MUST));
                }

                #region Log

                if (EventSource.IsEnabled(Event.Trace))
                {
                    EventSource.Raise(Event.Trace, method, "Combined Query #2",
                                      Event.Arg("Query", combinedQuery.toString()));
                }

                #endregion

                searchQuery.Salary = candidate.DesiredSalary == null
                    ? null
                    : candidate.DesiredSalary.Clone();
                searchQuery.ExcludeNoSalary         = false;
                searchQuery.Location                = member.Address.Location;
                searchQuery.Distance                = 50;
                searchQuery.JobTypes                = candidate.DesiredJobTypes;
                searchQuery.Relocations             = candidate.RelocationPreference != RelocationPreference.No && candidate.RelocationLocations != null
                    ? candidate.RelocationLocations = candidate.RelocationLocations.ToArray()
                    : null;

                //Add salary, job type, location restriction & (optionally) last run time
                var filter = _indexer.GetFilter(searchQuery, null, null);

                // combine salary, etc. filter with the excluded job ads
                combinedFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));

                // exclude blocked jobs
                var excludedIds = _jobAdActivityFiltersQuery.GetExcludeJobAdIds(member, searchQuery);
                if (excludedIds != null && excludedIds.Count > 0)
                {
                    combinedFilter.add(new FilterClause(new SpecialsFilter(SearchFieldName.Id, true, excludedIds.Select(id => id.ToFieldValue())), BooleanClause.Occur.MUST));
                }


                #region Log

                if (EventSource.IsEnabled(Event.Trace))
                {
                    EventSource.Raise(Event.Trace, method, "Combined Filter #2",
                                      Event.Arg("Query", combinedFilter.toString()));
                }

                #endregion

                var searchResults = searcher.Search(combinedQuery, combinedFilter, null, null, searchQuery.Skip, searchQuery.Take ?? reader.maxDoc(), false);

                #region Log

                if (EventSource.IsEnabled(Event.Trace))
                {
                    EventSource.Raise(Event.Trace, method, "MLT Results",
                                      Event.Arg("Results Count", searchResults == null ? 0 : searchResults.JobAdIds.Count));
                }

                #endregion

                return(searchResults);
            }
            catch (Exception e)
            {
                #region Log
                EventSource.Raise(Event.Error, method, "Unexpected exception.", e);
                #endregion
                throw;
            }
        }
コード例 #21
0
        /// <summary>
        ///     Creates the price range query.
        /// </summary>
        /// <param name="priceLists">The price lists.</param>
        /// <param name="index">The index.</param>
        /// <param name="field">The field.</param>
        /// <param name="currency">The currency.</param>
        /// <param name="lowerbound">The lowerbound.</param>
        /// <param name="upperbound">The upperbound.</param>
        /// <param name="lowerboundincluded">
        ///     if set to <c>true</c> [lowerboundincluded].
        /// </param>
        /// <param name="upperboundincluded">
        ///     if set to <c>true</c> [upperboundincluded].
        /// </param>
        /// <returns></returns>
        private static BooleanFilter CreatePriceRangeQuery(
            string[] priceLists,
            int index,
            string field,
            string currency,
            string lowerbound,
            string upperbound,
            bool lowerboundincluded,
            bool upperboundincluded)
        {
            var query = new BooleanFilter();

            // create left part
            var filter =
                new TermRangeFilter(
                    String.Format("{0}_{1}_{2}", field, currency, priceLists[index - 1].ToLower()),
                    "*",
                    "*",
                    true,
                    false);
            var leftClause = new FilterClause(filter, Occur.MUST_NOT);
            query.Add(leftClause);

            // create right part
            if (index == priceLists.Count()) // last element
            {
                //var rangefilter = NumericRangeFilter.;
                var filter2 =
                    new TermRangeFilter(
                        String.Format("{0}_{1}_{2}", field, currency, priceLists[index - 1].ToLower()),
                        lowerbound,
                        upperbound,
                        lowerboundincluded,
                        upperboundincluded);
                var rightClause = new FilterClause(filter2, Occur.MUST);
                query.Add(rightClause);
            }
            else
            {
                query.Add(new FilterClause(
                    CreatePriceRangeQuery(
                        priceLists,
                        index + 1,
                        field,
                        currency,
                        lowerbound,
                        upperbound,
                        lowerboundincluded,
                        upperboundincluded),
                    Occur.SHOULD));
            }

            return query;
        }
コード例 #22
0
ファイル: JobAdSearchService.cs プロジェクト: formist/LinkMe
        JobAdSearchResults IJobAdSearchService.SearchSimilar(Guid?memberId, Guid jobAdId, JobAdSearchQuery searchQuery)
        {
            const string method = "GetSimilarJobs";

            try
            {
                var reader   = GetReader();
                var searcher = new Searcher(reader);

                var docId = searcher.Fetch(jobAdId);

                // If the job ad cannot be found then return no results.

                if (docId == -1)
                {
                    return(new JobAdSearchResults());
                }

                var jobAd = _jobAdsQuery.GetJobAd <JobAd>(jobAdId);
                if (jobAd == null)
                {
                    return(new JobAdSearchResults());
                }

                // Look for more like this.

                var mlt = new MoreLikeThis(reader);
                mlt.setAnalyzer(_contentAnalyzer);
                mlt.setFieldNames(new [] { FieldName.Content, FieldName.Title });
                var query = mlt.like(docId);

                //query = new SeniorityIndexHandler().GetQuery(query, new JobAdSearchQuery {SeniorityIndex = jobAd.SeniorityIndex});

                // Ensure the initial job is not in the results.

                var searchFilter = new BooleanFilter();
                searchFilter.add(new FilterClause(new SpecialsFilter(SearchFieldName.Id, false, new[] { jobAdId.ToFieldValue() }), BooleanClause.Occur.MUST_NOT));

                // Add salary and location restriction.

                var filter = _indexer.GetFilter(
                    new JobAdSearchQuery
                {
                    Salary          = FudgeSalary(jobAd.Description.Salary),
                    ExcludeNoSalary = true,
                    Location        = jobAd.Description.Location,
                    Distance        = 50,
                },
                    null,
                    null);

                searchFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));

                return(searcher.Search(query, searchFilter, null, null, searchQuery.Skip, searchQuery.Take ?? reader.maxDoc(), false));
            }
            catch (Exception e)
            {
                #region Log
                EventSource.Raise(Event.Error, method, "Unexpected exception.", e);
                #endregion
                throw;
            }
        }
コード例 #23
0
        /// <summary>
        ///     Creates the query.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static Filter CreateQuery(ISearchCriteria criteria, string field, RangeFilterValue value)
        {
            var query = new BooleanFilter();

            object lowerbound = value.Lower;
            object upperbound = value.Upper;

            const bool lowerboundincluded = true;
            const bool upperboundincluded = false;

            var currency = criteria.Currency.ToLower();

            var upper = upperbound == null ? NumericUtils.LongToPrefixCoded(long.MaxValue) : ConvertToSearchable(upperbound);

            // format is "fieldname_store_currency_pricelist"
            string[] pls = null;
            var searchCriteria = criteria as CatalogItemSearchCriteria;
            if (searchCriteria != null)
            {
                pls = searchCriteria.Pricelists;
            }

            var parentPriceList = String.Empty;

            // Create  filter of type 
            // price_USD_pricelist1:[100 TO 200} (-price_USD_pricelist1:[* TO *} +(price_USD_pricelist2:[100 TO 200} (-price_USD_pricelist2:[* TO *} (+price_USD_pricelist3[100 TO 200}))))

            if (pls == null || !pls.Any())
            {
                return null;
            }

            var priceListId = pls[0].ToLower();

            var filter = new TermRangeFilter(
                String.Format("{0}_{1}_{2}", field, currency, priceListId),
                ConvertToSearchable(lowerbound),
                upper,
                lowerboundincluded,
                upperboundincluded);

            query.Add(new FilterClause(filter, Occur.SHOULD));

            if (pls.Count() > 1)
            {
                var q = CreatePriceRangeQuery(
                    pls,
                    1,
                    field,
                    currency,
                    ConvertToSearchable(lowerbound),
                    upper,
                    lowerboundincluded,
                    upperboundincluded);
                query.Add(new FilterClause(q, Occur.SHOULD));
            }

            return query;
        }
コード例 #24
0
ファイル: Search.cs プロジェクト: voquanghoa/YAFNET
        /// <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();

            if (searcher == null)
            {
                totalHits = 0;
                return(new List <SearchMessage>());
            }

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

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

            var analyzer = new StandardAnalyzer(MatchVersion);

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

            // search by single field
            if (searchField.IsSet())
            {
                var parser = new QueryParser(MatchVersion, 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.Dispose();

                return(results);
            }
            else
            {
                var parser = new MultiFieldQueryParser(
                    MatchVersion,
                    new[]
                {
                    "Message", "Topic",
                    this.Get <BoardSettings>().EnableDisplayName ? "AuthorDisplay" : "Author", "TopicTags"
                },
                    analyzer);

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

                // sort by date
                var sort = new Sort(new SortField("MessageId", SortFieldType.STRING, true));

                var fil = new BooleanFilter();

                // search this forum
                if (forumId > 0)
                {
                    fil.Add(new FilterClause(new TermsFilter(new Term("ForumId", forumId.ToString())), Occur.SHOULD));
                }
                else
                {
                    // filter user access
                    if (userAccessList.Any())
                    {
                        userAccessList.Where(a => !a.ReadAccess).ForEach(
                            access => fil.Add(
                                new FilterClause(
                                    new TermsFilter(new Term("ForumId", access.ForumID.ToString())),
                                    Occur.MUST_NOT)));
                    }
                }

                var hits = searcher.Search(query, fil.Any() ? fil : 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);

                this.searcherManager.Release(searcher);

                return(results);
            }
        }
コード例 #25
0
        public async Task <AdminSearchResult> SearchContent(AdminSearchContext searchContext)
        {
            var searchResult = new AdminSearchResult();

            await _luceneIndexProvider.SearchAsync(searchContext.Index, async searcher =>
            {
                Query query;
                if (string.IsNullOrWhiteSpace(searchContext.SearchTerm))
                {
                    query = new MatchAllDocsQuery();
                }
                else
                {
                    var luceneVersion = LuceneSettings.DefaultVersion;
                    var analyzer      = new StandardAnalyzer(luceneVersion);

                    var multiFieldQuery = new MultiFieldQueryParser(luceneVersion, searchContext.IndexFieldsToSearch, analyzer);
                    query = multiFieldQuery.Parse(QueryParserBase.Escape(searchContext.SearchTerm));
                }

                searchContext.PageNumber -= 1;

                var start = searchContext.PageNumber * searchContext.PageSize;
                var end   = searchContext.PageNumber * searchContext.PageSize + searchContext.PageSize;

                var collector = TopScoreDocCollector.Create(end, true);
                var filter    = new BooleanFilter();

                if (searchContext.ContentTypes.Any())
                {
                    filter.Add(new FieldCacheTermsFilter("Content.ContentItem.ContentType", searchContext.ContentTypes), Occur.MUST);
                }

                if (searchContext.Filters != null && searchContext.Filters.Any())
                {
                    await _searchFilters.InvokeAsync(x => x.Filter(searchContext, filter), null);
                }

                if (filter.Any())
                {
                    searcher.Search(query, filter, collector);
                }
                else
                {
                    searcher.Search(query, collector);
                }

                var docs = collector.GetTopDocs(start, end);
                searchResult.TotalRecordCount = docs.TotalHits;

                var contentItemIds = docs.ScoreDocs.Select(hit =>
                {
                    var doc = searcher.Doc(hit.Doc);
                    return(doc.GetField("ContentItemId").GetStringValue());
                });

                searchResult.ContentItems = await _contentManager.GetAsync(contentItemIds);
            });

            return(searchResult);
        }
コード例 #26
0
        public virtual async Task <SearchResult> SearchAsync(string term,
                                                             int?filterByCategory         = null,
                                                             int languageId               = -1,
                                                             PostType?postType            = null,
                                                             SearchPlace searchPlace      = SearchPlace.Anywhere,
                                                             SearchResultSortType orderBy = SearchResultSortType.Score,
                                                             int maxResult    = 1000,
                                                             bool exactSearch = false)
        {
            var result = new SearchResult();

            term = term.Trim();

            //replace multiple spaces with a single space
            RegexOptions options = RegexOptions.None;
            Regex        regex   = new Regex("[ ]{2,}", options);

            term = regex.Replace(term, " ");

            if (string.IsNullOrWhiteSpace(term))
            {
                return(result);
            }

            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            try
            {
                await Task.Run(() =>
                {
                    using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                    {
                        using (var searcher = new IndexSearcher(directory, readOnly: true))
                        {
                            var searchInFields = new List <string>();
                            if (searchPlace == SearchPlace.Anywhere)
                            {
                                searchInFields.AddRange(new string[] { "Title", "Description", "Keywords", "Tags" });
                            }
                            else
                            {
                                if (searchPlace.HasFlagFast(SearchPlace.Title))
                                {
                                    searchInFields.Add("Title");
                                }

                                if (searchPlace.HasFlagFast(SearchPlace.Description))
                                {
                                    searchInFields.Add("Description");
                                }

                                if (searchPlace.HasFlagFast(SearchPlace.Keywords))
                                {
                                    searchInFields.Add("Keywords");
                                }

                                if (searchPlace.HasFlagFast(SearchPlace.Tags))
                                {
                                    searchInFields.Add("Tags");
                                }
                            }

                            BooleanFilter filter = null;
                            if (languageId > -1 || filterByCategory != null || postType != null)
                            {
                                filter = new BooleanFilter();
                                if (languageId > -1)
                                {
                                    filter.Add(new FilterClause(
                                                   new QueryWrapperFilter(new TermQuery(new Term("LanguageId", languageId.ToString()))),
                                                   Occur.MUST));
                                }
                                if (filterByCategory != null)
                                {
                                    filter.Add(new FilterClause(
                                                   new QueryWrapperFilter(new TermQuery(new Term("Categories",
                                                                                                 filterByCategory.Value.ToString()))), Occur.MUST));
                                }
                                if (postType != null)
                                {
                                    filter.Add(new FilterClause(
                                                   new QueryWrapperFilter(new TermQuery(new Term("PostType",
                                                                                                 postType.Value.ToString()))), Occur.MUST));
                                }
                            }

                            var currentSettings = _settingService.LoadSetting <SiteSettings>();
                            if (!currentSettings.EnableBlog)
                            {
                                //Filter Blog Posts if Blog is disabled
                                if (filter == null)
                                {
                                    filter = new BooleanFilter();
                                }
                                filter.Add(new FilterClause(
                                               new QueryWrapperFilter(new TermQuery(new Term("PostType",
                                                                                             PostType.BlogPost.ToString()))), Occur.MUST_NOT));
                            }

                            Sort sort = new Sort(SortField.FIELD_SCORE);

                            switch (orderBy)
                            {
                            case SearchResultSortType.NumberOfVisits:
                                sort = new Sort(new SortField("NumberOfVisit", SortField.INT, true));
                                break;

                            case SearchResultSortType.PublishDate:
                                sort = new Sort(new SortField("PublishDate", SortField.LONG, true));
                                break;

                            case SearchResultSortType.LastUpDate:
                                sort = new Sort(new SortField("LastUpDate", SortField.LONG, true));
                                break;
                            }

                            var analyzer       = new StandardAnalyzer(Version);
                            var parser         = new MultiFieldQueryParser(Version, searchInFields.ToArray(), analyzer);
                            QueryScorer scorer = null;
                            var hits           = new List <ScoreDoc>();
                            Query query        = null;
                            if (exactSearch)
                            {
                                query = ParseQuery(term, parser);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                            }
                            else
                            {
                                query = ParseQuery($"(\"{term}\")", parser);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                                query = ParseQuery($"({term.Replace(" ", "*")})", parser);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                                query = ParseQuery($"(+{term.Trim().Replace(" ", " +")})", parser);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                                query = ParseQuery(term, parser);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                            }

                            scorer = new QueryScorer(query);

                            if (hits.Count == 0)
                            {
                                term   = SearchByPartialWords(term);
                                query  = ParseQuery(term, parser);
                                scorer = new QueryScorer(query);
                                hits.AddRange(searcher.Search(query, filter, maxResult, sort).ScoreDocs);
                            }

                            var formatter = new SimpleHTMLFormatter(
                                "<span class='badge badge-warning'>",
                                "</span>");
                            var fragmenter  = new SimpleFragmenter(300);
                            var highlighter = new Highlighter(formatter, scorer)
                            {
                                TextFragmenter = fragmenter
                            };

                            foreach (var scoreDoc in hits)
                            {
                                var doc = searcher.Doc(scoreDoc.Doc);
                                result.Documents.Add(new SearchResultDocument()
                                {
                                    DocumentId       = int.Parse(doc.Get("ID")),
                                    LanguageId       = int.Parse(doc.Get("LanguageId")),
                                    LanguageIsoCode  = doc.Get("LanguageCode"),
                                    Score            = scoreDoc.Score,
                                    DocumentTitle    = GetHighlight("Title", highlighter, analyzer, doc.Get("Title"), false),
                                    DocumentBody     = GetHighlight("Description", highlighter, analyzer, doc.Get("Description"), true),
                                    DocumentKeywords = doc.Get("Keywords"),
                                    DocumentTags     = doc.Get("Tags"),
                                });
                            }

                            result.Documents = result.Documents.DistinctBy(p => new { p.DocumentId })
                                               .ToList();

                            analyzer.Close();

                            //SuggestSimilar
                            using (var spellDirectory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath)))
                            {
                                using (var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellDirectory))
                                {
                                    result.SuggestSimilar.AddRange(spellChecker.SuggestSimilar(term, 10, null, null, true));
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                result.Error    = ex;
                result.HasError = true;
            }

            watch.Stop();
            result.ElapsedMilliseconds = watch.ElapsedMilliseconds;

            _eventPublisher.Publish(new SearchEvent(term, filterByCategory, languageId, postType, searchPlace, maxResult, result));

            return(result);
        }
コード例 #27
0
        public void TestEmpty()
        {
            BooleanFilter booleanFilter = new BooleanFilter();

            TstFilterCard(@"empty BooleanFilter returns no results", 0, booleanFilter);
        }
コード例 #28
0
        /// <summary>
        /// Given a latitude and longitude (in degrees) and the
        /// maximum great circle (surface of the earth) distance,
        /// returns a simple Filter bounding box to "fast match"
        /// candidates.
        /// </summary>
        public static Filter GetBoundingBoxFilter(double originLat, double originLng, double maxDistanceKM)
        {
            // Basic bounding box geo math from
            // http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates,
            // licensed under creative commons 3.0:
            // http://creativecommons.org/licenses/by/3.0

            // TODO: maybe switch to recursive prefix tree instead
            // (in lucene/spatial)?  It should be more efficient
            // since it's a 2D trie...

            // Degrees -> Radians:
            double originLatRadians = originLat.ToRadians();
            double originLngRadians = originLng.ToRadians();

            double angle = maxDistanceKM / (SloppyMath.EarthDiameter(originLat) / 2.0);

            double minLat = originLatRadians - angle;
            double maxLat = originLatRadians + angle;

            double minLng;
            double maxLng;

            if (minLat > -90.ToRadians() && maxLat < 90.ToRadians())
            {
                double delta = Math.Asin(Math.Sin(angle) / Math.Cos(originLatRadians));
                minLng = originLngRadians - delta;
                if (minLng < -180.ToRadians())
                {
                    minLng += 2 * Math.PI;
                }
                maxLng = originLngRadians + delta;
                if (maxLng > 180.ToRadians())
                {
                    maxLng -= 2 * Math.PI;
                }
            }
            else
            {
                // The query includes a pole!
                minLat = Math.Max(minLat, -90.ToRadians());
                maxLat = Math.Min(maxLat, 90.ToRadians());
                minLng = -180.ToRadians();
                maxLng = 180.ToRadians();
            }

            BooleanFilter f = new BooleanFilter();

            // Add latitude range filter:
            f.Add(NumericRangeFilter.NewDoubleRange("latitude", minLat.ToDegrees(), maxLat.ToDegrees(), true, true),
                  Occur.MUST);

            // Add longitude range filter:
            if (minLng > maxLng)
            {
                // The bounding box crosses the international date
                // line:
                BooleanFilter lonF = new BooleanFilter();
                lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), null, true, true),
                         Occur.SHOULD);
                lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", null, maxLng.ToDegrees(), true, true),
                         Occur.SHOULD);
                f.Add(lonF, Occur.MUST);
            }
            else
            {
                f.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), maxLng.ToDegrees(), true, true),
                      Occur.MUST);
            }

            return(f);
        }
コード例 #29
0
    private void InitComponent(GridPanelInfo panel, List <GridPanelColumnInfo> columnList)
    {
        if (panel.DisplayRowNumber)
        {
            RowNumbererColumn stt = new RowNumbererColumn();
            stt.Header = "STT";
            GridPanel1.ColumnModel.Columns.Add(stt);
        }
        if (panel.Header == false)
        {
            GridPanel1.Title = "";
        }
        else
        {
            GridPanel1.Title = panel.Title;
        }
        if (!string.IsNullOrEmpty(panel.AutoExpandColumn))
        {
            columnList = GridController.GetInstance().GetColumnInfo(this.ID, TableName, 1);
            if (columnList.Count() != 0)
            {
                if (columnList.Where(p => p.ColumnName == panel.AutoExpandColumn).FirstOrDefault() != null)
                {
                    GridPanel1.AutoExpandColumn = panel.AutoExpandColumn;
                }
            }
            else
            {
                datatable = DataController.DataHandler.GetInstance().ExecuteDataTable("select top 1 * from " + TableName);
                foreach (DataColumn item in datatable.Columns)
                {
                    if (item.ColumnName == panel.AutoExpandColumn)
                    {
                        GridPanel1.AutoExpandColumn = panel.AutoExpandColumn;
                        break;
                    }
                }
            }
        }
        this.PrimaryKey = Util.GetInstance().GetPrimaryKeyOfTable(panel.TableName);
        // Store1.AutoLoadParams.Add(new Ext.Net.Parameter("limit", panel.PageSize.ToString()));

        //sHdfgridName.Value = this.ID.ToString();
        //sHdftable.Value = this.TableName;
        //sHdfPrimaryKey.Value = panel.PrimaryColumn;
        //sHdfwhere.Value = panel.WhereClause.Replace("where", "");

        //if (this.Height != 0)
        //    GridPanel1.Height = this.Height;
        //else
        //    GridPanel1.Height = panel.Height.Value;
        //if (this.Width != 0)
        //    GridPanel1.Width = this.Width;
        //else
        //    GridPanel1.Width = panel.Width.Value;

        GenerateListener();
        //if (panel.PageSize.HasValue)
        //    PagingToolbar1.PageSize = panel.PageSize.Value;
        //else
        //    PagingToolbar1.PageSize = 5;

        if (!string.IsNullOrEmpty(panel.Icon))
        {
            GridPanel1.IconCls = panel.Icon;
        }
        else
        {
            GridPanel1.Icon = Icon.Table;
        }
        //set local filter
        if (panel.AllowFilter)
        {
            GridFilters filter = new GridFilters();
            filter.Local       = true;
            filter.FiltersText = "Lọc";
            GridPanel1.Plugins.Add(filter);
            datatable = DataController.DataHandler.GetInstance().ExecuteDataTable("select top 1 * from " + TableName);
            foreach (DataColumn item in datatable.Columns)
            {
                switch (item.DataType.ToString())
                {
                case "System.Int32":
                    NumericFilter numberic = new NumericFilter();
                    numberic.DataIndex = item.ColumnName;
                    filter.Filters.Add(numberic);
                    break;

                case "System.Boolean":
                    BooleanFilter boolean = new BooleanFilter();
                    boolean.DataIndex = item.ColumnName;
                    filter.Filters.Add(boolean);
                    break;

                case "System.String":
                    StringFilter str = new StringFilter();
                    str.DataIndex = item.ColumnName;
                    filter.Filters.Add(str);
                    break;

                case "System.DateTime":
                    DateFilter datefilter = new DateFilter();
                    datefilter.BeforeText = "Trước ngày";
                    datefilter.AfterText  = "Sau ngày";
                    datefilter.OnText     = "Vào ngày";
                    datefilter.DataIndex  = item.ColumnName;
                    filter.Filters.Add(datefilter);
                    break;
                }
            }
        }
    }
コード例 #30
0
        public virtual SearchResult MoreLikeThis(int postId, int?filterByCategory = null, int languageId = -1, PostType?postType = null, SearchPlace searchPlace = SearchPlace.Title | SearchPlace.Description,
                                                 int maxResult = 5, SearchResultSortType orderBy         = SearchResultSortType.Score)
        {
            var result = new SearchResult();

            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            try
            {
                using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                {
                    using (var searcher = new IndexSearcher(directory, readOnly: true))
                    {
                        var docNumber = GetLuceneDocNumber(postId, searcher);

                        if (docNumber == -1)
                        {
                            return(result);
                        }

                        var searchInFields = new List <string>();
                        if (searchPlace == SearchPlace.Anywhere)
                        {
                            searchInFields.AddRange(new string[] { "Title", "Description", "Keywords", "Tags" });
                        }
                        else
                        {
                            if (searchPlace.HasFlagFast(SearchPlace.Title))
                            {
                                searchInFields.Add("Title");
                            }

                            if (searchPlace.HasFlagFast(SearchPlace.Description))
                            {
                                searchInFields.Add("Description");
                            }

                            if (searchPlace.HasFlagFast(SearchPlace.Keywords))
                            {
                                searchInFields.Add("Keywords");
                            }

                            if (searchPlace.HasFlagFast(SearchPlace.Tags))
                            {
                                searchInFields.Add("Tags");
                            }
                        }

                        var analyzer     = new StandardAnalyzer(Version);
                        var moreLikeThis = new MoreLikeThis(searcher.IndexReader)
                        {
                            Analyzer = analyzer
                        };
                        moreLikeThis.SetFieldNames(searchInFields.ToArray());
                        moreLikeThis.MinDocFreq  = 1;
                        moreLikeThis.MinTermFreq = 1;
                        moreLikeThis.Boost       = true;

                        var query = moreLikeThis.Like(docNumber);

                        var filter = new BooleanFilter();

                        filter.Add(new FilterClause(
                                       new QueryWrapperFilter(new TermQuery(new Term("ID",
                                                                                     postId.ToString()))),
                                       Occur.MUST_NOT));

                        if (languageId > -1)
                        {
                            filter.Add(new FilterClause(
                                           new QueryWrapperFilter(new TermQuery(new Term("LanguageId",
                                                                                         languageId.ToString()))),
                                           Occur.MUST));
                        }
                        if (filterByCategory != null)
                        {
                            filter.Add(new FilterClause(
                                           new QueryWrapperFilter(new TermQuery(new Term("Categories",
                                                                                         filterByCategory.Value.ToString()))), Occur.MUST));
                        }
                        if (postType != null)
                        {
                            filter.Add(new FilterClause(
                                           new QueryWrapperFilter(new TermQuery(new Term("PostType",
                                                                                         postType.Value.ToString()))), Occur.MUST));
                        }

                        Sort sort = new Sort(SortField.FIELD_SCORE);

                        switch (orderBy)
                        {
                        case SearchResultSortType.NumberOfVisits:
                            sort = new Sort(new SortField("NumberOfVisit", SortField.INT, true));
                            break;

                        case SearchResultSortType.PublishDate:
                            sort = new Sort(new SortField("PublishDate", SortField.LONG, true));
                            break;

                        case SearchResultSortType.LastUpDate:
                            sort = new Sort(new SortField("LastUpDate", SortField.LONG, true));
                            break;
                        }

                        var hits = searcher.Search(query, filter, maxResult, sort).ScoreDocs;

                        foreach (var scoreDoc in hits)
                        {
                            var doc = searcher.Doc(scoreDoc.Doc);
                            result.Documents.Add(new SearchResultDocument()
                            {
                                DocumentId       = int.Parse(doc.Get("ID")),
                                LanguageId       = int.Parse(doc.Get("LanguageId")),
                                LanguageIsoCode  = doc.Get("LanguageCode"),
                                Score            = scoreDoc.Score,
                                DocumentTitle    = doc.Get("Title"),
                                DocumentBody     = doc.Get("Description"),
                                DocumentKeywords = doc.Get("Keywords"),
                                DocumentTags     = doc.Get("Tags"),
                            });
                        }

                        result.Documents = result.Documents.DistinctBy(p => new { p.DocumentId })
                                           .ToList();

                        analyzer.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Error    = ex;
                result.HasError = true;
            }

            watch.Stop();
            result.ElapsedMilliseconds = watch.ElapsedMilliseconds;

            return(result);
        }