Esempio n. 1
0
 /// <summary>
 /// PrefixQuery  count
 /// </summary>
 /// <param name="keyword"></param>
 /// <param name="field"></param>
 /// <returns></returns>
 public int GetCount(string keyword,string field)
 {
     int count = 0;
     string indexname = ConfigurationManager.AppSettings["TagIndex"].ToString();
     string typename = ConfigurationManager.AppSettings["TagType"].ToString();
     QueryContainer query = new PrefixQuery() { Field = field, Value = keyword };
     //调用仅取数量方法
     var counts = Connect.GetSearchClient().Count<KeyWords>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(query)
     );
     count = (int)counts.Count;
     return count;
 }
Esempio n. 2
0
        public virtual void TestNoSuchMultiTermsInNear()
        {
            //test to make sure non existent multiterms aren't throwing null pointer exceptions
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery  spanNoSuch  = new SpanMultiTermQueryWrapper <MultiTermQuery>(fuzzyNoSuch);
            SpanQuery  term        = new SpanTermQuery(new Term("field", "brown"));
            SpanQuery  near        = new SpanNearQuery(new SpanQuery[] { term, spanNoSuch }, 1, true);

            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);
            //flip order
            near = new SpanNearQuery(new SpanQuery[] { spanNoSuch, term }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            WildcardQuery wcNoSuch     = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery     spanWCNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(wcNoSuch);

            near = new SpanNearQuery(new SpanQuery[] { term, spanWCNoSuch }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            RegexpQuery rgxNoSuch     = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery   spanRgxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(rgxNoSuch);

            near = new SpanNearQuery(new SpanQuery[] { term, spanRgxNoSuch }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            PrefixQuery prfxNoSuch     = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery   spanPrfxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(prfxNoSuch);

            near = new SpanNearQuery(new SpanQuery[] { term, spanPrfxNoSuch }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            //test single noSuch
            near = new SpanNearQuery(new SpanQuery[] { spanPrfxNoSuch }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            //test double noSuch
            near = new SpanNearQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch }, 1, true);
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);
        }
Esempio n. 3
0
        public virtual void Delete(int itemID)
        {
            logger.Info("Deleting item #" + itemID);

            lock (accessor)
            {
                using (var iw = accessor.GetWriter())
                {
                    var    s     = accessor.GetSearcher();
                    string trail = GetTrail(s, new Term(TextExtractor.Properties.ID, itemID.ToString()));
                    if (trail == null)
                    {
                        return; // not indexed
                    }
                    var query = new PrefixQuery(new Term(TextExtractor.Properties.Trail, trail));
                    iw.DeleteDocuments(query);
                    iw.PrepareCommit();
                    iw.Commit();
                }
                accessor.RecreateSearcher();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 添加TermQuery或PrefixQuery搜索条件
        /// </summary>
        /// <param name="fieldName">字段名称</param>
        /// <param name="value">字段值</param>
        /// <param name="exactMatch">是否精确搜索</param>
        /// <param name="boostLevel">权重级别</param>
        /// <param name="asFilter">是否作为过滤条件</param>
        /// <returns>LuceneSearchBuilder</returns>
        public LuceneSearchBuilder WithField(string fieldName, string value, bool exactMatch = true, BoostLevel?boostLevel = null, bool asFilter = false)
        {
            string filteredValue = ClauseScrubber.LuceneKeywordsScrub(value);

            if (string.IsNullOrEmpty(filteredValue))
            {
                return(this);
            }

            Term  term = new Term(fieldName, filteredValue);
            Query query;

            if (exactMatch)
            {
                query = new TermQuery(term);
            }
            else
            {
                query = new PrefixQuery(term);
            }

            if (boostLevel.HasValue)
            {
                SetBoost(query, boostLevel.Value);
            }

            if (asFilter)
            {
                filters.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
            }
            else
            {
                clauses.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
            }

            return(this);
        }
Esempio n. 5
0
        public virtual void TestNoSuchMultiTermsInNotNear()
        {
            //test to make sure non existent multiterms aren't throwing non-matching field exceptions
            FuzzyQuery   fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery    spanNoSuch  = new SpanMultiTermQueryWrapper <MultiTermQuery>(fuzzyNoSuch);
            SpanQuery    term        = new SpanTermQuery(new Term("field", "brown"));
            SpanNotQuery notNear     = new SpanNotQuery(term, spanNoSuch, 0, 0);

            Assert.AreEqual(1, Searcher.Search(notNear, 10).TotalHits);

            //flip
            notNear = new SpanNotQuery(spanNoSuch, term, 0, 0);
            Assert.AreEqual(0, Searcher.Search(notNear, 10).TotalHits);

            //both noSuch
            notNear = new SpanNotQuery(spanNoSuch, spanNoSuch, 0, 0);
            Assert.AreEqual(0, Searcher.Search(notNear, 10).TotalHits);

            WildcardQuery wcNoSuch     = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery     spanWCNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(wcNoSuch);

            notNear = new SpanNotQuery(term, spanWCNoSuch, 0, 0);
            Assert.AreEqual(1, Searcher.Search(notNear, 10).TotalHits);

            RegexpQuery rgxNoSuch     = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery   spanRgxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(rgxNoSuch);

            notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1);
            Assert.AreEqual(1, Searcher.Search(notNear, 10).TotalHits);

            PrefixQuery prfxNoSuch     = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery   spanPrfxNoSuch = new SpanMultiTermQueryWrapper <MultiTermQuery>(prfxNoSuch);

            notNear = new SpanNotQuery(term, spanPrfxNoSuch, 1, 1);
            Assert.AreEqual(1, Searcher.Search(notNear, 10).TotalHits);
        }
Esempio n. 6
0
        public BookWrapper[] Search(string query, int count)
        {
            if (searcher == null)
            {
                return(new BookWrapper[0]);
            }

            var processedQuery = queryCleaner.Replace(query ?? string.Empty, string.Empty).ToLower(CultureInfo.InvariantCulture);

            if (string.IsNullOrWhiteSpace(processedQuery))
            {
                return(booksByIsbn.Values
                       .Take(count)
                       .ToArray());
            }

            var isbnQuery   = new PrefixQuery(new Term(isbnFieldName, processedQuery));
            var authorQuery = CreatePhraseQuery(authorFieldName, processedQuery);
            var titleQuery  = CreatePhraseQuery(titleFieldName, processedQuery);

            var booleanQuery = new BooleanQuery();

            booleanQuery.Clauses.Add(new BooleanClause(isbnQuery, Occur.SHOULD));
            booleanQuery.Clauses.Add(new BooleanClause(authorQuery, Occur.SHOULD));
            booleanQuery.Clauses.Add(new BooleanClause(titleQuery, Occur.SHOULD));

            var searchResult = searcher.Search(booleanQuery, count);
            var foundIsbns   = searchResult.ScoreDocs
                               .Select(d => searcher.Doc(d.Doc))
                               .Select(d => d.Get(isbnFieldName))
                               .ToArray();

            var foundBooks = foundIsbns.Select(isbn => booksByIsbn[isbn]).ToArray();

            return(foundBooks);
        }
Esempio n. 7
0
        public static string[] GetHints(string luceneIndex, string word, int maxResults, bool caseSensitive)
        {
            PrefixQuery query;

            try
            {
                readWriteLock.TryEnterReadLock(Constants.ReadWriteLockTimeOutMilliseconds);

                if (caseSensitive)
                {
                    query = new PrefixQuery(new Term(nameof(CodeWord.Word), word));
                }
                else
                {
                    query = new PrefixQuery(new Term(nameof(CodeWord.WordLower), word.ToLower()));
                }

                return(SearchDocuments(luceneIndex, query, maxResults).Select(u => u.Get(nameof(CodeWord.Word))).ToArray());
            }
            finally
            {
                readWriteLock.ExitReadLock();
            }
        }
Esempio n. 8
0
        public virtual void TestNoSuchMultiTermsInSpanFirst()
        {
            //this hasn't been a problem
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);

            Assert.AreEqual(0, searcher.Search(spanFirst, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
            Assert.AreEqual(0, searcher.Search(spanFirst, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
            Assert.AreEqual(0, searcher.Search(spanFirst, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
            Assert.AreEqual(0, searcher.Search(spanFirst, 10).TotalHits);
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj == this)
            {
                return(true);
            }

            return(obj is CatalogQuery other &&
                   ((SortedAttributeQuery == null && other.SortedAttributeQuery == null) || (SortedAttributeQuery?.Equals(other.SortedAttributeQuery) == true)) &&
                   ((ExactQuery == null && other.ExactQuery == null) || (ExactQuery?.Equals(other.ExactQuery) == true)) &&
                   ((SetQuery == null && other.SetQuery == null) || (SetQuery?.Equals(other.SetQuery) == true)) &&
                   ((PrefixQuery == null && other.PrefixQuery == null) || (PrefixQuery?.Equals(other.PrefixQuery) == true)) &&
                   ((RangeQuery == null && other.RangeQuery == null) || (RangeQuery?.Equals(other.RangeQuery) == true)) &&
                   ((TextQuery == null && other.TextQuery == null) || (TextQuery?.Equals(other.TextQuery) == true)) &&
                   ((ItemsForTaxQuery == null && other.ItemsForTaxQuery == null) || (ItemsForTaxQuery?.Equals(other.ItemsForTaxQuery) == true)) &&
                   ((ItemsForModifierListQuery == null && other.ItemsForModifierListQuery == null) || (ItemsForModifierListQuery?.Equals(other.ItemsForModifierListQuery) == true)) &&
                   ((ItemsForItemOptionsQuery == null && other.ItemsForItemOptionsQuery == null) || (ItemsForItemOptionsQuery?.Equals(other.ItemsForItemOptionsQuery) == true)) &&
                   ((ItemVariationsForItemOptionValuesQuery == null && other.ItemVariationsForItemOptionValuesQuery == null) || (ItemVariationsForItemOptionValuesQuery?.Equals(other.ItemVariationsForItemOptionValuesQuery) == true)));
        }
Esempio n. 10
0
        private TopDocs PrefixQuery(string keyword, string field)
        {
            TopDocs docs = null;
            int     n    = 10;//最多返回多少个结果

            SetOutput(string.Format("正在检索关键字:{0}", keyword));
            try
            {
                Term        t     = new Term(field, keyword);
                PrefixQuery query = new PrefixQuery(t);
                Stopwatch   watch = new Stopwatch();
                watch.Start();
                docs = searcher.Search(query, (Filter)null, n);
                watch.Stop();
                StringBuffer sb = "PrefixQuery搜索完成,共用时:" + watch.Elapsed.Hours + "时 " + watch.Elapsed.Minutes + "分 " + watch.Elapsed.Seconds + "秒 " + watch.Elapsed.Milliseconds + "毫秒";
                SetOutput(sb);
            }
            catch (Exception ex)
            {
                SetOutput(ex.Message);
                docs = null;
            }
            return(docs);
        }
Esempio n. 11
0
        /// <summary>
        /// 产品搜索
        /// 从索引
        /// </summary>
        /// <param name="key">搜索关键字</param>
        /// <param name="categorySysNo">分类编号</param>
        /// <param name="attributes">属性列表</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">分页总数</param>
        /// <param name="recCount">总记录数</param>
        /// <param name="highLight">是否高亮关键字</param>
        /// <param name="sort">排序(0:相关度 1:销量 2:价格 3:评分 4:上架时间)</param>
        /// <param name="isDescending">true 为降序 false为升序</param>
        /// <param name="productSysNo">商品编号</param>
        /// <param name="priceSource">产品价格来源</param>
        /// <param name="priceSourceSysNo">产品价格来源编号(会员等级编号)</param>
        /// <param name="showNotFrontEndOrder">搜索前台不能下单的商品(2014-2-14 黄波 添加 物流APP需要)</param>
        /// <returns>商品列表</returns>
        /// <remarks>2013-08-08 黄波 创建</remarks>
        /// <remarks>2013-11-12 邵斌 修改 添加搜索商品系统编号:该方法暂时闲置</remarks>
        /// <remarks>2013-12-23 邵斌 添加前台是否下单字段</remarks>
        /// <remarks>2014-02-14 黄波 添加是否搜索前台不能下单的商品(物流APP需要)</remarks>
        public IList <PdProductIndex> Search(string key
                                             , int?categorySysNo
                                             , List <int> attributes
                                             , int pageSize
                                             , ref int pageIndex
                                             , ref int pageCount
                                             , ref int recCount
                                             , bool highLight    = false
                                             , int sort          = 1
                                             , bool isDescending = false
                                             , int productSysNo  = 0
                                             , ProductStatus.产品价格来源 priceSource = ProductStatus.产品价格来源.会员等级价
                                             , int priceSourceSysNo             = CustomerLevel.初级
                                             , bool showNotFrontEndOrder        = false)
        {
            var returnValue = new List <PdProductIndex>();
            var indexSearch = Hyt.Infrastructure.Lucene.ProductIndex.Searcher;

            try
            {
                pageIndex = pageIndex == 0 ? 1 : pageIndex;
                key       = key ?? ""; //查询设置初始值

                #region 搜索条件
                BooleanQuery query = new BooleanQuery();
                BooleanQuery childQuery;
                BooleanQuery esenQuery;

                #region 关键字搜索
                string keywords = key.Trim();
                if (!string.IsNullOrWhiteSpace(keywords))
                {
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();
                    if (!IsErpCode(keywords))
                    {
                        ////全词去空格
                        //esenQuery.Add(new TermQuery(new Term("ProductName", Regex.Replace(keywords, @"\s", ""))),
                        //        BooleanClause.Occur.SHOULD);
                        //esenQuery.SetBoost(3.0F);
                        //esenQuery.Add(new TermQuery(new Term("ProductSubName", Regex.Replace(keywords, @"\s", ""))),
                        //        BooleanClause.Occur.SHOULD);
                        //esenQuery.SetBoost(3.0F);
                        //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                        esenQuery = new BooleanQuery();
                        //分词 盘古分词
                        var keyWordsSplitBySpace = GetKeyWordsSplitBySpace(keywords);

                        if (string.IsNullOrWhiteSpace(keyWordsSplitBySpace))
                        {
                            return(null);
                        }

                        keyWordsSplitBySpace = string.Format("{0}^{1}.0", keywords, (int)Math.Pow(3, 5));

                        QueryParser productNameQueryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "ProductName", new PanGuAnalyzer(true));
                        Query       productNameQuery       = productNameQueryParser.Parse(keyWordsSplitBySpace);
                        childQuery.Add(productNameQuery, BooleanClause.Occur.SHOULD);

                        //以什么开头,输入“ja”就可以搜到包含java和javascript两项结果了
                        Query prefixQuery_productName = new PrefixQuery(new Term("ProductName", key));
                        //直接模糊匹配,假设你想搜索跟‘wuzza’相似的词语,你可能得到‘fuzzy’和‘wuzzy’。
                        Query fuzzyQuery_productName = new FuzzyQuery(new Term("ProductName", key));
                        //通配符搜索
                        Query wildcardQuery_productName = new WildcardQuery(new Term("ProductName", string.Format("*{0}*", key.Trim())));


                        childQuery.Add(prefixQuery_productName, BooleanClause.Occur.SHOULD);
                        childQuery.Add(fuzzyQuery_productName, BooleanClause.Occur.SHOULD);
                        childQuery.Add(wildcardQuery_productName, BooleanClause.Occur.SHOULD);
                        //esenQuery.Add(new QueryParser("ProductName", new PanGuAnalyzer(true)).Parse(keyWordsSplitBySpace), BooleanClause.Occur.SHOULD);
                        //esenQuery.Add(new QueryParser("ProductSubName", new PanGuAnalyzer(true)).Parse(keyWordsSplitBySpace), BooleanClause.Occur.SHOULD);

                        ////分词  按空格
                        //var keyColl = keywords.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        //foreach (var item in keyColl)
                        //{
                        //    esenQuery.Add(new TermQuery(new Term("ProductName", item)),
                        //        BooleanClause.Occur.SHOULD);
                        //    esenQuery.Add(new TermQuery(new Term("ProductSubName", item)),
                        //        BooleanClause.Occur.SHOULD);
                        //}
                        //esenQuery.SetBoost(2.9F);
                        //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }
                    else
                    {
                        esenQuery.Add(new TermQuery(new Term("ErpCode", Regex.Replace(keywords, @"\s", ""))),
                                      BooleanClause.Occur.SHOULD);
                        esenQuery.SetBoost(3.0F);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 分类搜索
                if (categorySysNo.HasValue && categorySysNo.Value != 0)
                {
                    childQuery = new BooleanQuery();

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new TermQuery(new Term("Category", categorySysNo.Value.ToString())),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(3.0F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new WildcardQuery(new Term("AssociationCategory", string.Format("*,{0},*", categorySysNo.Value.ToString()))),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(2.8F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    //所有子分类
                    var childCategoryList = Hyt.BLL.Web.PdCategoryBo.Instance.GetChildAllCategory(categorySysNo.Value);
                    foreach (var item in childCategoryList)
                    {
                        esenQuery = new BooleanQuery();
                        esenQuery.Add(new TermQuery(new Term("Category", item.SysNo.ToString())),
                                      BooleanClause.Occur.SHOULD);
                        esenQuery.SetBoost(3.0F);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                        esenQuery = new BooleanQuery();
                        esenQuery.Add(new WildcardQuery(new Term("AssociationCategory", string.Format("*,{0},*", item.SysNo.ToString()))),
                                      BooleanClause.Occur.SHOULD);
                        childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    }

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 属性搜索
                if (attributes != null)
                {
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();

                    foreach (var item in attributes)
                    {
                        esenQuery.Add(new WildcardQuery(new Term("Attributes", string.Format("*,*:{0},*", item.ToString()))),
                                      BooleanClause.Occur.MUST);
                    }
                    childQuery.Add(esenQuery, BooleanClause.Occur.MUST);

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                #endregion

                #region 品牌搜索
                //if (brandSysNo.Value != 0)
                //{
                //    childQuery = new BooleanQuery();
                //    childQuery.Add(new TermQuery(new Term("BrandSysNo", brandSysNo.Value.ToString())),
                //               BooleanClause.Occur.SHOULD);
                //    childQuery.SetBoost(3.0F);
                //    query.Add(childQuery, BooleanClause.Occur.MUST);
                //}
                #endregion

                #region 仅搜索有效的商品

                childQuery = new BooleanQuery();
                childQuery.Add(new TermQuery(new Term("Status", ((int)Hyt.Model.WorkflowStatus.ProductStatus.产品上线状态.效).ToString())),
                               BooleanClause.Occur.SHOULD);
                query.Add(childQuery, BooleanClause.Occur.MUST);

                //2013-12-23 邵斌 添加前台是否下单字段
                if (!showNotFrontEndOrder)
                {
                    childQuery = new BooleanQuery();
                    childQuery.Add(new TermQuery(new Term("CanFrontEndOrder", ((int)Hyt.Model.WorkflowStatus.ProductStatus.商品是否前台下单.是).ToString())),
                                   BooleanClause.Occur.SHOULD);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                #endregion

                #region 排序

                //isDescending true为降序 false为升序
                SortField sf = null;
                switch (Math.Abs(sort))
                {
                case 1:    //销量
                    sf = new SortField("SalesCount", SortField.INT, isDescending);
                    break;

                case 2:    //价格
                    sf = new SortField("BasicPrice", SortField.FLOAT, isDescending);
                    break;

                case 3:    //评分
                    sf = new SortField("CommentCount", SortField.INT, isDescending);
                    break;

                case 4:    //上架时间
                    sf = new SortField("CreatedDate", SortField.STRING, isDescending);
                    break;

                default:
                    sf = new SortField(null, SortField.SCORE, false);
                    break;
                }

                Sort luceneSort;                            //排序对象

                //默认匹配度,表明是对固定信息进行搜索,所以就要进行先安匹配度来排序。这样用户搜索的商品将排在前面,方便用户筛选
                if (Math.Abs(sort) != (int)CommonEnum.LuceneProductSortType.默认匹配度)
                {
                    //无搜索关键字的时候就按模式设置的进行排序
                    luceneSort = new Sort();
                    luceneSort.SetSort(sf);
                }
                else
                {
                    //收搜索关键字时,就要先安匹配度进行排序,然后才是设置排序。
                    luceneSort = new Sort(new SortField[] { SortField.FIELD_SCORE, sf });
                }

                #endregion

                #region 商品系统编号搜索

                if (productSysNo > 0)
                {
                    childQuery = new BooleanQuery();

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new TermQuery(new Term("SysNo", productSysNo.ToString())),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(3.0F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                #endregion

                #endregion

                Hits hits = indexSearch.Search(query, luceneSort);

                recCount  = hits.Length();
                pageCount = (int)Math.Ceiling((double)recCount / (double)pageSize);
                if (pageIndex <= 0)
                {
                    pageIndex = 1;
                }
                if (pageIndex > pageCount)
                {
                    pageIndex = pageCount;
                }

                var recordIndex = Math.Max((pageIndex - 1) * pageSize, 0);

                PdProductIndex pdProductIndex;
                var            simpleHtmlFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
                var            T_ProductName       = "";
                while (recordIndex < recCount && returnValue.Count < pageSize)
                {
                    try
                    {
                        pdProductIndex = Hyt.Infrastructure.Lucene.ProductIndex.Instance.DocumentToModel(hits.Doc(recordIndex));
                        string productName = pdProductIndex.ProductName;
                        if (highLight && !string.IsNullOrEmpty(key))
                        {
                            var highlighter = new PanGu.HighLight.Highlighter(simpleHtmlFormatter, new PanGu.Segment())
                            {
                                FragmentSize = 50
                            };
                            T_ProductName = highlighter.GetBestFragment(key.Trim(), pdProductIndex.ProductName);
                            if (!string.IsNullOrWhiteSpace(T_ProductName))
                            {
                                pdProductIndex.ProductName = T_ProductName;
                            }
                        }
                        pdProductIndex.RankPrice = GetProductRankPrice(pdProductIndex.Prices, pdProductIndex.BasicPrice, priceSource, priceSourceSysNo);

                        returnValue.Add(pdProductIndex);
                    }
                    catch (Exception ex)
                    {
                        BLL.Log.SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, ex);
                    }
                    finally
                    {
                        recordIndex++;
                    }
                }
            }
            catch (Exception ex)
            {
                BLL.Log.SysLog.Instance.Error(LogStatus.系统日志来源.后台, ex.Message, ex);
            }
            finally
            {
                // indexSearch.Close();
            }
            return(returnValue);
        }
Esempio n. 12
0
 /// <summary>
 /// PrefixQuery查询
 /// </summary>
 /// <param name="keyword"></param>
 /// <param name="field"></param>
 /// <param name="start"></param>
 /// <param name="size"></param>
 /// <returns></returns>
 public List<KeyWords> GetList(string keyword, string field, int start, int size)
 {
     string indexname = ConfigurationManager.AppSettings["TagIndex"].ToString();
     string typename = ConfigurationManager.AppSettings["TagType"].ToString();
     QueryContainer query = new PrefixQuery() { Field = field, Value = keyword };
     var searchResults = Connect.GetSearchClient().Search<KeyWords>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(query)
         .From(start)
         .Size(size)
     );
     List<KeyWords> eslist = new List<KeyWords>(searchResults.Documents);
     return eslist;
 }
        public void Boost_ReturnsPrefixQuery()
        {
            var query = new PrefixQuery("prefix").Boost(2.2);

            Assert.IsInstanceOf <PrefixQuery> (query);
        }
Esempio n. 14
0
 public int GetCountByPrefix(string keyword, string field)
 {
     int count = 0;
     string indexname = ConfigurationManager.AppSettings["CRMIndex"].ToString();
     string typename = ConfigurationManager.AppSettings["CRMMemberType"].ToString();
     if (string.IsNullOrEmpty(field))
     { field = "_all"; }
     else { field = field.ToLower(); }
     QueryContainer prefixQuery = new PrefixQuery() { Field = field, Value = keyword };
     //调用仅取数量方法
     var counts = Connect.GetSearchClient().Count<Member>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(prefixQuery)
     );
     count = (int)counts.Count;
     return count;
 }
Esempio n. 15
0
		public SpanMultiQuery(PrefixQuery query)
		{
			_query = query;
		}
        public void TestCrazyPrefixes1()
        {
            Query expected = new PrefixQuery(new Term("field", "st*ar"));

            assertEquals(expected, Parse("st*ar*"));
        }
        public static IQueriedSearch <TSource, QueryStringQuery> UsingRelevanceImproved <TSource>(this IQueriedSearch <TSource> search, params Expression <Func <TSource, string> >[] fieldSelectors)
        {
            return(new Search <TSource, QueryStringQuery>(search, context =>
            {
                BoolQuery currentBoolQuery;
                BoolQuery newBoolQuery = new BoolQuery();
                MinShouldMatchQueryStringQuery currentQueryStringQuery;

                if (QueryHelpers.TryGetBoolQuery(context.RequestBody.Query, out currentBoolQuery))
                {
                    if (!QueryHelpers.TryGetMinShouldMatchQueryStringQuery(currentBoolQuery.Should[0], out currentQueryStringQuery))
                    {
                        return;
                    }
                    newBoolQuery = currentBoolQuery;
                }
                else
                {
                    currentBoolQuery = new BoolQuery();
                    if (!QueryHelpers.TryGetMinShouldMatchQueryStringQuery(context.RequestBody.Query, out currentQueryStringQuery))
                    {
                        return;
                    }
                    newBoolQuery.Should.Add(currentQueryStringQuery);
                }

                // If .UsingImprovedSynonyms has not been used there is no ExpandedQuery for us then we use the query
                if (currentQueryStringQuery.ExpandedQuery.IsNull())
                {
                    currentQueryStringQuery.ExpandedQuery = new string[] { currentQueryStringQuery.Query.ToString().Replace("¤", " ") };
                }

                foreach (var query in currentQueryStringQuery.ExpandedQuery)
                {
                    var terms = QueryHelpers.GetQueryPhrases(query);

                    foreach (var fieldSelector in fieldSelectors)
                    {
                        var fieldNameLowercase = search.Client.Conventions.FieldNameConvention.GetFieldNameForLowercase(fieldSelector);
                        var fieldNameAnalyzed = search.Client.Conventions.FieldNameConvention.GetFieldNameForAnalyzed(fieldSelector);

                        // Create PrefixQuery for single term queries larger than 2 characters
                        if (terms.Count() == 1 && terms.First().Length > 2)
                        {
                            var prefixQuery = new PrefixQuery(fieldNameLowercase, query.ToLower())
                            {
                                Boost = 0.5
                            };
                            newBoolQuery.Should.Add(prefixQuery);
                        }

                        // Create PhraseQuery and PhrasePrefixQuery for multiple term queries
                        if (terms.Count() > 1)
                        {
                            var phraseQuery = new PhraseQuery(fieldNameAnalyzed, query)
                            {
                                Boost = 5
                            };
                            var phrasePrefixQuery = new PhrasePrefixQuery(fieldNameLowercase, query.ToLower())
                            {
                                Boost = 10
                            };
                            newBoolQuery.Should.Add(phraseQuery);
                            newBoolQuery.Should.Add(phrasePrefixQuery);
                        }
                    }
                }

                if (newBoolQuery.Should.Count == 0)
                {
                    return;
                }

                context.RequestBody.Query = newBoolQuery;
            }));
        }
Esempio n. 18
0
        /// <summary>
        /// 查询索引
        /// </summary>
        /// <param name="fieldName">FieldName</param>
        /// <param name="keywords">关键字</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="totalRecord">总的记录</param>
        /// <returns>索引列表</returns>
        /// <remarks>2013-08-15 朱成果 创建</remarks>
        public List <CBPdProductIndex> QueryDoc(string fieldName, string keywords, int pageIndex, int pageSize, out int totalRecord)
        {
            var   search = new IndexSearcher(IndexStorePath);
            Query searchQuery;

            if (!string.IsNullOrEmpty(fieldName) && !string.IsNullOrEmpty(keywords))
            {
                #region [关键字查询]
                var          query = new BooleanQuery();
                BooleanQuery childQuery;
                BooleanQuery esenQuery;
                if (fieldName == "ProductName")
                {
                    #region 2016-4-6 杨浩 新增模糊搜索
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();
                    //模糊搜索
                    //esenQuery.Add(new FuzzyQuery(new Term("ProductName", Regex.Replace(keywords, @"\s", ""))), BooleanClause.Occur.SHOULD);

                    //esenQuery.SetBoost(4.0F);


                    //分词 盘古分词
                    var keyWordsSplitBySpace = GetKeyWordsSplitBySpace(keywords);


                    //string keyWordsSplitBySpace = string.Format("{0}^{1}.0", keywords, (int)Math.Pow(3, 5));
                    //不启用分词,直接用模糊搜索
                    QueryParser productNameQueryParser = new QueryParser(global::Lucene.Net.Util.Version.LUCENE_29, "ProductName", new PanGuAnalyzer(true));
                    Query       productNameQuery       = productNameQueryParser.Parse(keyWordsSplitBySpace);
                    childQuery.Add(productNameQuery, BooleanClause.Occur.SHOULD);

                    //以什么开头,输入“ja”就可以搜到包含java和javascript两项结果了
                    Query prefixQuery_productName = new PrefixQuery(new Term("ProductName", keywords.Trim()));

                    //直接模糊匹配,假设你想搜索跟‘wuzza’相似的词语,你可能得到‘fuzzy’和‘wuzzy’。
                    Query fuzzyQuery_productName = new FuzzyQuery(new Term("ProductName", keywords.Trim()));
                    //通配符搜索
                    Query wildcardQuery_productName = new WildcardQuery(new Term("ProductName", string.Format("{0}", keywords.Trim())));

                    childQuery.Add(prefixQuery_productName, BooleanClause.Occur.SHOULD);
                    childQuery.Add(fuzzyQuery_productName, BooleanClause.Occur.SHOULD);
                    childQuery.Add(wildcardQuery_productName, BooleanClause.Occur.SHOULD);
                    childQuery.SetBoost(4.0F);


                    //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                    #endregion

                    //childQuery = new BooleanQuery();
                    //esenQuery = new BooleanQuery();
                    ////全词去空格
                    //esenQuery.Add(new TermQuery(new Term("ProductName", Regex.Replace(keywords, @"\s", ""))),
                    //        BooleanClause.Occur.SHOULD);
                    //esenQuery.SetBoost(3.0F);
                    //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    //esenQuery = new BooleanQuery();
                    ////分词 盘古分词
                    //esenQuery.Add(new QueryParser("ProductName", new PanGuAnalyzer(true)).Parse(keywords),
                    //    BooleanClause.Occur.SHOULD);

                    ////分词  按空格
                    //var keyColl = keywords.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    //foreach (var item in keyColl)
                    //{
                    //    esenQuery.Add(new TermQuery(new Term("ProductName", item)),
                    //        BooleanClause.Occur.SHOULD);
                    //}
                    //esenQuery.SetBoost(2.9F);
                    //childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);
                    //query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                else if (fieldName == "Category")
                {
                    childQuery = new BooleanQuery();
                    esenQuery  = new BooleanQuery();
                    esenQuery.Add(new TermQuery(new Term("Category", keywords)),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(3.0F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    esenQuery = new BooleanQuery();
                    esenQuery.Add(new WildcardQuery(new Term("AssociationCategory", string.Format("*,{0},*", keywords))),
                                  BooleanClause.Occur.SHOULD);
                    esenQuery.SetBoost(2.8F);
                    childQuery.Add(esenQuery, BooleanClause.Occur.SHOULD);

                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }

                else if (fieldName == "BrandSysNo")
                {
                    childQuery = new BooleanQuery();
                    childQuery.Add(new TermQuery(new Term("BrandSysNo", keywords)),
                                   BooleanClause.Occur.SHOULD);
                    childQuery.SetBoost(3.0F);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                else if (fieldName == "DealerSysNos")
                {
                    childQuery = new BooleanQuery();
                    childQuery.Add(new WildcardQuery(new Term("DealerSysNos", string.Format("*,{0},*", keywords))),
                                   BooleanClause.Occur.SHOULD);
                    childQuery.SetBoost(2.8F);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                else if (fieldName == "ProductGroupCode")
                {
                    childQuery = new BooleanQuery();
                    childQuery.Add(new WildcardQuery(new Term("ProductGroupCode", string.Format("*,{0},*", keywords))),
                                   BooleanClause.Occur.SHOULD);
                    childQuery.SetBoost(2.8F);
                    query.Add(childQuery, BooleanClause.Occur.MUST);
                }
                else
                {
                    query.Add(new TermQuery(new Term(fieldName, keywords)),
                              BooleanClause.Occur.SHOULD);
                }
                #endregion
                searchQuery = query;
            }
            else
            {
                searchQuery = new WildcardQuery(new Term("ProductName", "*雪花秀*"));
            }
            //排序方式
            var sort = new Sort();
            //搜索
            Hits hits = search.Search(searchQuery, sort);

            totalRecord = hits.Length();//总的记录
            int startIndex = (pageIndex - 1) * pageSize;
            if (startIndex < 0)
            {
                startIndex = 0;
            }
            int endIndex = startIndex + pageSize;
            if (endIndex > totalRecord - 1)
            {
                endIndex = totalRecord - 1;
            }
            List <CBPdProductIndex> lst = new List <CBPdProductIndex>();
            for (int i = startIndex; i <= endIndex; i++)
            {
                var doc = hits.Doc(i);
                lst.Add(
                    new CBPdProductIndex
                {
                    DocID = hits.Id(i),
                    Score = hits.Score(i),
                    AssociationCategory = doc.Get("AssociationCategory"),
                    Attributes          = doc.Get("Attributes"),
                    Barcode             = doc.Get("Barcode"),
                    BrandSysNo          = Convert.ToInt32(doc.Get("BrandSysNo")),
                    Category            = Convert.ToInt32(doc.Get("Category")),
                    DisplayOrder        = Convert.ToInt32(doc.Get("DisplayOrder")),
                    NameAcronymy        = doc.Get("NameAcronymy"),
                    Prices           = doc.Get("Prices"),
                    ProductImage     = doc.Get("ProductImage"),
                    ProductName      = doc.Get("ProductName"),
                    QRCode           = doc.Get("QRCode"),
                    Status           = Convert.ToInt32(doc.Get("Status")),
                    SysNo            = Convert.ToInt32(doc.Get("SysNo")),
                    BasicPrice       = Convert.ToDecimal(doc.Get("BasicPrice")),
                    Price            = Convert.ToDecimal(doc.Get("Price")),
                    DispalySymbol    = 0,
                    RankPrice        = 0.00M,
                    ProductGroupCode = Convert.ToString(doc.Get("ProductGroupCode")),
                    DealerSysNos     = doc.Get("DealerSysNos"),
                    WarehouseSysNos  = doc.Get("WarehouseSysNos")
                });
            }
            search.Close();
            return(lst);
        }
Esempio n. 19
0
            public override Query ProcessQuery(string indexName, Query query, IndexQuery originalQuery)
            {
                if (indexName != SpecificIndexName)
                    return query;

                var customQuery = new PrefixQuery(new Term("CustomField", "CustomPrefix"));

                return new BooleanQuery
                    {
                        { query, Occur.MUST },
                        { customQuery, Occur.MUST}
                    };
            }
        public virtual void TestNoSuchMultiTermsInSpanFirst()
        {
            //this hasn't been a problem
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);

            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
            Assert.AreEqual(0, Searcher.Search(spanFirst, 10).TotalHits);
        }
        public virtual void TestNoSuchMultiTermsInOr()
        {
            //test to make sure non existent multiterms aren't throwing null pointer exceptions
            FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
            SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(fuzzyNoSuch);
            SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
            SpanOrQuery near = new SpanOrQuery(new SpanQuery[] { term, spanNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            //flip
            near = new SpanOrQuery(new SpanQuery[] { spanNoSuch, term });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
            SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(wcNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanWCNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
            SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(rgxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanRgxNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
            SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<MultiTermQuery>(prfxNoSuch);
            near = new SpanOrQuery(new SpanQuery[] { term, spanPrfxNoSuch });
            Assert.AreEqual(1, Searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch });
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);

            near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch });
            Assert.AreEqual(0, Searcher.Search(near, 10).TotalHits);
        }
Esempio n. 22
0
 public List<Member> GetListByPrefix(string keyword, string field, int start, int size)
 {
     string indexname = ConfigurationManager.AppSettings["CRMIndex"].ToString();
     string typename = ConfigurationManager.AppSettings["CRMMemberType"].ToString();
     if (string.IsNullOrEmpty(field))
     { field = "_all"; }
     else { field = field.ToLower(); }
     QueryContainer prefixQuery = new PrefixQuery() { Field = field, Value = keyword };
     var searchResults = Connect.GetSearchClient().Search<Member>(s => s
         .Index(indexname)
         .Type(typename)
         .Query(prefixQuery)
         .Analyzer("standard")
         .From(start)
         .Size(size)
     );
     List<Member> eslist = new List<Member>(searchResults.Documents);
     return eslist;
 }
Esempio n. 23
0
        /// <summary>
        /// 搜索文件
        /// </summary>
        public void SearchFile()
        {
            this.isStopSearch = false;
            this.dtStart      = DateTime.Now;//开始时间

            if (this.indexSearcher == null)
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            Query query;

            if (this.keyword.Name != "")
            {
                try
                {
                    QueryParser parser = new QueryParser("Name", this.analyzer);
                    query = parser.Parse(this.keyword.Name);
                    boolQuery.Add(query, BooleanClause.Occur.MUST);
                }
                catch (Exception)
                { }
            }

            if (this.keyword.Directory != null && this.keyword.Directory != "")
            {
                PrefixQuery pq = new PrefixQuery(new Term("Directory", this.keyword.Directory));
                boolQuery.Add(pq, BooleanClause.Occur.MUST);
            }

            if (this.keyword.Extension != null && this.keyword.Extension != "")
            {
                TermQuery termQuery = new TermQuery(new Term("Extension", this.keyword.Extension));
                boolQuery.Add(termQuery, BooleanClause.Occur.MUST);
            }

            try
            {
                //搜索前Static.PageItems项
                this.topDocs = this.indexSearcher.Search(boolQuery, Static.PageItems);
            }
            catch (Exception)
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            this.dtStop = DateTime.Now;//完成时间
            if (this.topDocs.totalHits > 0)
            {
                CrossThreadOperateControl CrossUpdateStatus = delegate()
                {
                    lock (this.form.tsslStatus)
                    {
                        this.form.OrginalStatusText = Deal.ToEnglishNumString(this.topDocs.totalHits)
                                                      + " 个对象     用时:" + (this.dtStop - this.dtStart).TotalSeconds + "秒";
                        this.form.tsslStatus.Text = Deal.LimitStringLength(this.form.OrginalStatusText,
                                                                           this.form.TsslLength);
                    }
                };
                this.form.Invoke(CrossUpdateStatus);
            }
            else
            {
                this.NullResult(); //空结果,修改主窗的status状态。
                this.CallBack();   //线程执行完,修改主窗的相关属性。
                return;
            }

            this.pageNums    = topDocs.totalHits / Static.PageItems + 1; //总的页数
            this.currentPage = 1;                                        //每次搜索时当前页数为1

            UpdateLVMethod method = new UpdateLVMethod(this.UpdateFileListView);

            this.UpdateListView(method, this.currentPage);
            this.CallBack();//线程执行完,修改主窗的相关属性。
        }
Esempio n. 24
0
        public void VisitMethod(QNode node)
        {
            var method = EnumResolver.ResolveNodeType(node.Type);

            switch (method)
            {
            case NodeType.OrderBy:
            case NodeType.OrderByDescending:
                var member = this.ContextField.Pop();
                var query  = this.ContextQuery.Pop();
                this.SortingRequest.Add(new KeyValuePair <Field, NodeType>(member, method));
                break;

            case NodeType.Where:
                break;

            case NodeType.Contains:
            {
                var value = string.Format("*{0}*", this.ContextValue.Pop());
                var term  = new QueryStringQuery {
                    DefaultField = this.ContextField.Pop(), Query = value
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.StartsWith:
            {
                var term = new PrefixQuery {
                    Field = this.ContextField.Pop(), Value = this.ContextValue.Pop()
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.EndsWith:
            {
                var value = string.Format("*{0}", this.ContextValue.Pop());
                var term  = new WildcardQuery {
                    Field = this.ContextField.Pop(), Value = value
                };
                this.ContextQuery.Push(term);
            }
            break;

            case NodeType.In:
            {
                var value = this.ContextValue.Pop() as List <string>;
                var terms = new TermsQuery {
                    Field = this.ContextField.Pop(), Terms = value
                };
                this.ContextQuery.Push(terms);
            }
            break;

            case NodeType.NotIn:
            {
                var value = this.ContextValue.Pop() as List <string>;
                var terms = !new TermsQuery {
                    Field = this.ContextField.Pop(), Terms = value
                };
                this.ContextQuery.Push(terms);
            }
            break;

            default:
                throw new NotImplementedException(method.ToString());
            }
        }
        /// <summary> Gets a MARC record from a Z39.50 server, by performaing a search </summary>
        /// <param name="Attribute_Number"> Z39.50 attribute number which indicates against which metadata field search should occur </param>
        /// <param name="Search_Term"> Term to search for within the attribute number </param>
        /// <param name="Z3950_Server"> Z39.50 server endpoint information </param>
        /// <param name="Message"> Return message from the server </param>
        /// <returns> MARC record, if retrievable by search </returns>
        public static MARC_Record Get_Record(int Attribute_Number, string Search_Term, Z3950_Endpoint Z3950_Server, out string Message)
        {
            // Initially set the message to empty
            Message = String.Empty;

            // http://jai-on-asp.blogspot.com/2010/01/z3950-client-in-cnet-using-zoomnet-and.html
            // http://www.indexdata.com/yaz/doc/tools.html#PQF
            // http://www.loc.gov/z3950/agency/defns/bib1.html
            // http://www.assembla.com/code/wasolic/subversion/nodes/ZOOM.NET
            // http://lists.indexdata.dk/pipermail/yazlist/2007-June/002080.html
            // http://fclaweb.fcla.edu/content/z3950-access-aleph

            string prefix = "@attrset Bib-1 @attr 1=" + Attribute_Number + " ";

            try
            {
                //	allocate MARC tools
                MARC21_Exchange_Format_Parser parser = new MARC21_Exchange_Format_Parser();

                //	establish connection
                IConnection connection = new Connection(Z3950_Server.URI, Convert.ToInt32(Z3950_Server.Port));
                connection.DatabaseName = Z3950_Server.Database_Name;

                // Any authentication here?
                if (Z3950_Server.Username.Length > 0)
                    connection.Username = Z3950_Server.Username;
                if (Z3950_Server.Password.Length > 0)
                    connection.Password = Z3950_Server.Password;

                // Set to USMARC
                connection.Syntax = RecordSyntax.USMARC;

                //	call the Z39.50 server
                IPrefixQuery query = new PrefixQuery(prefix + Search_Term);
                IResultSet records = connection.Search(query);

                // If the record count is not one, return a message
                if (records.Count != 1)
                {
                    Message = records.Count == 0 ? "ERROR: No matching record found in Z39.50 endpoint" : "ERROR: More than one matching record found in Z39.50 endpoint by ISBN";
                    return null;
                }

                //	capture the byte stream
                IRecord record = records[0];
                MemoryStream ms = new MemoryStream(record.Content);

                //	display while debugging
                //MessageBox.Show(Encoding.UTF8.GetString(record.Content));

                try
                {
                    //	feed the record to the parser and add the 955
                    MARC_Record marcrec = parser.Parse(ms);
                    parser.Close();
                    return marcrec;
                }
                catch (Exception error)
                {
                    Message = "ERROR: Unable to parse resulting record into the MARC Record structure!\n\n" + error.Message;
                    return null;
                    //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML.");
                    //Trace.WriteLine("Could not convert item " + Primary_Identifier   + " to MARCXML.");
                    //Trace.WriteLine("Error details: " + error.Message);
                }
            }
            catch (Exception error)
            {
                if (error.Message.IndexOf("The type initializer for 'Zoom.Net.Yaz") >= 0)
                {
                    Message = "ERROR: The Z39.50 libraries did not correctly initialize.\n\nThese libraries do not currently work in 64-bit environments.";
                }
                else
                {
                    Message = "ERROR: Unable to connect and search provided Z39.50 endpoint.\n\n" + error.Message;
                }

                return null;
                //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML.");
                //Trace.WriteLine("Could not convert item " + Primary_Identifier   + " to MARCXML.");
                //Trace.WriteLine("Error details: " + error.Message);
            }
        }
Esempio n. 26
0
 public SpanMultiQuery(PrefixQuery query)
 {
     _query = query;
 }
Esempio n. 27
0
 private static void MultiTermQuery(PrefixQuery query, AzureQueryLogger.IndentedTextWriter writer)
 {
     AzureQueryLogger.VisitTerm(query.Prefix, "Prefix Term", writer);
 }
        public static QueryBase GetDefaultQuery(this TermNode node, IQueryVisitorContext context)
        {
            if (!(context is IElasticQueryVisitorContext elasticContext))
            {
                throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context));
            }

            QueryBase query;
            string    field         = node.Field;
            var       defaultFields = node.GetDefaultFields(elasticContext.DefaultFields);

            if (field == null && defaultFields != null && defaultFields.Length == 1)
            {
                field = defaultFields[0];
            }

            if (elasticContext.MappingResolver.IsPropertyAnalyzed(field))
            {
                var fields = !String.IsNullOrEmpty(field) ? new[] { field } : defaultFields;

                if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*"))
                {
                    query = new QueryStringQuery {
                        Fields = fields,
                        AllowLeadingWildcard = false,
                        AnalyzeWildcard      = true,
                        Query = node.UnescapedTerm
                    };
                }
                else
                {
                    if (fields != null && fields.Length == 1)
                    {
                        if (node.IsQuotedTerm)
                        {
                            query = new MatchPhraseQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                        else
                        {
                            query = new MatchQuery {
                                Field = fields[0],
                                Query = node.UnescapedTerm
                            };
                        }
                    }
                    else
                    {
                        query = new MultiMatchQuery {
                            Fields = fields,
                            Query  = node.UnescapedTerm
                        };
                        if (node.IsQuotedTerm)
                        {
                            ((MultiMatchQuery)query).Type = TextQueryType.Phrase;
                        }
                    }
                }
            }
            else
            {
                if (!node.IsQuotedTerm && node.UnescapedTerm.EndsWith("*"))
                {
                    query = new PrefixQuery {
                        Field = field,
                        Value = node.UnescapedTerm.TrimEnd('*')
                    };
                }
                else
                {
                    query = new TermQuery {
                        Field = field,
                        Value = node.UnescapedTerm
                    };
                }
            }

            return(query);
        }
        private Query buildQuery(String searchTerm, IEnumerable <AnatomyFacet> facets)
        {
            Query query;

            if (String.IsNullOrWhiteSpace(searchTerm) || searchTerm == "*")
            {
                query = new MatchAllDocsQuery();
            }
            else
            {
                if (searchTerm.Length > 1)
                {
                    try
                    {
                        BooleanQuery boolQuery = new BooleanQuery();
                        using (TokenStream source = analyzer.ReusableTokenStream("Name", new System.IO.StringReader(searchTerm)))
                        {
                            var  termAtt    = source.GetAttribute <ITermAttribute>();
                            bool moreTokens = source.IncrementToken();
                            if (moreTokens)
                            {
                                while (moreTokens)
                                {
                                    var analyzedTerm = termAtt.Term;

                                    boolQuery.Add(new TermQuery(new Term("Name", analyzedTerm)), Occur.SHOULD);
                                    boolQuery.Add(new PrefixQuery(new Term("Name", analyzedTerm)), Occur.SHOULD);

                                    moreTokens = source.IncrementToken();
                                }
                                query = boolQuery;
                            }
                            else
                            {
                                query = buildEmptyQuery();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        query = buildEmptyQuery();
                    }
                }
                else
                {
                    query = new PrefixQuery(new Term("Name", searchTerm.ToLower()));
                }
            }

            if (facets != null && facets.Count() > 0)
            {
                BooleanQuery boolQuery = new BooleanQuery();
                boolQuery.Add(query, Occur.MUST);
                foreach (var facet in facets)
                {
                    bool         add        = false;
                    BooleanQuery facetQuery = new BooleanQuery();
                    foreach (var value in facet.Values)
                    {
                        facetQuery.Add(new TermQuery(new Term(facet.Field, value)), Occur.SHOULD);
                        add = true;
                    }
                    if (add)
                    {
                        boolQuery.Add(facetQuery, Occur.MUST);
                    }
                }
                query = boolQuery;
            }

            return(query);
        }
Esempio n. 30
0
        /// <summary>
        /// Search for Terms based on the search criteria.
        /// <param name="dictionary">The value for dictionary.</param>
        /// <param name="audience">Patient or Healthcare provider</param>
        /// <param name="language">The language in which the details needs to be fetched</param>
        /// <param name="query">The search query</param>
        /// <param name="matchType">Defines if the search should begin with or contain the key word</param>
        /// <param name="size">Defines the size of the search</param>
        /// <param name="from">Defines the Offset for search</param>
        /// <param name="includeAdditionalInfo">If true, the RelatedResources and Media fields will be populated. Else, they will be empty.</param>
        /// <returns>A list of GlossaryTerm</returns>
        /// </summary>
        public async Task <GlossaryTermResults> Search(string dictionary, AudienceType audience, string language, string query, MatchType matchType, int size, int from, bool includeAdditionalInfo)
        {
            // Elasticsearch knows how to figure out what the ElasticSearch name is for
            // a given field when given a PropertyInfo.
            Field[] requestedESFields = (includeAdditionalInfo ? ALL_FIELDS : DEFAULT_FIELDS)
                                        .Select(pi => new Field(pi))
                                        .ToArray();

            // Set up the SearchRequest to send to elasticsearch.
            Indices index = Indices.Index(new string[] { this._apiOptions.AliasName });
            Types   types = Types.Type(new string[] { "terms" });

            // Figure out the specific term subquery based on the match type.
            QueryBase termSubquery;

            switch (matchType)
            {
            case MatchType.Begins: termSubquery = new PrefixQuery {
                    Field = "term_name", Value = query
            }; break;

            case MatchType.Contains: termSubquery = new MatchQuery {
                    Field = "term_name._contain", Query = query
            }; break;

            case MatchType.Exact: termSubquery = new TermQuery {
                    Field = "term_name", Value = query
            }; break;

            default:
                throw new ArgumentException($"Uknown matchType value '${matchType}'.");
            }

            SearchRequest request = new SearchRequest(index, types)
            {
                Query = new TermQuery {
                    Field = "language", Value = language.ToString()
                } &&
                new TermQuery {
                    Field = "audience", Value = audience.ToString()
                } &&
                new TermQuery {
                    Field = "dictionary", Value = dictionary.ToString()
                } &&
                termSubquery
                ,
                Sort = new List <ISort>
                {
                    new SortField {
                        Field = "term_name"
                    }
                },
                Size   = size,
                From   = from,
                Source = new SourceFilter
                {
                    Includes = requestedESFields
                }
            };

            ISearchResponse <GlossaryTerm> response = null;

            try
            {
                response = await _elasticClient.SearchAsync <GlossaryTerm>(request);
            }
            catch (Exception ex)
            {
                String msg = $"Could not search dictionary '{dictionary}', audience '{audience}', language '{language}', query '{query}', size '{size}', from '{from}'.";
                _logger.LogError($"Error searching index: '{this._apiOptions.AliasName}'.");
                _logger.LogError(msg, ex);
                throw new APIErrorException(500, msg);
            }

            if (!response.IsValid)
            {
                String msg = $"Invalid response when searching for dictionary '{dictionary}', audience '{audience}', language '{language}', query '{query}', size '{size}', from '{from}'.";
                _logger.LogError(msg);
                throw new APIErrorException(500, "errors occured");
            }

            GlossaryTermResults glossaryTermResults = new GlossaryTermResults();

            if (response.Total > 0)
            {
                // Build the array of glossary terms for the returned results.
                List <GlossaryTerm> termResults = new List <GlossaryTerm>();
                foreach (GlossaryTerm res in response.Documents)
                {
                    termResults.Add(res);
                }

                glossaryTermResults.Results = termResults.ToArray();

                // Add the metadata for the returned results
                glossaryTermResults.Meta = new ResultsMetadata()
                {
                    TotalResults = (int)response.Total,
                    From         = from
                };
            }
            else if (response.Total == 0)
            {
                // Add the defualt value of empty GlossaryTerm list.
                glossaryTermResults.Results = new GlossaryTerm[] {};

                // Add the metadata for the returned results
                glossaryTermResults.Meta = new ResultsMetadata()
                {
                    TotalResults = (int)response.Total,
                    From         = from
                };
            }

            return(glossaryTermResults);
        }
Esempio n. 31
0
 public override Query VisitPrefixQuery(PrefixQuery prefixq)
 {
     _queryInfo.PrefixQueries++;
     return(base.VisitPrefixQuery(prefixq));
 }
Esempio n. 32
0
        public virtual void TestPrefix()
        {
            PrefixQuery expected = new PrefixQuery(new Term("field", "foobar"));

            assertEquals(expected, Parse("foobar*"));
        }
        public static MARC_Record Get_Record_By_Primary_Identifier(string Primary_Identifier, Z3950_Endpoint Z3950_Server, out string Message, Record_Character_Encoding encoding)
        {
            // Initially set the message to empty
            Message = String.Empty;

            // http://jai-on-asp.blogspot.com/2010/01/z3950-client-in-cnet-using-zoomnet-and.html
            // http://www.indexdata.com/yaz/doc/tools.html#PQF
            // http://www.loc.gov/z3950/agency/defns/bib1.html
            // http://www.assembla.com/code/wasolic/subversion/nodes/ZOOM.NET
            // http://lists.indexdata.dk/pipermail/yazlist/2007-June/002080.html
            // http://fclaweb.fcla.edu/content/z3950-access-aleph

            const string prefix = "@attrset Bib-1 @attr 1=12 ";

            try
            {
                IConnection  connection; //	zoom db connector
                IPrefixQuery query;      //	zoom query
                IRecord      record;     //	one zoom record
                IResultSet   records;    //	collection of records

                //	allocate MARC tools
                MARC21_Exchange_Format_Parser parser = new MARC21_Exchange_Format_Parser();

                //	establish connection
                connection = new Connection(Z3950_Server.URI, Convert.ToInt32(Z3950_Server.Port));
                connection.DatabaseName = Z3950_Server.Database_Name;

                // Any authentication here?
                if (Z3950_Server.Username.Length > 0)
                {
                    connection.Username = Z3950_Server.Username;
                }
                if (Z3950_Server.Password.Length > 0)
                {
                    connection.Password = Z3950_Server.Password;
                }

                // Set to USMARC
                connection.Syntax = RecordSyntax.USMARC;

                //	call the Z39.50 server
                query   = new PrefixQuery(prefix + Primary_Identifier);
                records = connection.Search(query);

                // If the record count is not one, return a message
                if (records.Count != 1)
                {
                    if (records.Count == 0)
                    {
                        Message = "ERROR: No matching record found in Z39.50 endpoint";
                    }
                    else
                    {
                        Message = "ERROR: More than one matching record found in Z39.50 endpoint by primary identifier";
                    }
                    return(null);
                }

                //	capture the byte stream
                record = records[0];
                MemoryStream ms = new MemoryStream(record.Content);

                //	display while debugging
                //MessageBox.Show(Encoding.UTF8.GetString(record.Content));

                try
                {
                    //	feed the record to the parser and add the 955
                    MARC_Record marcrec = parser.Parse(ms, encoding);
                    parser.Close();
                    return(marcrec);
                }
                catch (Exception error)
                {
                    Message = "ERROR: Unable to parse resulting record into the MARC Record structure!\n\n" + error.Message;
                    return(null);
                    //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML.");
                    //Trace.WriteLine("Could not convert item " + Primary_Identifier   + " to MARCXML.");
                    //Trace.WriteLine("Error details: " + error.Message);
                }
            }
            catch (Exception error)
            {
                if (error.Message.IndexOf("The type initializer for 'Zoom.Net.Yaz") >= 0)
                {
                    Message = "ERROR: The Z39.50 libraries did not correctly initialize.\n\nThese libraries do not currently work in 64-bit environments.";
                }
                else
                {
                    Message = "ERROR: Unable to connect and search provided Z39.50 endpoint.\n\n" + error.Message;
                }

                return(null);
                //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML.");
                //Trace.WriteLine("Could not convert item " + Primary_Identifier   + " to MARCXML.");
                //Trace.WriteLine("Error details: " + error.Message);
            }
        }
Esempio n. 34
0
        public Report SearchBook(int catalogueID, string sysno, string isbn, string barcode)
        {
            if (catalogueID == 0) throw new ArgumentNullException("catalogueID");
            if (String.IsNullOrEmpty(sysno) && String.IsNullOrEmpty(isbn) && String.IsNullOrEmpty(barcode))
                throw new ArgumentNullException("sysno+isbn+barcode");

            Report report = new Report(catalogueID, sysno, isbn, barcode);

            try
            {
                Catalogue catalogue = CatalogueComponent.Instance.GetByID(catalogueID);

                if (catalogue != null)
                {
                    IConnection connection = ConnectionFactory.Create(catalogue.ZServerUrl, catalogue.ZServerPort.Value);
                    connection.DatabaseName = catalogue.DatabaseName;
                    connection.Syntax = RecordSyntax.USMARC;
                    connection.Charset = catalogue.Charset;
                    connection.UserName = catalogue.UserName;
                    connection.Password = catalogue.Password;
                    connection.Timeout = 5;

                    RecordCharset charset = RecordCharset.Default;
                    Enum.TryParse(catalogue.Charset, true, out charset);

                    IPrefixQuery prefixQuery = new PrefixQuery();
                    if (!String.IsNullOrEmpty(sysno))
                    {
                        prefixQuery.Add(UseAttribute.LocalNumber_12, sysno);
                    }
                    if (!String.IsNullOrEmpty(isbn))
                    {
                        prefixQuery.Add(UseAttribute.ISBN_7, isbn);
                    }
                    if (!String.IsNullOrEmpty(barcode))
                    {
                        prefixQuery.Add(UseAttribute.Barcode_1063, barcode);
                    }
                    IResultSet resultSet = connection.Search(prefixQuery);

                    report.Size = (int)resultSet.Size;
                    report.XmlData = resultSet.GetData(RecordFormat.XML, charset, MAX_RECORORD_COUNT);
                    report.Errors = ((report.Size > MAX_RECORORD_COUNT) ? String.Format("Příliš mnoho záznamů ({0}), upřesněte dotaz.", report.Size) : null);
                    report.Success = true;
                }
                else
                {
                    report.Errors = String.Format("Katalog ID={0} neexistuje, nelze vyhledat čárový kód.", catalogueID);
                }
            }
            catch (Bib1Exception be)
            {
                report.Errors = String.Format("{0}: {1}. ", be.DiagnosticCode, be.Message) + (be.InnerException != null ? be.InnerException.Message : "");
            }
            catch (Exception ex)
            {
                report.Errors = ex.Message + (ex.InnerException != null ? String.Format(". {0}", ex.InnerException.Message) : "");
            }

            return report;
        }
        public void Boost_WhenBoostIsLessThanZero_ThrowsArgumentOutOfRangeException()
        {
            var query = new PrefixQuery("prefix");

            Assert.Throws <ArgumentOutOfRangeException>(() => query.Boost(-.1));
        }
Esempio n. 36
0
        public static Query GenerateQuery(string fieldName, string query, Analyzer analyzer)
        {
            if (query == null)
            {
                return(null);
            }

            var resultQuery = new BooleanQuery();
            var phraseQuery = new PhraseQuery {
                Slop = 0
            };

            //var phraseQueryTerms = new List<Term>();

            //not much to search, only do exact match
            if (query.Length < 4)
            {
                phraseQuery.Add(new Term(fieldName, query));

                resultQuery.Add(phraseQuery, Occur.MUST);
                return(resultQuery);
            }

            //add phrase match with boost, we will add the terms to the phrase below
            phraseQuery.Boost = 20;
            resultQuery.Add(phraseQuery, Occur.SHOULD);

            var tokenStream   = analyzer.TokenStream("SearchText", new StringReader(query));
            var termAttribute = tokenStream.AddAttribute <ITermAttribute>();

            while (tokenStream.IncrementToken())
            {
                var term = termAttribute.Term;

                //phraseQueryTerms.Add(new Term(fieldName, term));
                //phraseQuery.Add(new[] { new Term(fieldName, term) });
                phraseQuery.Add(new Term(fieldName, term));

                var exactMatch = new TermQuery(new Term(fieldName, term));

                //if the term is larger than 3, we'll do both exact match and wildcard/prefix
                if (term.Length >= 3)
                {
                    var innerQuery = new BooleanQuery();

                    //add exact match with boost
                    exactMatch.Boost = 10;
                    innerQuery.Add(exactMatch, Occur.SHOULD);

                    //add wildcard
                    var pq = new PrefixQuery(new Term(fieldName, term));
                    //needed so that wildcard searches will return a score
                    pq.RewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE; //new ErrorCheckingScoringBooleanQueryRewrite();
                    innerQuery.Add(pq, Occur.SHOULD);

                    resultQuery.Add(innerQuery, Occur.MUST);
                }
                else
                {
                    resultQuery.Add(exactMatch, Occur.MUST);
                }
            }

            //phraseQuery.Add(phraseQueryTerms.ToArray());

            return(resultQuery.Clauses.Count > 0 ? resultQuery : null);
        }
Esempio n. 37
0
        public virtual void TestCrazyPrefixes2()
        {
            Query expected = new PrefixQuery(new Term("field", "st*ar\\*"));

            assertEquals(expected, Parse("st*ar\\\\**"));
        }
Esempio n. 38
0
        public List <Guid> Search(string searchQuery, int PageSize, int PageIndex, LuceneSearcherType SearchType, LuceneSortField SortField, bool hasPhoto)
        {
            try
            {
                var photoQuery = new TermQuery(new Term("HasPhoto", hasPhoto.ToString()));
                var results    = new List <ScoreDoc>();
                var res        = new List <Guid>();
                // validation
                if (string.IsNullOrEmpty(searchQuery) || string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", "")))
                {
                    return(new List <Guid>());
                }
                var Content = searchQuery.Trim();

                if (SortField != LuceneSortField.PubDate)
                {
                    sort = new Sort(new SortField(SortField.ToString(), System.Globalization.CultureInfo.CurrentCulture, true));
                }

                // set up lucene searcher
                //using (var _searcher = new IndexSearcher(_directory, false))
                //{
                var analyzer = new StandardAnalyzer(Currentversion);

                if (SearchType == LuceneSearcherType.Key)
                {
                    if (Content.Contains("|"))
                    {
                        var fields      = new[] { "Title", "Description" };
                        var queryParser = new MultiFieldQueryParser(Currentversion, fields, analyzer);
                        //var query = queryParser.Parse(Content.Replace('|', ' '));
                        var          query     = queryParser.Parse(Content.Replace(" ", "+").Replace('|', ' ') + (hasPhoto ? ",HasPhoto:True" : ""));
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        if (results.Count < PageSize)
                        {
                            QueryParser oParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", analyzer);
                            string      desc = "", finalQuery = "";
                            desc       = " OR (Description:" + Content + ")";
                            finalQuery = "(" + Content + desc + ")";
                            query      = oParser.Parse(finalQuery);
                            results    = _searcher.Search(query, null, (PageIndex + 1) * PageSize, Sort.RELEVANCE).ScoreDocs.ToList();
                        }
                    }
                    else if (Content.Contains(" "))
                    {
                        var fields      = new[] { "Title", "Description" };
                        var queryParser = new MultiFieldQueryParser(Currentversion, fields, analyzer);
                        //var query = queryParser.Parse("\"" + Content + "\"");
                        var          query     = queryParser.Parse("\"" + Content + "\"");
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        if (results.Count < PageSize)
                        {
                            QueryParser oParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", analyzer);
                            string      desc = "", finalQuery = "";
                            desc       = " OR (Description:" + Content + ")";
                            finalQuery = "(" + Content + desc + ")";
                            query      = oParser.Parse(finalQuery);
                            results    = _searcher.Search(query, null, (PageIndex + 1) * PageSize, Sort.RELEVANCE).ScoreDocs.ToList();
                        }
                    }
                    else
                    {
                        var          query     = new PrefixQuery(new Term("Title", Content.ToString()));
                        BooleanQuery innerExpr = new BooleanQuery();
                        innerExpr.Add(query, Occur.MUST);
                        if (hasPhoto)
                        {
                            innerExpr.Add(photoQuery, Occur.MUST);
                        }

                        results = _searcher.Search(innerExpr, null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                        //if (results.Count < (PageIndex + 1) * PageSize)
                        //    results = _searcher.Search(new PrefixQuery(new Term("Description", Content.ToString())), null, (PageIndex + 1) * PageSize, sort).ScoreDocs.ToList();
                    }
                }
                var resultsId = results.Skip(PageIndex * PageSize).Take(PageSize);
                foreach (var doc in resultsId)
                {
                    var tempdoc = _searcher.Doc(doc.Doc);
                    res.Add(Guid.Parse(tempdoc.Get("FeedItemId")));
                }

                return(res);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("write.lock"))
                {
                    try
                    {
                        var lockFilePath = Path.Combine(LuceneDir, "write.lock");
                        if (File.Exists(lockFilePath))
                        {
                            File.Delete(lockFilePath);
                        }
                    }
                    catch { }
                }
                return(new List <Guid>());
            }
        }
Esempio n. 39
0
        /// <summary>
        /// Extracts all <see cref="MultiTermQuery"/>s for <paramref name="field"/>, and returns equivalent
        /// automata that will match terms.
        /// </summary>
        internal static CharacterRunAutomaton[] ExtractAutomata(Query query, string field)
        {
            List <CharacterRunAutomaton> list = new List <CharacterRunAutomaton>();

            if (query is BooleanQuery)
            {
                BooleanClause[] clauses = ((BooleanQuery)query).GetClauses();
                foreach (BooleanClause clause in clauses)
                {
                    if (!clause.IsProhibited)
                    {
                        list.AddAll(Arrays.AsList(ExtractAutomata(clause.Query, field)));
                    }
                }
            }
            else if (query is DisjunctionMaxQuery)
            {
                foreach (Query sub in ((DisjunctionMaxQuery)query).Disjuncts)
                {
                    list.AddAll(Arrays.AsList(ExtractAutomata(sub, field)));
                }
            }
            else if (query is SpanOrQuery)
            {
                foreach (Query sub in ((SpanOrQuery)query).GetClauses())
                {
                    list.AddAll(Arrays.AsList(ExtractAutomata(sub, field)));
                }
            }
            else if (query is SpanNearQuery)
            {
                foreach (Query sub in ((SpanNearQuery)query).GetClauses())
                {
                    list.AddAll(Arrays.AsList(ExtractAutomata(sub, field)));
                }
            }
            else if (query is SpanNotQuery)
            {
                list.AddAll(Arrays.AsList(ExtractAutomata(((SpanNotQuery)query).Include, field)));
            }
            else if (query is SpanPositionCheckQuery)
            {
                list.AddAll(Arrays.AsList(ExtractAutomata(((SpanPositionCheckQuery)query).Match, field)));
            }
            else if (query is ISpanMultiTermQueryWrapper)
            {
                list.AddAll(Arrays.AsList(ExtractAutomata(((ISpanMultiTermQueryWrapper)query).WrappedQuery, field)));
            }
            else if (query is AutomatonQuery)
            {
                AutomatonQuery aq = (AutomatonQuery)query;
                if (aq.Field.Equals(field, StringComparison.Ordinal))
                {
                    list.Add(new CharacterRunAutomatonToStringAnonymousHelper(aq.Automaton, () => aq.ToString()));
                }
            }
            else if (query is PrefixQuery)
            {
                PrefixQuery pq     = (PrefixQuery)query;
                Term        prefix = pq.Prefix;
                if (prefix.Field.Equals(field, StringComparison.Ordinal))
                {
                    list.Add(new CharacterRunAutomatonToStringAnonymousHelper(
                                 BasicOperations.Concatenate(BasicAutomata.MakeString(prefix.Text()), BasicAutomata.MakeAnyString()),
                                 () => pq.ToString()));
                }
            }
            else if (query is FuzzyQuery)
            {
                FuzzyQuery fq = (FuzzyQuery)query;
                if (fq.Field.Equals(field, StringComparison.Ordinal))
                {
                    string utf16    = fq.Term.Text();
                    int[]  termText = new int[utf16.CodePointCount(0, utf16.Length)];
                    for (int cp, i = 0, j = 0; i < utf16.Length; i += Character.CharCount(cp))
                    {
                        termText[j++] = cp = utf16.CodePointAt(i);
                    }
                    int    termLength             = termText.Length;
                    int    prefixLength           = Math.Min(fq.PrefixLength, termLength);
                    string suffix                 = UnicodeUtil.NewString(termText, prefixLength, termText.Length - prefixLength);
                    LevenshteinAutomata builder   = new LevenshteinAutomata(suffix, fq.Transpositions);
                    Automaton           automaton = builder.ToAutomaton(fq.MaxEdits);
                    if (prefixLength > 0)
                    {
                        Automaton prefix = BasicAutomata.MakeString(UnicodeUtil.NewString(termText, 0, prefixLength));
                        automaton = BasicOperations.Concatenate(prefix, automaton);
                    }
                    list.Add(new CharacterRunAutomatonToStringAnonymousHelper(automaton, () => fq.ToString()));
                }
            }
            else if (query is TermRangeQuery)
            {
                TermRangeQuery tq = (TermRangeQuery)query;
                if (tq.Field.Equals(field, StringComparison.Ordinal))
                {
                    // this is *not* an automaton, but its very simple
                    list.Add(new SimpleCharacterRunAutomatonAnonymousHelper(BasicAutomata.MakeEmpty(), tq));
                }
            }
            return(list.ToArray(/*new CharacterRunAutomaton[list.size()]*/));
        }
Esempio n. 40
0
        private void GetDataFromWeb()
        {
            try
            {
                var connection = new Connection("aleph.nkp.cz", 9991)
                {
                    DatabaseName = "SKC-UTF", //"CNB-UTF",// "SKC-UTF", //"NKC-UTF",
                    Syntax       = Zoom.Net.RecordSyntax.XML
                };
                connection.Connect();

                // Declare the query either in PQF or CQL according to whether the Z39.50 implementation at the endpoint support it.
                // The following query is in PQF format
                //var query = "@attr 1=4 krakatit";
                var query = "";
                if (txtISBN.Text != "")
                {
                    query = "@attr 1=7 \"" + txtISBN.Text + "\"";
                }
                else if (txtName.Text != "" && txtAuthorSurname.Text == "")
                {
                    query = "@attr 1=4 @attr 3=3 \"" + global.RemoveDiacritics(txtName.Text) + "\" ";
                }
                else if (txtName.Text == "" && global.RemoveDiacritics(txtAuthorSurname.Text) != "")
                {
                    query = "@attr 1=1003 @attr 3=3 \"" + global.RemoveDiacritics(txtAuthorSurname.Text) + "\"";
                }
                else if (txtName.Text != "" && txtAuthorSurname.Text != "")
                {
                    query = "@and @attr 1=4 @attr 3=3 \"" + global.RemoveDiacritics(txtName.Text) + "\" @attr 1=1003 @attr 3=3 \"" + global.RemoveDiacritics(txtAuthorSurname.Text) + "\"";
                }
                else
                {
                    Dialogs.ShowWar(Lng.Get("NoFindBookData", "No data (ISBN / Title) to booking search!"), Lng.Get("Warning"));
                    return;
                }
                var q = new PrefixQuery(query);

                // Get the search results in binary format
                // Note that each result is in XML format
                var results = connection.Search(q);

                if (results.Count > 0)
                {
                    var             result = results[0];
                    string          text   = Encoding.UTF8.GetString(result.Content);
                    List <bookItem> list   = XmlToBook(text);
                    Files.SaveFile("res.xml", text);

                    if (list.Count > 0)
                    {
                        bookItem itm = list[0];

                        // ----- Get Cober from "Obalkyknih.cz" -----
                        string urlAddress = "https://obalkyknih.cz/view?isbn=" + itm.ISBN.Replace("-", "");

                        HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(urlAddress);
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            Stream       receiveStream = response.GetResponseStream();
                            StreamReader readStream    = null;

                            if (response.CharacterSet == null)
                            {
                                readStream = new StreamReader(receiveStream);
                            }
                            else
                            {
                                readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                            }

                            string data = readStream.ReadToEnd();

                            int pos = data.IndexOf("table class=\"detail\"");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf("<a href=", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 9;
                                    int posStop = data.IndexOf("\"", posStart);
                                    if (posStop >= 0)
                                    {
                                        string strImg = data.Substring(posStart, posStop - posStart);
                                        using (WebClient client = new WebClient())
                                        {
                                            string path = Path.GetTempFileName();
                                            client.DownloadFile(strImg, path);
                                            itm.coverPath = path;
                                        }
                                    }
                                }
                            }

                            // ----- Anotation -----
                            pos = data.IndexOf("<h3>Anotace</h3>");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf("<p>", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 3;
                                    int posStop = data.IndexOf("</p>", posStart);
                                    if (posStop >= 0)
                                    {
                                        itm.Content = data.Substring(posStart, posStop - posStart).Trim();
                                    }
                                }
                            }
                            response.Close();
                            readStream.Close();
                        }

                        // ----- Get from databazeknih -----
                        urlAddress = "https://www.databazeknih.cz/search?q=" + itm.ISBN.Replace("-", "");

                        request  = (HttpWebRequest)WebRequest.Create(urlAddress);
                        response = (HttpWebResponse)request.GetResponse();
                        itm.URL  = response.ResponseUri.ToString();
                        request  = (HttpWebRequest)WebRequest.Create(response.ResponseUri + "?show=binfo");
                        response = (HttpWebResponse)request.GetResponse();

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            Stream       receiveStream = response.GetResponseStream();
                            StreamReader readStream    = null;

                            if (response.CharacterSet == null)
                            {
                                readStream = new StreamReader(receiveStream);
                            }
                            else
                            {
                                readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                            }

                            string data = readStream.ReadToEnd();

                            int pos = data.IndexOf("'bpoints'");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf(">", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 1;
                                    int posStop = data.IndexOf("<", posStart);
                                    if (posStop >= 0)
                                    {
                                        itm.Rating = data.Substring(posStart, posStop - posStart - 1);
                                    }
                                }
                            }

                            pos = data.IndexOf("href='zanry");
                            while (pos >= 0)
                            {
                                int posStart = data.IndexOf(">", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 1;
                                    int posStop = data.IndexOf("<", posStart);
                                    if (posStop >= 0)
                                    {
                                        if (itm.Genres != "")
                                        {
                                            itm.Genres += ", ";
                                        }
                                        itm.Genres += data.Substring(posStart, posStop - posStart);
                                        pos         = data.IndexOf("href='zanry", posStop);
                                    }
                                }
                                else
                                {
                                    pos = -1;
                                }
                            }

                            pos = data.IndexOf("href='prekladatele");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf(">", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 1;
                                    int posStop = data.IndexOf("<", posStart);
                                    if (posStop >= 0)
                                    {
                                        itm.Translator = data.Substring(posStart, posStop - posStart);
                                    }
                                }
                            }

                            pos = data.IndexOf("href='serie");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf(">", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 1;
                                    int posStop = data.IndexOf("<", posStart);
                                    if (posStop >= 0)
                                    {
                                        itm.Series = data.Substring(posStart, posStop - posStart);

                                        pos = data.IndexOf("class='info'", posStop);
                                        if (pos >= 0)
                                        {
                                            posStart = data.IndexOf(">", pos);
                                            if (posStart >= 0)
                                            {
                                                posStart += 1;
                                                posStop   = data.IndexOf("<", posStart);
                                                if (posStop >= 0)
                                                {
                                                    itm.SeriesNum = Conv.ToNumber(data.Substring(posStart, posStop - posStart)).ToString();
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            pos = data.IndexOf("Originální název:");
                            if (pos >= 0)
                            {
                                int posStart = data.IndexOf("<h4>", pos);
                                if (posStart >= 0)
                                {
                                    posStart += 4;
                                    int posStop = data.IndexOf("</h4>", posStart);
                                    if (posStop >= 0)
                                    {
                                        itm.OrigTitle = data.Substring(posStart, posStop - posStart);
                                        posStart      = itm.OrigTitle.IndexOf("(");
                                        if (posStart >= 0)
                                        {
                                            posStart += 1;
                                            posStop   = itm.OrigTitle.IndexOf(")", posStart);
                                            if (posStop >= 0)
                                            {
                                                itm.OrigYear  = itm.OrigTitle.Substring(posStart, posStop - posStart);
                                                itm.OrigTitle = itm.OrigTitle.Substring(0, posStart - 1).Trim();
                                            }
                                        }
                                    }
                                }
                            }

                            response.Close();
                            readStream.Close();
                        }

                        FillByBookItems(itm);
                    }
                }
                else
                {
                    Dialogs.ShowWar(Lng.Get("BookNotFound", "Book not found!"), Lng.Get("Warning"));
                }
            }
            catch (Exception Err)
            {
                Dialogs.ShowErr(Lng.Get("ServerError", "Cannot connet to server!") + " (" + Err.Message + ")", Lng.Get("Warning"));
            }
        }