-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from nixtus/feat-autocomplete
feat: use lucene for auto complete searches
- Loading branch information
Showing
9 changed files
with
176 additions
and
130 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
Nixtus.Plugin.Widgets.Lucene/Controllers/LuceneCatalogController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Nixtus.Plugin.Widgets.Lucene.Services; | ||
using Nop.Core; | ||
using Nop.Core.Domain.Catalog; | ||
using Nop.Core.Domain.Media; | ||
using Nop.Services.Catalog; | ||
using Nop.Web.Controllers; | ||
using Nop.Web.Factories; | ||
using System.Linq; | ||
|
||
namespace Nixtus.Plugin.Widgets.Lucene.Controllers | ||
{ | ||
public class LuceneCatalogController : BasePublicController | ||
{ | ||
private readonly LuceneSettings _luceneSettings; | ||
private readonly CatalogSettings _catalogSettings; | ||
private readonly IProductService _productService; | ||
private readonly IStoreContext _storeContext; | ||
private readonly IWorkContext _workContext; | ||
private readonly IProductModelFactory _productModelFactory; | ||
private readonly MediaSettings _mediaSettings; | ||
private readonly ILuceneService _luceneService; | ||
|
||
public LuceneCatalogController(LuceneSettings luceneSettings, CatalogSettings catalogSettings, | ||
IProductService productService, | ||
IStoreContext storeContext, | ||
IWorkContext workContext, | ||
IProductModelFactory productModelFactory, | ||
MediaSettings mediaSettings, ILuceneService luceneService) | ||
{ | ||
_luceneSettings = luceneSettings; | ||
_catalogSettings = catalogSettings; | ||
_productService = productService; | ||
_storeContext = storeContext; | ||
_workContext = workContext; | ||
_productModelFactory = productModelFactory; | ||
_mediaSettings = mediaSettings; | ||
_luceneService = luceneService; | ||
} | ||
|
||
public IActionResult SearchTermAutoComplete(string term) | ||
{ | ||
if (string.IsNullOrWhiteSpace(term) || term.Length < _catalogSettings.ProductSearchTermMinimumLength) | ||
return Content(""); | ||
|
||
//products | ||
var productNumber = _catalogSettings.ProductSearchAutoCompleteNumberOfProducts > 0 ? | ||
_catalogSettings.ProductSearchAutoCompleteNumberOfProducts : 10; | ||
|
||
IPagedList<Product> products = null; | ||
|
||
// use lucene search if enabled | ||
if (_luceneSettings.AutoCompleteSearchEnabled) | ||
{ | ||
_luceneService.GetLuceneDirectory(); | ||
|
||
var luceneProducts = _luceneService.Search(term); | ||
|
||
products = new PagedList<Product>(luceneProducts.AsQueryable(), 0, productNumber); | ||
} | ||
else | ||
{ | ||
products = _productService.SearchProducts( | ||
storeId: _storeContext.CurrentStore.Id, | ||
keywords: term, | ||
languageId: _workContext.WorkingLanguage.Id, | ||
visibleIndividuallyOnly: true, | ||
pageSize: productNumber); | ||
} | ||
|
||
var showLinkToResultSearch = _catalogSettings.ShowLinkToAllResultInSearchAutoComplete && (products.TotalCount > productNumber); | ||
|
||
var models = _productModelFactory.PrepareProductOverviewModels(products, false, _catalogSettings.ShowProductImagesInSearchAutoComplete, _mediaSettings.AutoCompleteSearchThumbPictureSize).ToList(); | ||
var result = (from p in models | ||
select new | ||
{ | ||
label = p.Name, | ||
producturl = Url.RouteUrl("Product", new { SeName = p.SeName }), | ||
productpictureurl = p.DefaultPictureModel.ImageUrl, | ||
showlinktoresultsearch = showLinkToResultSearch | ||
}) | ||
.ToList(); | ||
return Json(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Nixtus.Plugin.Widgets.Lucene/Infrastructure/RouterProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nop.Core.Domain.Localization; | ||
using Nop.Data; | ||
using Nop.Services.Localization; | ||
using Nop.Web.Framework.Mvc.Routing; | ||
using System.Linq; | ||
|
||
namespace Nixtus.Plugin.Widgets.Lucene.Infrastructure | ||
{ | ||
public class RouterProvider : IRouteProvider | ||
{ | ||
public int Priority => 1; | ||
|
||
public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder) | ||
{ | ||
var pattern = string.Empty; | ||
if (DataSettingsManager.DatabaseIsInstalled) | ||
{ | ||
var localizationSettings = endpointRouteBuilder.ServiceProvider.GetRequiredService<LocalizationSettings>(); | ||
if (localizationSettings.SeoFriendlyUrlsForLanguagesEnabled) | ||
{ | ||
var langservice = endpointRouteBuilder.ServiceProvider.GetRequiredService<ILanguageService>(); | ||
var languages = langservice.GetAllLanguages().ToList(); | ||
pattern = "{language:lang=" + languages.FirstOrDefault().UniqueSeoCode + "}/"; | ||
} | ||
} | ||
|
||
// override autocomplete search | ||
endpointRouteBuilder.MapControllerRoute("ProductSearchAutoComplete", $"{pattern}catalog/searchtermautocomplete", | ||
new { controller = "LuceneCatalog", action = "SearchTermAutoComplete" }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.