Example #1
0
 public void InsertFreeTextFacet()
 {
     var facet = FacetSetUpHelper.CreateFreeTextFacet();
     facet = new Facets().GetById(facet.Id);
     Assert.AreNotEqual(0, facet.Id);
     Assert.AreEqual(FacetType.FreeText, facet.Type);
     Assert.AreEqual("FreeTextTest", facet.Name);
 }
Example #2
0
 public static Facet CreateFreeTextFacet()
 {
     var facets = new Facets(new DatabaseProvider());
     var facet = new Facet
                     {
                         Type = FacetType.FreeText,
                         Name = "FreeTextTest"
                     };
     var record = facets.Insert(facet);
     facet = record;
     return facet;
 }
Example #3
0
        public void GetListFacet()
        {
            var facet = FacetSetUpHelper.CreateListFacet();

            facet = new Facets().GetById(facet.Id);
            Assert.AreNotEqual(0, facet.Id);
            Assert.AreEqual(FacetType.List, facet.Type);
            Assert.AreEqual("ListTest", facet.Name);
            Assert.IsNotNull(facet.Values);
            Assert.AreEqual(1, facet.Values.Count(v => v.Value == "One"));
            Assert.AreEqual(1, facet.Values.Count(v => v.Value == "Two"));
            foreach (var value in facet.Values)
            {
                Assert.AreEqual(facet.Id, value.FacetId);
                Assert.AreNotEqual(0, value.Id);
            }
        }
Example #4
0
 public static Facet CreateListFacet()
 {
     var facets = new Facets(new DatabaseProvider());
     var facet = new Facet
                     {
                         Type = FacetType.List,
                         Name = "ListTest",
                         Values = new List<FacetValue>
                                      {
                                          new FacetValue {Value = "One"},
                                          new FacetValue {Value = "Two"},
                                      }
                     };
     var record = facets.Insert(facet);
     facet = record;
     return facet;
 }
Example #5
0
        public void AllFacets()
        {
            FacetSetUpHelper.CreateFreeTextFacet();
            FacetSetUpHelper.CreateListFacet();

            var facet = new Facets().GetAll().FirstOrDefault(f => f.Type == FacetType.FreeText);
            Assert.IsNotNull(facet);
            Assert.AreEqual("FreeTextTest", facet.Name);

            facet = new Facets().GetAll().FirstOrDefault(f => f.Type == FacetType.List);
            Assert.IsNotNull(facet);
            Assert.AreNotEqual(0, facet.Id);
            Assert.AreEqual(FacetType.List, facet.Type);
            Assert.AreEqual("ListTest", facet.Name);
            Assert.IsNotNull(facet.Values);
            Assert.AreEqual(1, facet.Values.Count(v => v.Value == "One"));
            Assert.AreEqual(1, facet.Values.Count(v => v.Value == "Two"));
            foreach (var value in facet.Values)
            {
                Assert.AreEqual(facet.Id, value.FacetId);
                Assert.AreNotEqual(0, value.Id);
            }
        }
Example #6
0
        // GET: /<controller>/
        public IActionResult ProdList(string pg, string cat_main, string cat_sub, string key1, string citykey, string pricerange,
                                      string datefilter, string budget, string[] duration, string[] guidelang)
        {
            //紀錄目前頁次
            int active_page_idx = Convert.ToInt32(pg ?? "1");

            //紀緣分割後的durations

            if (duration != null)
            {
                _durations = duration.Select(s => s.Replace("-", ",")).ToArray();
            }

            //B2d分銷商資料
            var aesUserData = User.Identities.SelectMany(i => i.Claims.Where(c => c.Type == ClaimTypes.UserData).Select(c => c.Value)).FirstOrDefault();
            var UserData    = JsonConvert.DeserializeObject <B2dAccount>(AesCryptHelper.aesDecryptBase64(aesUserData, Website.Instance.AesCryptKey));
            //分銷商語系
            string locale = UserData.LOCALE;
            //分銷商幣別
            string currency = UserData.CURRENCY;
            //取得可售商品之國家&城市
            var countries = CountryRepostory.GetCountries(locale);

            Dictionary <string, string> uikey = CommonRepostory.getuiKey(RedisHelper, UserData.LOCALE);
            ProdTitleModel title = JsonConvert.DeserializeObject <ProdTitleModel>(JsonConvert.SerializeObject(uikey));

            List <ProductBaseModel> prodList = null;

            //查詢條件不為空值才查
            if (!string.IsNullOrEmpty(key1) || !string.IsNullOrEmpty(citykey))
            {
                int    offset      = (active_page_idx - 1) * 20; //計算從第幾筆開始
                int    size        = 20;                         //分頁筆數
                int    total_count = 0;                          //out 參數 (接收返回的total_count參數)
                int    total_pages = 0;                          //out 參數 (接收返回的total_pages參數)
                Facets facets      = new Facets();               //out 參數 (接收返回的facets參數)
                Stats  stats       = new Stats();                //out 參數 (接收返回的stats參數)

                //取得資料
                prodList = SearchRepostory.GetProduct(locale, currency, key1, citykey, offset, size, datefilter, budget, _durations, guidelang, cat_main, cat_sub,
                                                      out total_count, out total_pages, out stats, out facets);

                List <CountryInfo> country = new List <CountryInfo>();
                country = CountryRepostory.GetCountries(key1, citykey, locale);

                //當城市或國家不為空,則清除查詢關鍵字
                if (country != null && country.Count() > 0)
                {
                    //已選城市的判斷
                    if (!string.IsNullOrEmpty(citykey))
                    {
                        var    cd3    = country.SelectMany(co => co.Cities.Where(ci => ci.IsSelceted == true).ToList()).ToList().First();
                        string citycd = cd3.CityCode;
                        ViewData["citycode"] = citycd;
                    }
                    else
                    {
                        var city = country.FirstOrDefault().Cities.FirstOrDefault();
                        city.IsSelceted      = true;
                        ViewData["citycode"] = city.CityCode;
                    }
                    key1 = null;
                }

                //傳入VIEW的參數
                ViewData["total_count"]     = total_count;
                ViewData["active_page_idx"] = active_page_idx;
                ViewData["total_pages"]     = total_pages;
                ViewData["key"]             = key1;
                ViewData["facets"]          = facets;
                ViewData["duration"]        = duration;
                ViewData["guidelang"]       = guidelang;
                ViewData["stats"]           = stats;
                ViewData["pricerange"]      = !string.IsNullOrEmpty(pricerange) ? pricerange : string.Format("{0};{1}", stats.price.min, stats.price.max);
                ViewData["budget"]          = !string.IsNullOrEmpty(budget) ? budget : string.Format("{0};{1}", stats.price.min, stats.price.max);
                ViewData["country"]         = country;
                ViewData["prodTitle"]       = title;
            }

            return(View(prodList));
        }
 /// <summary>
 /// Adds the specified facet.
 /// </summary>
 /// <param name="facet">The facet.</param>
 internal void Add(SearchFacet facet)
 {
     Facets.Add(facet);
 }
Example #8
0
        /// <summary>
        /// Search facet content in the existing index.
        /// </summary>
        /// <param name="text">The text to search for.</param>
        /// <param name="indexFields">The array of index fields to search in.</param>
        /// <param name="facetPaths">The array of facet paths to perform a drill down search on.</param>
        /// <param name="numberToReturn">The maximum number of documents to return.</param>
        /// <returns>The facet document.</returns>
        /// <remarks>Use wildcard chars ('*', '?', '\'), logical ('AND', 'OR'), Quoted exact phrase ("search this").</remarks>
        public Nequeo.Search.Engine.FacetDocument SearchFacetDocument(string text, FacetData.IndexField[] indexFields, FacetPath[] facetPaths, int numberToReturn = Int32.MaxValue)
        {
            Nequeo.Search.Engine.FacetDocument documents = new FacetDocument();
            documents.TotalHits = 0;

            try
            {
                // If text exists.
                if (!String.IsNullOrEmpty(text))
                {
                    // Load the searcher.
                    Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(_reader);
                    string         searchFieldName           = "facetcontent";
                    Query          query      = null;
                    DrillDownQuery queryFacet = null;
                    TopDocs        results    = null;

                    // Build the facet configuration information.
                    FacetsConfig config = new FacetsConfig();

                    // Add the config.
                    foreach (FacetData.IndexField item in indexFields)
                    {
                        config.SetIndexFieldName(item.DimensionName, item.IndexFieldName);
                    }

                    // Get bytes
                    char[] textArray = text.ToCharArray();

                    // Search logical.
                    if (text.Contains("AND") || text.Contains("OR"))
                    {
                        // Create the query.
                        query = CreateLogicalQuery(text, searchFieldName);
                    }
                    else if (textArray[0].Equals('"') && textArray[textArray.Length - 1].Equals('"'))
                    {
                        // Create the query.
                        query = CreateQuotedQuery(new string(textArray, 1, textArray.Length - 2), searchFieldName);
                    }
                    else
                    {
                        // Create the query.
                        query = CreateBoolenQuery(text, BooleanClause.Occur.SHOULD, searchFieldName);
                    }

                    // Create the facet query.
                    queryFacet = new DrillDownQuery(config, query);
                    foreach (FacetPath facetPath in facetPaths)
                    {
                        // Add the path.
                        queryFacet.Add(facetPath.DimensionName, facetPath.Path);
                    }

                    // The collector.
                    FacetsCollector collector = new FacetsCollector();

                    // Search.
                    if (queryFacet != null)
                    {
                        results = FacetsCollector.Search(searcher, queryFacet, numberToReturn, collector);
                    }

                    // Get the total number of results that was asked for.
                    int totalResult = ((results.ScoreDocs != null && results.ScoreDocs.Length > 0) ? results.ScoreDocs.Length : 0);

                    // If result found.
                    if (results != null && results.TotalHits > 0)
                    {
                        List <TextDataResult>     textDataResults = new List <TextDataResult>();
                        List <FileDocumentResult> fileDocResults  = new List <FileDocumentResult>();

                        List <FacetPathResult>       facetPathResults = new List <FacetPathResult>();
                        IDictionary <string, Facets> facetsMap        = new Dictionary <string, Facets>();

                        // Add the facet count.
                        foreach (FacetData.IndexField item in indexFields)
                        {
                            // Add the facet for each index field.
                            facetsMap[item.DimensionName] = GetTaxonomyFacetCounts(_facetReader, config, collector, item.IndexFieldName);
                        }

                        // Create the multi facet list.
                        foreach (FacetPath facetPath in facetPaths)
                        {
                            try
                            {
                                // Add the facets.
                                Facets facets = facetsMap.First(u => u.Key.ToLower().Contains(facetPath.DimensionName.ToLower())).Value;
                                float  number = facets.GetSpecificValue(facetPath.DimensionName, facetPath.Path);

                                // Add the path.
                                facetPathResults.Add(new FacetPathResult(facetPath.DimensionName, number, facetPath.Path));
                            }
                            catch { }
                        }

                        // For each document found.
                        for (int i = 0; i < totalResult; i++)
                        {
                            FileDocumentResult fileDocument = null;
                            TextDataResult     textData     = null;

                            int docID = results.ScoreDocs[i].Doc;
                            Lucene.Net.Documents.Document doc = searcher.Doc(docID);

                            try
                            {
                                // Get the data for each field.
                                IndexableField[] textNameFields = doc.GetFields("textname");

                                // If this field exists then text data.
                                if (textNameFields.Length > 0)
                                {
                                    // Assign the data to the text document.
                                    textData       = new TextDataResult();
                                    textData.Name  = textNameFields.Length > 0 ? textNameFields[0].StringValue : null;
                                    textData.Score = results.ScoreDocs[i].Score;
                                    textData.Doc   = docID;

                                    // Do not know if the text was stored.
                                    IndexableField[] textValueFields = doc.GetFields("textcomplete");
                                    textData.Text = textValueFields.Length > 0 ? textValueFields[0].StringValue : null;
                                }
                            }
                            catch { }

                            // If text data exists then add.
                            if (textData != null)
                            {
                                textDataResults.Add(textData);
                            }

                            try
                            {
                                // Get the data for each field.
                                IndexableField[] pathNameFields     = doc.GetFields("path");
                                IndexableField[] modifiedNameFields = doc.GetFields("modified");

                                // If this field exists then file document.
                                if (pathNameFields.Length > 0)
                                {
                                    // Assign the data to the path document.
                                    fileDocument          = new FileDocumentResult();
                                    fileDocument.Path     = pathNameFields.Length > 0 ? pathNameFields[0].StringValue : null;
                                    fileDocument.Modified = modifiedNameFields.Length > 0 ? modifiedNameFields[0].StringValue : null;
                                    fileDocument.Score    = results.ScoreDocs[i].Score;
                                    fileDocument.Doc      = docID;
                                }
                            }
                            catch { }

                            // If file data exists then add.
                            if (fileDocument != null)
                            {
                                fileDocResults.Add(fileDocument);
                            }
                        }

                        // Assign the facet document values.
                        documents.MaxScore            = results.MaxScore;
                        documents.TotalHits           = results.TotalHits;
                        documents.FacetPathResults    = facetPathResults.ToArray();
                        documents.TextDataResults     = textDataResults.ToArray();
                        documents.FileDocumentResults = fileDocResults.ToArray();
                    }
                }

                // Return the documents.
                return(documents);
            }
            catch (Exception)
            {
                throw;
            }
        }
		/// <summary>
		/// Gets the facet parameters.
		/// </summary>
		/// <param name="criteria">The criteria.</param>
		/// <returns></returns>
		protected virtual Facets<ESDocument> GetFacets(ISearchCriteria criteria)
		{
			// Now add facets
			var facetParams = new Facets<ESDocument>();
			foreach (var filter in criteria.Filters)
			{
				if (filter is AttributeFilter)
					AddFacetQueries(facetParams, filter.Key, ((AttributeFilter)filter).Values);
				else if (filter is RangeFilter)
					AddFacetQueries(facetParams, filter.Key, ((RangeFilter)filter).Values);
				else if (filter is PriceRangeFilter)
				{
					var currency = ((PriceRangeFilter)filter).Currency;
					if (currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
					{
						AddFacetQueries(facetParams, filter.Key, ((PriceRangeFilter)filter).Values, criteria);
					}
				}
			}

			return facetParams;
		}
Example #10
0
        /// <summary>
        /// Gets the product.
        /// </summary>
        /// <returns>The product.</returns>
        /// <param name="lang">Lang.</param>
        /// <param name="currency">Currency.</param>
        /// <param name="filter">Filter.</param>
        /// <param name="skip">Skip.</param>
        /// <param name="size">Size.</param>
        /// <param name="total_count">Total count.</param>
        /// <param name="total_pages">Total pages.</param>
        public static List <ProductBaseModel> GetProduct(string lang, string currency, string filter, string citykey,
                                                         int skip, int size, string datefilter, string budgetfilter, string[] durations, string[] guidelang, string cat_main, string cat_sub,
                                                         out int total_count, out int total_pages, out Stats stats, out Facets facets)
        {
            //連接WMS-API
            try
            {
                List <ProductBaseModel> pLst = new List <ProductBaseModel>();

                string jsonResult;

                //建立連線到WMS-API
                var _uri = "https://192.168.2.83:6001/api/Search";

                using (var handler = new HttpClientHandler())
                {
                    // Ignore Certificate Error!!
                    handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;

                    using (var client = new HttpClient(handler))
                    {
                        #region Uri with QueryStrings

                        /*
                         * {
                         *   "lang":"en",
                         *   "currency":"TWD",
                         *   "cat_keys":["TAG_1_3"],
                         *   "cat_main_keys":["TAG_1"],
                         *   "city_keys":["A01-001-00006"],
                         *   "count":"10",
                         *   "country_keys":["A01-001"],
                         *   "date_from":"20180101",
                         *   "date_to":"20181018",
                         *   "locale":"繁體中文",    //語系(WMS)
                         *   "price_from":"0",
                         *   "price_to":"1000",
                         *   "prod_ids":["20140","20159"],
                         *   "q":"水金九",
                         *   "sort":"PDESC",
                         *   "start":"0",
                         *   "stats":["price"]
                         * }
                         */

                        var query = new Dictionary <string, object>
                        {
                            ["lang"]     = lang,     //語系
                            ["currency"] = currency, //幣別
                            ["start"]    = skip,     //計算從第幾筆開始
                            ["count"]    = size,     //分頁筆數
                            ["q"]        = filter,   //查詢條件
                            //["city_key"] = citykey,
                            ["stats"]       = new string[] { "price" },
                            ["facets"]      = new string[] { "cat_main", "cat", "guide_lang" }, //, "total_time", "sale_dt"
                            ["company_xid"] = "1"
                        };

                        //城市
                        if (citykey != null && citykey.Length > 0)
                        {
                            query.Add("city_keys", new string[] { citykey });
                        }

                        #region 加入Filter

                        //日期起始
                        if (!string.IsNullOrEmpty(datefilter) && datefilter.IndexOf("-") != -1)
                        {
                            //將StarDate&EndDate分割,加入WMS-API(query)的查詢條件中
                            var daterange = datefilter.Split(new char[2] {
                                '-', '/'
                            });
                            query.Add("date_from", daterange[0] + daterange[1] + daterange[2]);
                            query.Add("date_to", daterange[3] + daterange[4] + daterange[5]);
                        }
                        //價格起始
                        if (!string.IsNullOrEmpty(budgetfilter) && budgetfilter.IndexOf(";") != -1)
                        {
                            var budget = budgetfilter.Split(new char[1] {
                                ';'
                            });
                            query.Add("price_from", budget[0]);
                            query.Add("price_to", budget[1]);
                        }
                        //行程時間
                        if (durations != null && durations.Length > 0)
                        {
                            query.Add("durations", durations);
                        }
                        //導覽語言
                        query.Add("guide_langs", guidelang);

                        //次分類
                        if (cat_sub != null && cat_sub.Length > 0)
                        {
                            query.Add("cat_keys", new string[] { cat_sub });
                        }
                        //主分類
                        if (cat_main != null && cat_main.Length > 0)
                        {
                            query.Add("cat_main_keys", new string[] { cat_main });
                        }

                        #endregion

                        //轉換JSON格式
                        var content = JsonConvert.SerializeObject(query);

                        #endregion Uri with QueryStrings

                        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _uri))
                        {
                            //request.Headers.Add("x-auth-key", "kkdaysearchapi_Rfd_fsg+x+TcJy");
                            request.Headers.Add("Accept", "application/json");
                            // Add body content
                            request.Content = new StringContent(
                                content,
                                Encoding.UTF8,
                                "application/json"
                                );

                            var response = client.SendAsync(request).Result;
                            jsonResult = response.Content.ReadAsStringAsync().Result;
                        }
                    }

                    //分析資料並轉換回ProductBaseModel串列
                    JObject jProdobj = JObject.Parse(jsonResult);

                    //查詢產品      (將OBJECT物件轉為LIST型態)
                    pLst = jProdobj["prods"].ToObject <List <ProductBaseModel> >();

                    //準備商品之國家&城市資料
                    List <Country> countries = new List <Country>();

                    foreach (var naToken in pLst)
                    {
                        var naVal = naToken.countries;

                        Country countryinfo = new Country()
                        {
                            id     = naVal.ToString(),
                            name   = naVal.ToString(),
                            cities = new List <City>()
                        };

                        foreach (var ctVal in naVal)
                        {
                            countryinfo.cities.Add(new City()
                            {
                                id   = ctVal.ToString(),
                                name = ctVal.ToString()
                            });
                        }

                        countries.Add(countryinfo);
                    }

                    facets = new Facets()
                    {
                        cat_main   = jProdobj["facets"]["cat_main"].ToObject <List <CatMain> >(),     //主分類
                        cat_sub    = jProdobj["facets"]["cat"].ToObject <List <CatSub> >(),           //次分類
                        guide_lang = jProdobj["facets"]["guide_lang"].ToObject <List <GuideLang> >(), //導覽語系
                        //total_time = jProdobj["facets"]["total_time"].ToObject<List<TotalTime>>()    //行程時間
                    };

                    stats = new Stats()
                    {
                        //價格條件
                        price = jProdobj["stats"]["price"].ToObject <Price>()
                    };

                    //查詢返迴總筆數 (將OBJECT物件轉為INT型態)
                    total_count = jProdobj["metadata"]["total_count"].ToObject <int>();

                    //計算總頁數
                    total_pages = (total_count / size) + ((total_count % size == 0) ? 0 : 1);
                }

                return(pLst);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Adds the facet queries.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="criteria">Search criteria.</param>
        private void AddFacetQueries(Facets<ESDocument> param, string fieldName, ISearchCriteria criteria)
        {
            var ffilter = new BoolFilter<ESDocument>();
            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(fieldName))
                {
                    var q = ElasticQueryHelper.CreateQuery(criteria, f);
                    ffilter.Must(ff => ff.Bool(bb => q));
                }
            }

            var facetFilter = new FacetFilter<ESDocument>();
            facetFilter.Bool(f => ffilter);

            param.Terms(t => t.FacetName(fieldName.ToLower()).Field(fieldName.ToLower()).FacetFilter(ff => facetFilter));
        }
Example #12
0
 public void CreateFacetOfArea()
 {
     Facets.Add(new FacetParameter {
         Name = "area", Value = AreaCode
     });
 }
Example #13
0
 public LinqToSolrFacet GetFacet(string propertyName)
 {
     return(Facets.FirstOrDefault(x => x.Name == propertyName));
 }
Example #14
0
        public LinqToSolrQuery AddFacet(LambdaExpression field)
        {
            Facets.Add(LinqToSolrFacet.Create(field));

            return(this);
        }
		/// <summary>
		/// Adds the facet queries.
		/// </summary>
		/// <param name="param">The param.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <param name="values">The values.</param>
		/// <param name="criteria">The criteria.</param>
		private void AddFacetQueries(Facets<ESDocument> param, string fieldName, IEnumerable<RangeFilterValue> values, ISearchCriteria criteria)
		{
			if (values == null)
				return;

			foreach (var value in values)
			{
				var query = ElasticQueryHelper.CreatePriceRangeFilter(criteria, fieldName, value);
				if (query != null)
				{
					param.FilterFacets(
						ff => ff.FacetName(String.Format("{0}-{1}", fieldName, value.Id)).Filter(f => f.Bool(b => query)));
				}
			}
		}
		/// <summary>
		/// Adds the facet queries.
		/// </summary>
		/// <param name="param">The param.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <param name="values">The values.</param>
		private void AddFacetQueries(Facets<ESDocument> param, string fieldName, IEnumerable<RangeFilterValue> values)
		{
			if (values == null)
				return;

			foreach (var value in values)
			{
				var filter = new FacetFilter<ESDocument>();
				filter.Range(r => r.IncludeLower(false).IncludeUpper().From(value.Lower).To(value.Upper));
				param.FilterFacets(ff => ff.FacetName(String.Format("{0}-{1}", fieldName, value.Id)).Filter(f => filter));
			}
		}
		/// <summary>
		/// Adds the facet queries.
		/// </summary>
		/// <param name="param">The param.</param>
		/// <param name="fieldName">Name of the field.</param>
		/// <param name="values">The values.</param>
		private void AddFacetQueries(Facets<ESDocument> param, string fieldName, AttributeFilterValue[] values)
		{
			if (values == null)
				return;

			param.Terms(t => t.FacetName(fieldName.ToLower()).Field(fieldName.ToLower()));
		}
        public virtual void TestDirectory()
        {
            Store.Directory indexDir = NewDirectory();
            Store.Directory taxoDir  = NewDirectory();
            IndexWriter     w        = new IndexWriter(indexDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)));
            var             tw       = new DirectoryTaxonomyWriter(taxoDir);

            // first empty commit
            w.Commit();
            tw.Commit();
            var          mgr    = new SearcherTaxonomyManager(indexDir, taxoDir, null);
            FacetsConfig config = new FacetsConfig();

            config.SetMultiValued("field", true);
            AtomicBoolean stop = new AtomicBoolean();

            // How many unique facets to index before stopping:
            int ordLimit = TEST_NIGHTLY ? 100000 : 6000;

            var indexer = new IndexerThread(w, config, tw, mgr, ordLimit, stop);

            indexer.Start();

            try
            {
                while (!stop.Get())
                {
                    SearcherAndTaxonomy pair = mgr.Acquire();
                    try
                    {
                        //System.out.println("search maxOrd=" + pair.taxonomyReader.getSize());
                        FacetsCollector sfc = new FacetsCollector();
                        pair.Searcher.Search(new MatchAllDocsQuery(), sfc);
                        Facets      facets = GetTaxonomyFacetCounts(pair.TaxonomyReader, config, sfc);
                        FacetResult result = facets.GetTopChildren(10, "field");
                        if (pair.Searcher.IndexReader.NumDocs > 0)
                        {
                            //System.out.println(pair.taxonomyReader.getSize());
                            Assert.True(result.ChildCount > 0);
                            Assert.True(result.LabelValues.Length > 0);
                        }

                        //if (VERBOSE) {
                        //System.out.println("TEST: facets=" + FacetTestUtils.toString(results.get(0)));
                        //}
                    }
                    finally
                    {
                        mgr.Release(pair);
                    }
                }
            }
            finally
            {
                indexer.Join();
            }

            if (VERBOSE)
            {
                Console.WriteLine("TEST: now stop");
            }

            IOUtils.Dispose(mgr, tw, w, taxoDir, indexDir);
        }
Example #19
0
 public void Clear()
 {
     Name = String.Empty;
     Facets.Clear();
     dimensions = null;
 }
Example #20
0
        /// <summary>
        /// Gets the prod list.
        /// </summary>
        /// <returns>The prod list.</returns>
        /// <param name="rq">Rq.</param>
        //1.取得商品列表
        public static SearchProductModel GetProdList(SearchRQModel rq)
        {
            SearchProductModel      prod = new SearchProductModel();
            List <ProductBaseModel> pLst = new List <ProductBaseModel>();

            try {
                JObject obj = SearchProxy.GetProdList(rq);

                Metadata md = new Metadata();

                DataModel.Discount.DiscountRuleModel disc = null;

                #region --1.取回傳資料是否成功的訊息、統計用資訊--

                md.result     = obj["metadata"]["status"].ToString();
                md.result_msg = obj["metadata"]["desc"].ToString();

                prod.metadata = md;
                #endregion

                //如果狀態為0000  表示搜尋沒問題  才繼續滿足商品欄位
                if (md.result == "0000")
                {
                    #region --2.從傑森物件取『商品列表』--
                    JArray jsonPlst = (JArray)obj["data"]["prods"];

                    int countBlackProd = 0;//計算黑名單筆數

                    for (int i = 0; i < jsonPlst.Count; i++)
                    {
                        var model = new ProductBaseModel();

                        string prod_no = jsonPlst[i]["id"].ToString();

                        //抓商品是否為黑名單
                        bool isBlack = DiscountRepository.GetProdBlackWhite(prod_no);

                        //表示該商品為白名單 需要綁入列表中 (黑名單的就不綁了)
                        if (isBlack != true)
                        {
                            model.prod_no           = Convert.ToInt32(prod_no);
                            model.prod_name         = jsonPlst[i]["name"].ToString();
                            model.b2d_price         = DiscountRepository.GetCompanyDiscPrice(Int64.Parse(rq.company_xid), (double)jsonPlst[i]["price"], prod_no, jsonPlst[i]["main_cat_key"].ToString(), ref disc); //分銷價
                            model.b2c_price         = (double)jsonPlst[i]["sale_price"];                                                                                                                            //直客價
                            model.display_ref_price = jsonPlst[i]["display_price"].ToString();

                            model.prod_currency   = jsonPlst[i]["currency"].ToString();
                            model.prod_img_url    = jsonPlst[i]["img_url"].ToString();
                            model.rating_count    = (int)jsonPlst[i]["rating_count"];
                            model.avg_rating_star = (double)jsonPlst[i]["rating_star"];
                            model.instant_booking = (bool)jsonPlst[i]["instant_booking"];
                            model.order_count     = (int)jsonPlst[i]["order_count"];
                            model.days            = (int)jsonPlst[i]["days"];
                            model.hours           = (int)jsonPlst[i]["hours"];
                            model.introduction    = jsonPlst[i]["introduction"].ToString();
                            model.duration        = (int)jsonPlst[i]["duration"];
                            //model.display_price_usd = jsonPlst[i]["display_price_usd"].ToString();
                            //model.price_usd = (double)jsonPlst[i]["price_usd"];
                            model.prod_type = jsonPlst[i]["main_cat_key"].ToString();
                            model.tag       = jsonPlst[i]["cat_key"].ToObject <string[]>();//把傑森物件轉成字串陣列


                            //取國家,城市
                            List <Country> country =
                                ((JArray)jsonPlst[i]["countries"])
                                .Select(x => new Country {
                                id     = (string)x["id"],
                                name   = (string)x["name"],
                                cities = x["cities"].ToObject <List <City> >()
                            }).ToList();

                            model.countries = country;

                            pLst.Add(model);
                        }
                        else
                        {
                            countBlackProd++;
                        }

                        //頁數、筆數統計
                        md.total_count = (int)obj["metadata"]["pagination"]["total_count"] - countBlackProd; //商品總筆數 扣掉黑名單筆數
                        md.start       = (int)obj["metadata"]["pagination"]["start"];                        //從第?筆開始
                        md.count       = (int)obj["metadata"]["pagination"]["count"];                        //一頁?筆商品

                        prod.metadata = md;

                        //stats 金額統計
                        if (rq.stats != null)
                        {
                            Stats s = new Stats();
                            s.price = new Price()
                            {
                                min      = (int)obj["data"]["stats"]["price"]["min"],
                                max      = (int)obj["data"]["stats"]["price"]["max"],
                                count    = (int)obj["data"]["stats"]["price"]["count"],
                                currency = obj["data"]["stats"]["price"]["currency"].ToString()
                            };
                            prod.stats = s;
                        }

                        //facets 大分類 小分類 ...的統計
                        if (rq.facets != null)
                        {
                            Facets f = new Facets();

                            //大分
                            if (rq.facets.Where(x => x.Equals("cat_main")).Count() == 1)
                            {
                                List <CatMain> cm =
                                    ((JArray)obj["data"]["facets"]["cat_main"])
                                    .Select(x => new CatMain {
                                    id    = (string)x["id"],
                                    name  = (string)x["name"],
                                    sort  = (string)x["sort"],
                                    count = (int)x["count"]
                                }).ToList();

                                f.cat_main = cm;
                            }

                            //小分
                            if (rq.facets.Where(x => x.Equals("cat")).Count() == 1)
                            {
                                List <Cat> ca =
                                    ((JArray)obj["data"]["facets"]["cat"])
                                    .Select(x => new Cat {
                                    id    = (string)x["id"],
                                    name  = (string)x["name"],
                                    sort  = (string)x["sort"],
                                    count = (int)x["count"]
                                }).ToList();

                                f.cat = ca;
                            }


                            if (rq.facets.Where(x => x.Equals("total_time")).Count() == 1)
                            {
                                List <TotalTime> tt =
                                    ((JArray)obj["data"]["facets"]["total_time"])
                                    .Select(x => new TotalTime {
                                    time  = (int)x["time"],
                                    count = (int)x["count"]
                                }).ToList();

                                f.total_time = tt;
                            }

                            if (rq.facets.Where(x => x.Equals("guide_lang")).Count() == 1)
                            {
                                List <GuideLang> gl =
                                    ((JArray)obj["data"]["facets"]["guide_lang"])
                                    .Select(x => new GuideLang {
                                    id    = (string)x["id"],
                                    name  = (string)x["name"],
                                    count = (int)x["count"]
                                }).ToList();

                                f.guide_lang = gl;
                            }

                            //可販售日期
                            if (rq.facets.Where(x => x.Equals("sale_dt")).Count() == 1)
                            {
                                List <SaleDt> sd =
                                    ((JArray)obj["data"]["facets"]["sale_dt"])
                                    .Select(x => new SaleDt {
                                    id    = (int)x["id"],
                                    count = (int)x["count"]
                                }).ToList();

                                f.sale_dt = sd;
                            }

                            prod.facets = f;
                        }
                    }

                    prod.prods = pLst;
                    #endregion
                }
            } catch (Exception ex) {
                Website.Instance.logger.FatalFormat($"getProdLst  Error :{ex.Message},{ex.StackTrace}");

                throw ex;
            }

            return(prod);
        }
Example #21
0
        /// <summary>
        /// FacetConvert
        /// </summary>
        /// <param name="facets"></param>
        /// <returns></returns>
        public Facet FacetConvert(Facets facets)
        {
            switch (facets)
            {
            case Facets.additives:
                return(Facet.additive);

            case Facets.allergens:
                return(Facet.allergen);

            case Facets.brands:
                return(Facet.brand);

            case Facets.categories:
                return(Facet.category);

            case Facets.countries:
                return(Facet.country);

            case Facets.contributors:
                return(Facet.contributor);

            case Facets.codes:
                return(Facet.code);

            case Facets.entry_dates:
                return(Facet.entry_date);

            case Facets.ingredients:
                return(Facet.ingredient);

            case Facets.labels:
                return(Facet.label);

            case Facets.languages:
                return(Facet.language);

            case Facets.nutrition_grade:
                return(Facet.nutrition_grade);

            case Facets.packagings:
                return(Facet.packaging);

            case Facets.packager_codes:
                return(Facet.packager_code);

            case Facets.photographers:
                return(Facet.photographer);

            case Facets.informers:
                return(Facet.informer);

            case Facets.purchase_places:
                return(Facet.purchase_place);

            case Facets.states:
                return(Facet.state);

            case Facets.stores:
                return(Facet.store);

            case Facets.traces:
                return(Facet.trace);
            }
            throw new Exception("current value is not relevant: " + facets.ToString());
        }
Example #22
0
 public void CreateFacetOfOrganizationSeoName()
 {
     Facets.Add(new FacetParameter {
         Name = "organization_seo_lowercase", Value = OrganizationSeoName
     });
 }
Example #23
0
 /// <summary>
 /// Adds a facet to the <see cref="Facets"/> collection
 /// </summary>
 /// <param name="facet"></param>
 /// <returns></returns>
 public StatsParameters AddFacet(string facet)
 {
     Facets.Add(facet);
     return(this);
 }
Example #24
0
 public bool HasFacets()
 {
     return(Facets != null && Facets.Any());
 }
Example #25
0
        public virtual void Test_Directory() // LUCENENET specific - name collides with property of LuceneTestCase
        {
            Store.Directory indexDir = NewDirectory();
            Store.Directory taxoDir  = NewDirectory();
            IndexWriter     w        = new IndexWriter(indexDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)));
            var             tw       = new DirectoryTaxonomyWriter(taxoDir);

            // first empty commit
            w.Commit();
            tw.Commit();
            var          mgr    = new SearcherTaxonomyManager(indexDir, taxoDir, null);
            FacetsConfig config = new FacetsConfig();

            config.SetMultiValued("field", true);
            AtomicBoolean stop = new AtomicBoolean();

            // How many unique facets to index before stopping:
            //int ordLimit = TestNightly ? 100000 : 6000;
            // LUCENENET specific: 100000 facets takes about 2-3 hours. To keep it under
            // the 1 hour free limit of Azure DevOps, this was reduced to 30000.
            int ordLimit = TestNightly ? 30000 : 6000;

            var indexer = new IndexerThread(w, config, tw, mgr, ordLimit, stop);

            indexer.Start();

            try
            {
                while (!stop)
                {
                    SearcherAndTaxonomy pair = mgr.Acquire();
                    try
                    {
                        //System.out.println("search maxOrd=" + pair.taxonomyReader.getSize());
                        FacetsCollector sfc = new FacetsCollector();
                        pair.Searcher.Search(new MatchAllDocsQuery(), sfc);
                        Facets      facets = GetTaxonomyFacetCounts(pair.TaxonomyReader, config, sfc);
                        FacetResult result = facets.GetTopChildren(10, "field");
                        if (pair.Searcher.IndexReader.NumDocs > 0)
                        {
                            //System.out.println(pair.taxonomyReader.getSize());
                            Assert.IsTrue(result.ChildCount > 0);
                            Assert.IsTrue(result.LabelValues.Length > 0);
                        }

                        //if (VERBOSE) {
                        //System.out.println("TEST: facets=" + FacetTestUtils.toString(results.get(0)));
                        //}
                    }
                    finally
                    {
                        mgr.Release(pair);
                    }
                }
            }
            finally
            {
                indexer.Join();
            }

            if (Verbose)
            {
                Console.WriteLine("TEST: now stop");
            }

            IOUtils.Dispose(mgr, tw, w, taxoDir, indexDir);
        }
Example #26
0
        public static async Task Execute(IOwinContext context, PackageSearcherManager searcherManager)
        {
            Trace.TraceInformation("Search: {0}", context.Request.QueryString);

            string q = context.Request.Query["q"] ?? string.Empty;

            string projectType = context.Request.Query["projectType"] ?? string.Empty;

            string sortBy = context.Request.Query["sortBy"] ?? string.Empty;

            bool luceneQuery;

            if (!bool.TryParse(context.Request.Query["luceneQuery"], out luceneQuery))
            {
                luceneQuery = true;
            }

            bool includePrerelease;

            if (!bool.TryParse(context.Request.Query["prerelease"], out includePrerelease))
            {
                includePrerelease = false;
            }

            bool countOnly;

            if (!bool.TryParse(context.Request.Query["countOnly"], out countOnly))
            {
                countOnly = false;
            }

            string feed = context.Request.Query["feed"] ?? "none";

            int skip;

            if (!int.TryParse(context.Request.Query["skip"], out skip))
            {
                skip = 0;
            }

            int take;

            if (!int.TryParse(context.Request.Query["take"], out take))
            {
                take = 20;
            }

            bool includeExplanation;

            if (!bool.TryParse(context.Request.Query["explanation"], out includeExplanation))
            {
                includeExplanation = false;
            }

            bool ignoreFilter;

            if (!bool.TryParse(context.Request.Query["ignoreFilter"], out ignoreFilter))
            {
                ignoreFilter = false;
            }

            string        fxName             = context.Request.Query["supportedFramework"];
            FrameworkName supportedFramework = null;

            if (!string.IsNullOrEmpty(fxName))
            {
                supportedFramework = VersionUtility.ParseFrameworkName(fxName);
            }
            if (supportedFramework == null || !searcherManager.GetFrameworks().Contains(supportedFramework))
            {
                supportedFramework = FrameworksList.AnyFramework;
            }

            Query  query;
            string content;
            int    maxAgeSeconds = 0;

            try
            {
                query = LuceneQueryCreator.Parse(q, luceneQuery);

                if (query == null)
                {
                    query         = new MatchAllDocsQuery();
                    maxAgeSeconds = 3600;
                }
                if (!ignoreFilter && !luceneQuery)
                {
                    string facet = includePrerelease ?
                                   Facets.LatestPrereleaseVersion(supportedFramework) :
                                   Facets.LatestStableVersion(supportedFramework);

                    var newQuery = new BooleanQuery();
                    newQuery.Add(query, Occur.MUST);
                    newQuery.Add(new TermQuery(new Term("Facet", facet)), Occur.MUST);
                    query = newQuery;
                }

                string args = string.Format("Searcher.Search(..., {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10})", q, countOnly, projectType, includePrerelease, feed, sortBy, skip, take, includeExplanation, ignoreFilter, luceneQuery);
                Trace.TraceInformation(args);

                content = Searcher.Search(
                    searcherManager,
                    query,
                    countOnly,
                    projectType,
                    includePrerelease,
                    feed,
                    sortBy,
                    skip,
                    take,
                    includeExplanation,
                    ignoreFilter);

                JObject result = JObject.Parse(content);
                result["answeredBy"] = "Search";
            }
            catch (Lucene.Net.QueryParsers.ParseException)
            {
                context.Response.StatusCode = 400;
                content = "Invalid query";
            }
            finally
            {
                context.Response.Headers.Add("Cache-Control", new[] { string.Format("private, max-age={0}", maxAgeSeconds) });
                context.Response.ContentType = "application/json";
            }
            await context.Response.WriteAsync(content);
        }
Example #27
0
        public static void TestFlexLuceneFS(string[] args)
        {
            string ixdir = string.Empty;

            if (IsLinux)
            {
                ixdir = "/home/master/flexlucene/indexes";
            }
            else
            {
                ixdir = @"c:\temp\flexlucene\indexes";
            }

            StandardAnalyzer analyzer = new StandardAnalyzer();

            //FlexLucene.Store.Directory index = SimpleFSDirectory.Open(java.nio.file.Paths.get(ixdir),SimpleFSLockFactory.INSTANCE);
            FlexLucene.Store.Directory index = SimpleFSDirectory.Open(java.nio.file.Paths.get(ixdir));
//			FlexLucene.Store.Directory index = NIOFSDirectory.Open(java.nio.file.Paths.get(ixdir));



            config = new IndexWriterConfig(analyzer);
            cnf    = new FacetsConfig();
            cnf.SetIndexFieldName("title", "facet_title");
            cnf.SetIndexFieldName("isbn", "facet_isbn");
            LuceneTest.taxoDir    = (FlexLucene.Store.Directory) new RAMDirectory();
            LuceneTest.taxoWriter = (TaxonomyWriter) new FlexLucene.Facet.Taxonomy.Directory.DirectoryTaxonomyWriter(LuceneTest.taxoDir, IndexWriterConfigOpenMode.CREATE);
            IndexWriter w = new IndexWriter(index, LuceneTest.config);

            addDoc(w, "Lucene in Action", "9900001");
            addDoc(w, "Lucene for Dummies", "9900002");
            addDoc(w, "Lucene for Dummies 2", "9900003");

            w.close();
            String querystr = "isbn:99*";

            Query                q           = new QueryParser("title", (Analyzer)analyzer).Parse(querystr);
            int                  hitsPerPage = 10;
            IndexReader          reader      = (IndexReader)DirectoryReader.Open(index);
            IndexSearcher        searcher    = new IndexSearcher(reader);
            TopScoreDocCollector collector   = TopScoreDocCollector.Create(hitsPerPage);

            searcher.Search(q, (Collector)collector);
            ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
            Console.WriteLine("Found " + hits.Length + " hits.");
            for (int i = 0; i < hits.Length; ++i)
            {
                int      docId = hits [i].Doc;
                Document d     = searcher.Doc(docId);
                Console.WriteLine(i + 1 + ". " + d.Get("isbn") + "\t" + d.Get("title"));
            }
            SortedSetDocValuesReaderState state = (SortedSetDocValuesReaderState) new DefaultSortedSetDocValuesReaderState(reader, "facet_isbn");
            FacetsCollector fc = new FacetsCollector();

            FacetsCollector.Search(searcher, q, 10, (Collector)fc);
            Facets      facets = (Facets) new SortedSetDocValuesFacetCounts(state, fc);
            FacetResult result = facets.GetTopChildren(10, "isbn", new String[0]);

            for (int j = 0; j < result.ChildCount; ++j)
            {
                LabelAndValue lv = result.LabelValues [j];
                Console.WriteLine(String.Format("Label={0}, Value={1}", lv.Label, lv.Value));
            }
            reader.close();
        }
        /// <summary>
        /// Gets the facet parameters.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        protected virtual Facets<ESDocument> GetFacets(ISearchCriteria criteria)
        {
            // Now add facets
            var facetParams = new Facets<ESDocument>();
            foreach (var filter in criteria.Filters)
            {
                if (filter is AttributeFilter)
                {
                    AddFacetQueries(facetParams, filter.Key, ((AttributeFilter)filter).Values, criteria);
                }
                else if (filter is RangeFilter)
                {
                    AddFacetQueries(facetParams, filter.Key, ((RangeFilter)filter).Values, criteria);
                }
                else if (filter is PriceRangeFilter)
                {
                    var currency = ((PriceRangeFilter)filter).Currency;
                    if (currency.Equals(criteria.Currency, StringComparison.OrdinalIgnoreCase))
                    {
                        AddFacetPriceQueries(facetParams, filter.Key, ((PriceRangeFilter)filter).Values, criteria);
                    }
                }
                else if (filter is CategoryFilter)
                {
                    AddFacetQueries(facetParams, filter.Key, ((CategoryFilter)filter).Values);
                }
            }

            /*
            var catalogCriteria = criteria as CatalogItemSearchCriteria;

            if (catalogCriteria != null)
            {
                AddSubCategoryFacetQueries(facetParams, catalogCriteria);
            }
             * */

            return facetParams;
        }
 private void AddFacetQueries(Facets<ESDocument> param, string fieldName, IEnumerable<CategoryFilterValue> values)
 {
     foreach (var val in values)
     {
         var facetName = String.Format("{0}-{1}", fieldName.ToLower(), val.Id.ToLower());
         param.FilterFacets(ff => 
             ff.FacetName(facetName).Filter(f => f.Query(q => q.Bool(bf => bf.Must(bfm =>
             bfm.Custom("{{\"wildcard\" : {{ \"{0}\" : \"{1}\" }}}}", fieldName.ToLower(), val.Outline.ToLower()))))));
     }
 }
        /// <summary>
        /// Adds the facet queries.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="values">The values.</param>
        private void AddFacetQueries(
            Facets<ESDocument> param, string fieldName, IEnumerable<AttributeFilterValue> values, ISearchCriteria criteria)
        {
            if (values == null) return;

            var ffilter = new BoolFilter<ESDocument>();
            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(fieldName))
                {
                    var q = ElasticQueryHelper.CreateQuery(criteria, f);
                    ffilter.Must(ff => ff.Bool(bb=>q));
                }
            }

            var facetFilter = new FacetFilter<ESDocument>();
            facetFilter.Bool(f => ffilter);
            
            //var filter = new FacetFilter<ESDocument>();
            //facetFilter.Terms(x => x.Values(values.Select(y => y.Value).ToArray()));
            //var filterFacet = new FilterFacet<ESDocument>();
            //filterFacet.FacetName(fieldName.ToLower()).FacetFilter(f => facetFilter);

            param.Terms(t => t.FacetName(fieldName.ToLower()).Field(fieldName.ToLower()).FacetFilter(ff => facetFilter));
        }
        protected override async Task Execute(IOwinContext context)
        {
            Trace.TraceInformation("Search: {0}", context.Request.QueryString);

            string q = context.Request.Query["q"] ?? String.Empty;

            string projectType = context.Request.Query["projectType"] ?? String.Empty;

            string sortBy = context.Request.Query["sortBy"] ?? String.Empty;

            bool luceneQuery;

            if (!bool.TryParse(context.Request.Query["luceneQuery"], out luceneQuery))
            {
                luceneQuery = true;
            }

            bool includePrerelease;

            if (!bool.TryParse(context.Request.Query["prerelease"], out includePrerelease))
            {
                includePrerelease = false;
            }

            bool countOnly;

            if (!bool.TryParse(context.Request.Query["countOnly"], out countOnly))
            {
                countOnly = false;
            }

            string feed = context.Request.Query["feed"] ?? "none";

            int skip;

            if (!int.TryParse(context.Request.Query["skip"], out skip))
            {
                skip = 0;
            }

            int take;

            if (!int.TryParse(context.Request.Query["take"], out take))
            {
                take = 20;
            }

            bool includeExplanation = false;

            if (!bool.TryParse(context.Request.Query["explanation"], out includeExplanation))
            {
                includeExplanation = false;
            }

            bool ignoreFilter;

            if (!bool.TryParse(context.Request.Query["ignoreFilter"], out ignoreFilter))
            {
                ignoreFilter = false;
            }

            string        fxName             = context.Request.Query["supportedFramework"];
            FrameworkName supportedFramework = null;

            if (!String.IsNullOrEmpty(fxName))
            {
                supportedFramework = VersionUtility.ParseFrameworkName(fxName);
            }
            if (supportedFramework == null || !SearcherManager.GetFrameworks().Contains(supportedFramework))
            {
                supportedFramework = FrameworksList.AnyFramework;
            }

            var query = LuceneQueryCreator.Parse(q, luceneQuery);

            if (!ignoreFilter && !luceneQuery)
            {
                string facet = includePrerelease ?
                               Facets.LatestPrereleaseVersion(supportedFramework) :
                               Facets.LatestStableVersion(supportedFramework);

                var newQuery = new BooleanQuery();
                newQuery.Add(query, Occur.MUST);
                newQuery.Add(new TermQuery(new Term("Facet", facet)), Occur.MUST);
                query = newQuery;
            }

            string args = string.Format("Searcher.Search(..., {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10})", q, countOnly, projectType, includePrerelease, feed, sortBy, skip, take, includeExplanation, ignoreFilter, luceneQuery);

            Trace.TraceInformation(args);

            string content = NuGet.Indexing.Searcher.Search(
                SearcherManager,
                query,
                countOnly,
                projectType,
                includePrerelease,
                feed,
                sortBy,
                skip,
                take,
                includeExplanation,
                ignoreFilter);

            JObject result = JObject.Parse(content);

            result["answeredBy"] = ServiceName.ToString();

            await WriteResponse(context, result.ToString());
        }
        /// <summary>
        /// Adds the facet queries.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="values">The values.</param>
        private void AddFacetQueries(Facets<ESDocument> param, string fieldName, IEnumerable<RangeFilterValue> values, ISearchCriteria criteria)
        {
            if (values == null)
                return;

            var ffilter = new Filter<ESDocument>();
            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(fieldName))
                {
                    var q = ElasticQueryHelper.CreateQuery(criteria, f);
                    ffilter.Bool(ff => q);
                }
            }

            foreach (var value in values)
            {
                var filter = new FacetFilter<ESDocument>();
                filter.Range(r => r.IncludeLower(false).IncludeUpper().From(value.Lower).To(value.Upper));
                filter.And(b => ffilter);
                param.FilterFacets(ff => ff.FacetName(String.Format("{0}-{1}", fieldName, value.Id)).Filter(f => filter));
            }
        }
        public virtual void TestSparseFacets()
        {
            Store.Directory dir     = NewDirectory();
            Store.Directory taxoDir = NewDirectory();

            // Writes facet ords to a separate directory from the
            // main index:
            var taxoWriter = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);

            RandomIndexWriter writer = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this,
#endif
                Random, dir);
            FacetsConfig config = new FacetsConfig();

            Document doc = new Document();

            doc.Add(new FacetField("a", "foo1"));
            writer.AddDocument(config.Build(taxoWriter, doc));

            if (Random.NextBoolean())
            {
                writer.Commit();
            }

            doc = new Document();
            doc.Add(new FacetField("a", "foo2"));
            doc.Add(new FacetField("b", "bar1"));
            writer.AddDocument(config.Build(taxoWriter, doc));

            if (Random.NextBoolean())
            {
                writer.Commit();
            }

            doc = new Document();
            doc.Add(new FacetField("a", "foo3"));
            doc.Add(new FacetField("b", "bar2"));
            doc.Add(new FacetField("c", "baz1"));
            writer.AddDocument(config.Build(taxoWriter, doc));

            // NRT open
            IndexSearcher searcher = NewSearcher(writer.GetReader());

            // NRT open
            var taxoReader = new DirectoryTaxonomyReader(taxoWriter);

            FacetsCollector c = new FacetsCollector();

            searcher.Search(new MatchAllDocsQuery(), c);

            Facets facets = GetTaxonomyFacetCounts(taxoReader, new FacetsConfig(), c);

            // Ask for top 10 labels for any dims that have counts:
            IList <FacetResult> results = facets.GetAllDims(10);

            Assert.AreEqual(3, results.Count);
            Assert.AreEqual("dim=a path=[] value=3 childCount=3\n  foo1 (1)\n  foo2 (1)\n  foo3 (1)\n", results[0].ToString());
            Assert.AreEqual("dim=b path=[] value=2 childCount=2\n  bar1 (1)\n  bar2 (1)\n", results[1].ToString());
            Assert.AreEqual("dim=c path=[] value=1 childCount=1\n  baz1 (1)\n", results[2].ToString());

            IOUtils.Dispose(writer, taxoWriter, searcher.IndexReader, taxoReader, taxoDir, dir);
        }
        /// <summary>
        /// Adds the facet queries.
        /// </summary>
        /// <param name="param">The param.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="values">The values.</param>
        /// <param name="criteria">The criteria.</param>
        private void AddFacetPriceQueries(Facets<ESDocument> param, string fieldName, IEnumerable<RangeFilterValue> values, ISearchCriteria criteria)
        {
            if (values == null)
                return;

            var ffilter = new MustFilter<ESDocument>();
            foreach (var f in criteria.CurrentFilters)
            {
                if (!f.Key.Equals(fieldName))
                {
                    var q = ElasticQueryHelper.CreateQuery(criteria, f);
                    ffilter.Bool(ff => q);
                }
            }

            foreach (var value in values)
            {
                var query = ElasticQueryHelper.CreatePriceRangeFilter(criteria, fieldName, value);
                query.Must(b =>ffilter);
                if (query != null)
                {
                    param.FilterFacets(
                        ff => ff.FacetName(String.Format("{0}-{1}", fieldName, value.Id)).Filter(f => f.Bool(b => query)));
                }
            }
        }
        public virtual void TestNrt()
        {
            Store.Directory   dir     = NewDirectory();
            Store.Directory   taxoDir = NewDirectory();
            IndexWriterConfig iwc     = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            // Don't allow tiny maxBufferedDocs; it can make this
            // test too slow:
            iwc.SetMaxBufferedDocs(Math.Max(500, iwc.MaxBufferedDocs));

            // MockRandom/AlcololicMergePolicy are too slow:
            TieredMergePolicy tmp = new TieredMergePolicy();

            tmp.FloorSegmentMB = .001;
            iwc.SetMergePolicy(tmp);
            IndexWriter  w      = new IndexWriter(dir, iwc);
            var          tw     = new DirectoryTaxonomyWriter(taxoDir);
            FacetsConfig config = new FacetsConfig();

            config.SetMultiValued("field", true);
            AtomicBoolean stop = new AtomicBoolean();

            // How many unique facets to index before stopping:
            int ordLimit = TEST_NIGHTLY ? 100000 : 6000;

            var indexer = new IndexerThread(w, config, tw, null, ordLimit, stop);

            var mgr = new SearcherTaxonomyManager(w, true, null, tw);

            var reopener = new ThreadAnonymousInnerClassHelper(this, stop, mgr);

            reopener.Name = "reopener";
            reopener.Start();

            indexer.Name = "indexer";
            indexer.Start();

            try
            {
                while (!stop.Get())
                {
                    SearcherAndTaxonomy pair = mgr.Acquire();
                    try
                    {
                        //System.out.println("search maxOrd=" + pair.taxonomyReader.getSize());
                        FacetsCollector sfc = new FacetsCollector();
                        pair.Searcher.Search(new MatchAllDocsQuery(), sfc);
                        Facets      facets = GetTaxonomyFacetCounts(pair.TaxonomyReader, config, sfc);
                        FacetResult result = facets.GetTopChildren(10, "field");
                        if (pair.Searcher.IndexReader.NumDocs > 0)
                        {
                            //System.out.println(pair.taxonomyReader.getSize());
                            Assert.True(result.ChildCount > 0);
                            Assert.True(result.LabelValues.Length > 0);
                        }

                        //if (VERBOSE) {
                        //System.out.println("TEST: facets=" + FacetTestUtils.toString(results.get(0)));
                        //}
                    }
                    finally
                    {
                        mgr.Release(pair);
                    }
                }
            }
            finally
            {
                indexer.Join();
                reopener.Join();
            }

            if (VERBOSE)
            {
                Console.WriteLine("TEST: now stop");
            }

            IOUtils.Dispose(mgr, tw, w, taxoDir, dir);
        }
Example #36
0
        /// <summary>
        /// Insert.
        /// </summary>
        /// <param name="v"></param>
        /// <param name="old"></param>
        protected void Insert(Vertex v, Triangle old)
        {
            // Avoid duplicates, if this facet contains v as a vertex,
            // just return.
            if ((old.A.X == v.X) && (old.A.Y == v.Y))
            {
                return;
            }
            if ((old.B.X == v.X) && (old.B.Y == v.Y))
            {
                return;
            }
            if ((old.C.X == v.X) && (old.C.Y == v.Y))
            {
                return;
            }

            m_points.Add(v);

            // Split old into 3 triangles,
            // Because old is counter clockwise, when duplicated,
            // ab, bc, ca is counter clockwise.
            // By changing one point and keeping to the commutation,
            // they remain counter clockwise.
            Triangle ab = new Triangle(old); // contains old ab, v is new C.
            Triangle bc = new Triangle(old); // contains old bc, v is new A.
            Triangle ca = new Triangle(old); // contains old ca, v is new B.

            ab.C = v;
            bc.A = v;
            ca.B = v;

            // This also makes assigning the sides easy.
            ab.BC = bc;
            ab.CA = ca;
            bc.AB = ab;
            bc.CA = ca;
            ca.AB = ab;
            ca.BC = bc;

            // The existing trianges that share an edge with old,
            // now share an edge with one of the three new triangles.
            // Repair the existing.

            // One way of looking at it:
            // for (int j = 0; j < 3; j++)
            // {
            //    if ((ab.AB != null) && (ab.AB.Edge(j) == old)) ab.AB.SetEdge(j, ab);
            //    if ((bc.BC != null) && (bc.BC.Edge(j) == old)) bc.BC.SetEdge(j, bc);
            //    if ((ca.CA != null) && (ca.CA.Edge(j) == old)) ca.CA.SetEdge(j, ca);
            // }
            // This is faster, null check is once per edge, and default logic
            // reduces the compares by one. Instead of 3*3*2 comparisons = 18,
            // Worst case is 3*3 = 9, Average is 2+3+3=8.
            Triangle[] ta = { ab.AB, bc.BC, ca.CA };
            Triangle[] tb = { ab, bc, ca };
            for (int j = 0; j < 3; j++)
            {
                if (ta[j] == null)
                {
                    continue;
                }
                if (ta[j].Edge(0) == old)
                {
                    ta[j].SetEdge(0, tb[j]);
                    continue;
                }
                if (ta[j].Edge(1) == old)
                {
                    ta[j].SetEdge(1, tb[j]);
                    continue;
                }
                ta[j].SetEdge(2, tb[j]);
            }

            // Add the new, remove the old.
            Facets.Add(ab);
            Facets.Add(bc);
            Facets.Add(ca);
            Facets.Remove(old);

            // Check for 1st order flipping.
            // Triangle ab has neighbor ab.AB.
            // Depth of up to recursion deep.
            // Remember that due to commutators, same.same is outward,
            // same.different is inward.

            flipIfNeeded(ab, ab.AB, Recursion);
            flipIfNeeded(bc, bc.BC, Recursion);
            flipIfNeeded(ca, ca.CA, Recursion);

            return;
        }
Example #37
0
        public virtual void TestRandom()
        {
            string[]        tokens   = GetRandomTokens(10);
            Store.Directory indexDir = NewDirectory();
            Store.Directory taxoDir  = NewDirectory();

            RandomIndexWriter w      = new RandomIndexWriter(Random, indexDir, Similarity, TimeZone);
            var             tw       = new DirectoryTaxonomyWriter(taxoDir);
            FacetsConfig    config   = new FacetsConfig();
            int             numDocs  = AtLeast(1000);
            int             numDims  = TestUtil.NextInt32(Random, 1, 7);
            IList <TestDoc> testDocs = GetRandomDocs(tokens, numDocs, numDims);

            foreach (TestDoc testDoc in testDocs)
            {
                Document doc = new Document();
                doc.Add(NewStringField("content", testDoc.content, Field.Store.NO));
                for (int j = 0; j < numDims; j++)
                {
                    if (testDoc.dims[j] != null)
                    {
                        doc.Add(new FacetField("dim" + j, testDoc.dims[j]));
                    }
                }
                w.AddDocument(config.Build(tw, doc));
            }

            // NRT open
            IndexSearcher searcher = NewSearcher(w.GetReader());

            // NRT open
            var tr = new DirectoryTaxonomyReader(tw);

            int iters = AtLeast(100);

            for (int iter = 0; iter < iters; iter++)
            {
                string searchToken = tokens[Random.Next(tokens.Length)];
                if (VERBOSE)
                {
                    Console.WriteLine("\nTEST: iter content=" + searchToken);
                }
                FacetsCollector fc = new FacetsCollector();
                FacetsCollector.Search(searcher, new TermQuery(new Term("content", searchToken)), 10, fc);
                Facets facets = GetTaxonomyFacetCounts(tr, config, fc);

                // Slow, yet hopefully bug-free, faceting:
                var expectedCounts = new List <Dictionary <string, int?> >();
                for (int i = 0; i < numDims; i++)
                {
                    expectedCounts.Add(new Dictionary <string, int?>());
                }

                foreach (TestDoc doc in testDocs)
                {
                    if (doc.content.Equals(searchToken, StringComparison.Ordinal))
                    {
                        for (int j = 0; j < numDims; j++)
                        {
                            if (doc.dims[j] != null)
                            {
                                int?v = expectedCounts[j].ContainsKey(doc.dims[j]) ? expectedCounts[j][doc.dims[j]] : null;
                                if (v == null)
                                {
                                    expectedCounts[j][doc.dims[j]] = 1;
                                }
                                else
                                {
                                    expectedCounts[j][doc.dims[j]] = (int)v + 1;
                                }
                            }
                        }
                    }
                }

                List <FacetResult> expected = new List <FacetResult>();
                for (int i = 0; i < numDims; i++)
                {
                    List <LabelAndValue> labelValues = new List <LabelAndValue>();
                    int totCount = 0;
                    foreach (KeyValuePair <string, int?> ent in expectedCounts[i])
                    {
                        labelValues.Add(new LabelAndValue(ent.Key, ent.Value.Value));
                        totCount += ent.Value.Value;
                    }
                    SortLabelValues(labelValues);
                    if (totCount > 0)
                    {
                        expected.Add(new FacetResult("dim" + i, new string[0], totCount, labelValues.ToArray(), labelValues.Count));
                    }
                }

                // Sort by highest value, tie break by value:
                SortFacetResults(expected);

                IList <FacetResult> actual = facets.GetAllDims(10);

                // Messy: fixup ties
                SortTies(actual);

                Assert.AreEqual(expected, actual);
            }

            IOUtils.Dispose(w, tw, searcher.IndexReader, tr, indexDir, taxoDir);
        }
Example #38
0
 // Get all available data.
 /// <summary>
 /// get_data
 /// </summary>
 /// <param name="facet"></param>
 /// <returns></returns>
 public string get_data(Facets facet)
 {
     return(Utils.fetch(facet.ToString(), 1));
 }
 public void Init()
 {
     instance = new Facets();
 }
Example #40
0
 public void Clear()
 {
     SourceLength = 0;
     TargetLength = 0;
     Facets.Clear();
 }