private string CreateRequest(string request, GsaSetting gsaSetting, string langId, int pageNumber, int pageIndex)
        {
            var context = EngineContext.Current.Resolve <ICollectionMappingService>();

            string query          = String.Empty;
            var    collection     = context.GetCollectionByLanguageCulture(langId);
            string collectionList = gsaSetting.Collection;

            if (collection != null)
            {
                collectionList = String.Empty;
                foreach (var c in collection)
                {
                    collectionList = c.Collection + "|";
                }
                if (collectionList.EndsWith("|"))
                {
                    collectionList = collectionList.Substring(0, collectionList.Length - 1);
                }
            }
            string ie        = "utf8";
            string oe        = "utf8";
            int    nextItems = pageIndex * gsaSetting.ResultsPerPage;

            /*Removed &lr={3}*/
            query = String.Format("q={0}&num={1}&site={2}&ie={9}&client={3}&access={4}&sort={5}&getfields={6}&start={7}&sa=N&filter=0&entqr=3&output=x{8}&entqrm=3o&oe={10}",
                                  request, (1000 - nextItems) >= gsaSetting.ResultsPerPage ? gsaSetting.ResultsPerPage : 1000 - nextItems, collectionList, gsaSetting.FrontEndClient, gsaSetting.AccessType, "date:D:L:d1", GetFields(), nextItems, gsaSetting.OutputOption, ie, oe);
            return(query);
        }
        public SearchResultModel Search(string request, GsaSetting gsaSetting, string langId, CatalogPagingFilteringModel command)
        {
            /*Create Search Request*/
            var query = CreateRequest(request, gsaSetting, langId, command.PageNumber, command.PageIndex);
            /*Create Request*/
            var req  = (HttpWebRequest)WebRequest.Create("http://" + gsaSetting.GsaHost + "/search");
            var data = Encoding.GetEncoding("UTF-8").GetBytes(query);

            req.Method        = "POST";
            req.ContentType   = "text/xml";
            req.ContentLength = data.Length;
            using (var stream = req.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse)req.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var searchResult   = GenerateResultModel(responseString, request, command, gsaSetting.ResultsPerPage);



            return(searchResult);
        }
        private void GetJsonResult(GsaSetting gsaSetting, string request, CatalogPagingFilteringModel command, out ElasticSearchResultModel modelRes)
        {
            var model = new ElasticSearchResultModel();

            modelRes = model;

            var settings       = new ConnectionSettings(new Uri(gsaSetting.ElasticHost)).DefaultIndex(gsaSetting.SearchIndex);
            var client         = new ElasticClient(settings);
            var searchResponse = client.Search <Product>(s => s
                                                         .Size(0)
                                                         .Query(q => q
                                                                .MultiMatch(m => m
                                                                            .Fields(f => f.Field(p => p.pagetext).Field(p => p.name))
                                                                            .Query(request)
                                                                            )
                                                                )
                                                         .Aggregations(a => a
                                                                       .Terms("unique_results", c => c
                                                                              .Field(f => f.nopid)
                                                                              .Aggregations(ag => ag
                                                                                            .TopHits("top_unique_result", th => th
                                                                                                     .Source(src => src
                                                                                                             .Includes(fs => fs
                                                                                                                       .Field(f => f.name)
                                                                                                                       .Field(f => f.nopid)
                                                                                                                       .Field(f => f.pagetext)
                                                                                                                       .Field(f => f.page_number)
                                                                                                                       )
                                                                                                             )
                                                                                                     .Size(1)
                                                                                                     .TrackScores()
                                                                                                     )
                                                                                            )
                                                                              )
                                                                       )
                                                         );

            model.SearchKeyword        = request;
            model.ElastigRequestResult = null;
            model.TotalResult          = 0;
            model.TotalPages           = 0;

            if (searchResponse.IsValid)
            {
                var results = searchResponse.Aggs.Terms("unique_results");
                model.ElastigRequestResult = results;

                if (results == null || results == null)
                {
                    return;
                }
                model.TotalResult = results.Buckets.Count;
                model.TotalPages  = (results.Buckets.Count <= gsaSetting.EResultsPerPage ? 1 : (results.Buckets.Count / gsaSetting.EResultsPerPage) + 1);
            }
        }
        public ElasticSearchResultModel Search(string request, GsaSetting gsaSetting, string langId, CatalogPagingFilteringModel command)
        {
            var model = new ElasticSearchResultModel();

            GetJsonResult(gsaSetting, request, command, out model);
            var productService = Core.Infrastructure.EngineContext.Current.Resolve <Nop.Services.Catalog.IProductService>();
            var workContext    = Core.Infrastructure.EngineContext.Current.Resolve <Core.IWorkContext>();

            var products = productService.SearchProducts(
                keywords: request,
                searchDescriptions: true,
                languageId: workContext.WorkingLanguage.Id,
                orderBy: Core.Domain.Catalog.ProductSortingEnum.NameAsc,
                pageIndex: command.PageIndex,
                pageSize: gsaSetting.EResultsPerPage
                );

            model.NopSearchResults = products.TotalCount;
            model.NopProductsId    = new List <int>();
            return(model);
        }
Exemplo n.º 5
0
        public override void Install()
        {
            /*Set Default Settings data*/
            var settings = new GsaSetting
            {
                GsaHost          = "",
                Collection       = "",
                FrontEndClient   = "",
                AccessType       = "p",
                MaxSearchResults = 1000,
                OutputOption     = "xml",
                ResultsPerPage   = 15,
                SortFormat       = "Default",
                ImageHost        = "",
                isGsa            = false,
                IsElastic        = true,
                ElasticHost      = "",
                SearchIndex      = "",
                SearchKey        = "",
                EResultsPerPage  = 10
            };

            _settingService.SaveSetting(settings);

            /*Set Resource */
            /* Display Resource*/

            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.GsaHost", "Host Name");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.DefaultCollection", "Default Collection Name");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.FrontEndClient", "Frontend client");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.AccessType", "Access Type");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.MaxSearchResults", "Max Search Results");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.OutputOption", "Output Option");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.ResultsPerPage", "Results Per Page");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.SortFormat", "SortFormat");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Result", "Search Results");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.TotalResults", "Total Results For", "en-US");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.TotalResults", "סה''כ תוצאות ", "he-IL");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.LastUpdate", "Last update");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Photographer", "Photographer");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.City", "City");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Region", "Region");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Today", "Today");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Humidity", "Humidity");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.WindAndSpeed", "Wind direction and speed");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Tommorow", "Tommorow");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.ThreeDaysFrom", "3 days from");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Two_days_from", "Two days from");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.DisplayResult", "Display Results {0} of {1}");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.ExchangeRate", "Exchange rate");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.ImageHost", "Hostname for Images");

            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Collection", "Collection");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.LanguageCulture", "Language Culture");

            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.UseGsa", "Gsa Search engine");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.UseElastic", "Elastic Search engine (with default search)");

            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.ElasticHost", "Elastic Host");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.SearchIndex", "Search Index");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.SearchKey", "Search Key");
            /*Tab Settings*/
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Tab.General", "General");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Tab.GsaConfig", "Gsa Configuration");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Tab.ElasticSearchConfig", "Elastic Search Configuration");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Config.SearchEngine", "Select Search Engine");
            /*Error Resource*/
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.GsaHostError", "Host Name Required");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.CollectionError", "Collection Name Required");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.FrontEndClientError", "Frontend client Required");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.EventType", "Event Type");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Time", "Time");
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Search.Adress", "Address");
            /*Hint Resorses*/
            this.AddOrUpdatePluginLocaleResource("Nop.Search.Plugin.GSA.Collection.Hint", @"You can search multiple collections by separating collection names with the OR character, which is notated as the pipe symbol, or the AND character, which is notated as a period.
                The following example uses the AND character: &site = col1.col2
                The following example uses the OR character: &site = col1 | col2");
            try
            {
                _context.Install();
            }
            catch { }
            base.Install();
        }
        private List <ProductOverviewModel> GetResults(CatalogPagingFilteringModel command, GsaSetting gsaSetting)
        {
            /*Get Total */


            var searchResult = new List <ProductOverviewModel>();
            var skip         = command.PageIndex * gsaSetting.EResultsPerPage;
            var take         = gsaSetting.EResultsPerPage;

            foreach (var r in _elasticSearchResult.ElastigRequestResult.Buckets.Skip(skip).Take(take))
            {
                var topHits = r.TopHits("top_unique_result");
                if (topHits == null || topHits.Total == 0)
                {
                    continue;
                }
                var hit = topHits.Hits <Search.Plugin.GSA.Model.Product>().FirstOrDefault();
                if (hit != null)
                {
                    var data = hit.Source.ToProductOverviewModel();
                    if (data != null)
                    {
                        searchResult.Add(data);
                    }
                }
            }

            if (searchResult.Count == 0 || searchResult.Count < gsaSetting.EResultsPerPage)
            {
                int count = (_elasticSearchResult.TotalResult + _elasticSearchResult.NopSearchResults) < gsaSetting.EResultsPerPage ? 0 : searchResult.Count;
                searchResult.AddRange(PopulateFromNop(command, gsaSetting, count));
            }

            /*Paging Calculation*/
            IPagedList <ProductOverviewModel> result = new PagedList <ProductOverviewModel>(_elasticSearchResult.Data.Results, command.PageIndex, gsaSetting.EResultsPerPage, (_elasticSearchResult.TotalResult + _elasticSearchResult.NopSearchResults));

            _elasticSearchResult.Data.PagingFilteringContext.PageNumber = command.PageNumber == 0 ? 1 : command.PageNumber;
            _elasticSearchResult.Data.PagingFilteringContext.AllowCustomersToSelectPageSize = false;
            _elasticSearchResult.Data.PagingFilteringContext.LoadPagedList(result);
            _elasticSearchResult.Data.TotalResults = _elasticSearchResult.TotalResult + _elasticSearchResult.NopSearchResults;
            return(searchResult);
        }
        private List <ProductOverviewModel> PopulateFromNop(CatalogPagingFilteringModel command, GsaSetting gsaSetting, int count)
        {
            int diff     = count == 0 ? 0 : gsaSetting.EResultsPerPage - count;
            int pageSize = diff == 0 ? _elasticSearchResult.NopProductsId.Count + gsaSetting.EResultsPerPage : diff;
            var products = _productService.SearchProducts(
                keywords: _elasticSearchResult.SearchKeyword,
                searchDescriptions: true,
                languageId: _workContext.WorkingLanguage.Id,
                orderBy: ProductSortingEnum.NameAsc,
                pageIndex: (command.PageIndex == 0 ? 0 : command.PageIndex - _elasticSearchResult.TotalPages),
                pageSize: pageSize
                );

            if (_elasticSearchResult.NopProductsId.Count > 0)
            {
                foreach (var i in _elasticSearchResult.NopProductsId)
                {
                    var item = products.Where(x => x.Id == i).FirstOrDefault();
                    if (item != null)
                    {
                        products.Remove(item);
                    }
                }
                _elasticSearchResult.NopProductsId = new List <int>();
            }
            else if (diff > 0)
            {
                _elasticSearchResult.NopProductsId.AddRange(products.Select(x => x.Id));
            }
            return(products.ToListProductOverviewModel());
        }