Product Search free text search for color showing products with searched color matching variant not published for channel

_
I have a free text search query. When i search for a color, i get products even which has that color variant unpublished for that active channel, which should not happen ideally. Code block attached

    public override SearchResponse Search(SearchQuery searchQuery,
                IDictionary<string, ISet<string>> tags = null,
                bool addPriceFilterTags = false,
                bool addNewsFilterTags = false,
                bool addCategoryFilterTags = false)
            {
                var pageModel = _requestModelAccessor.RequestModel.CurrentPageModel;
                var isBrandPageType = pageModel.IsBrandPageType();
                if (string.IsNullOrEmpty(searchQuery.Text) && searchQuery.CategorySystemId.GetValueOrDefault() == Guid.Empty && searchQuery.ProductListSystemId == null && !isBrandPageType)
                {
                    return null;
                }
                var searchQueryBuilder = _searchQueryBuilderFactory.Create(CultureInfo.CurrentUICulture, ProductCatalogSearchDomains.Products, searchQuery);
                if (isBrandPageType)
                {
                    if (tags != null)
                    {
                        if (!tags.ContainsKey(BrandListViewModel.TagName))
                        {
                            tags.Add(BrandListViewModel.TagName, new SortedSet<string>(new[] { pageModel.Page.Localizations.CurrentUICulture.Name }));
                        }
                    }
                    else
                    {
                        tags = new Dictionary<string, ISet<string>> { { BrandListViewModel.TagName, new SortedSet<string>(new[] { pageModel.Page.Localizations.CurrentUICulture.Name }) } };
                    }
                }
                searchQueryBuilder.ApplySelectedFilterTags(tags);
                if (addCategoryFilterTags)
                {
                    searchQueryBuilder.ApplySelectedFilterCategories();
                }
                if (addPriceFilterTags)
                {
                    searchQueryBuilder.ApplyPriceFilterTags();
                }
                if (addNewsFilterTags)
                {
                    searchQueryBuilder.ApplyNewsFilterTags();
                }
                searchQueryBuilder.AddFilterReadTags();
                searchQueryBuilder.ApplyProductCatalogDefaultSearchTag();
                if (searchQuery.ProductListSystemId == null)
                {
                    searchQueryBuilder.ApplyCategoryTags();
                    searchQueryBuilder.ApplyCategorySorting();
                    searchQueryBuilder.ApplyDefaultSortOrder();
                    //searchQueryBuilder.ApplyFreeTextSearchTags(); Removing the default free text search with search for specific fields in free text in line 140
                }
                else
                {
                    searchQueryBuilder.ApplyProductListSorting();
                }
                var request = searchQueryBuilder.Build();
                request.ReadTags.Add(VariantIndexDocumentMerger.MergeTagName);
                request.ReadTags.Add(TagNames.ArticleNumber);
                request.ReadTags.Add(TagNames.Name);
                request.ReadTags.Add("Color".GetFieldDefinitionForProducts().GetTagName(CultureInfo.CurrentUICulture));
                request.ReadTags.Add("RSKNumber".GetFieldDefinitionForProducts().GetTagName());
                request.ReadTags.Add("ProductDescription".GetFieldDefinitionForProducts().GetTagName(CultureInfo.CurrentUICulture));
                if (!string.IsNullOrWhiteSpace(searchQuery.Text))
                {
                    var optionalTagClause = new OptionalTagClause();
                    optionalTagClause.Tags.Add(new Tag(TagNames.VariantArticleNumbers, searchQuery.Text));
                    optionalTagClause.Tags.Add(new Tag("ProductDescription".GetFieldDefinitionForProducts()?.GetTagName(CultureInfo.CurrentUICulture), $"*{searchQuery.Text.Replace(" ", "\\ ")}*") { AllowFuzzy = false, Analyzer = "whitespace" });
                    optionalTagClause.Tags.Add(new Tag("_name".GetFieldDefinitionForProducts()?.GetTagName(CultureInfo.CurrentUICulture), $"*{searchQuery.Text.Replace(" ", "\\ ")}*") { AllowFuzzy = false, Analyzer = "whitespace" });
                    optionalTagClause.Tags.Add(new Tag("Color".GetFieldDefinitionForProducts().GetTagName(CultureInfo.CurrentUICulture), searchQuery.Text));
                    int rskNumberSearchValue = 0;
                    int.TryParse(searchQuery.Text, out rskNumberSearchValue);
                    if (rskNumberSearchValue>0)
                    {
                        optionalTagClause.Tags.Add(new Tag("RSKNumber".GetFieldDefinitionForProducts().GetTagName(), rskNumberSearchValue));
                    }
                    request.FilterTags.Add(optionalTagClause);
                }
                request.FilterTags.Add(new Tag(TagNames.ActiveChannelSystemId, _requestModelAccessor.RequestModel.ChannelModel.Channel.SystemId));
                var searchResponse = _searchService.Search(request);
                var priceTagName = _requestModelAccessor.RequestModel.Cart.IncludeVAT ? "-price-incl_vat" : "-price";
                var searchResult = searchResponse.Hits
                                                 .Select(hit =>
                                                 {
                                                     var priceTags = hit.Tags
                                                                        .Where(x => x.Name.EndsWith(priceTagName, StringComparison.OrdinalIgnoreCase))
                                                                        .Select(x => x.OriginalValue)
                                                                        .Cast<decimal>()
                                                                        .DefaultIfEmpty(decimal.MinusOne)
                                                                        .Min();
                                                     return new SortableSearchHit(hit) { Price = priceTags };
                                                 })
                                                 .ToList();
                if (addPriceFilterTags && (searchQuery.ContainsPriceFilter() || searchQuery.SortBy == SearchQueryConstants.Price))
                {
                    searchResult = FilterAndSortOnPrice(searchResult, searchQuery);
                }
                return new SearchResponse
                {
                    ETag = searchResponse.ETag,
                    Hits = new Collection<Hit>(searchResult.Cast<Hit>().ToList()),
                    MaxScore = searchResult.Count == 0 ? 0 : searchResult.Max(x => x.Score),
                    TotalHitCount = searchResult.Count
                };
            }

The searchquerybuilder.ApplyProductCatalogDefaultSearchTag() has the following definition

public void ApplyProductCatalogDefaultSearchTag()
        {
            _request.FilterTags.Add(new Tag(TagNames.ActiveChannelSystemId, _requestModelAccessor.RequestModel?.ChannelModel?.SystemId ?? Guid.Empty));
            _request.FilterTags.Add(new Tag(TagNames.AssortmentSystemId, _requestModelAccessor.RequestModel?.ChannelModel?.Channel?.MarketSystemId?.MapTo<MarketModel>()?.Market.AssortmentSystemId ?? Guid.Empty));
            _request.FilterTags.Add(new Tag(TagNames.HasPrice, true));
            if (_searchQuery.ProductListSystemId != null)
            {
                _request.FilterTags.Add(new Tag(TagNames.ProductListSystemId, _searchQuery.ProductListSystemId.Value) { AllowFuzzy = false });
            }
        }_

Litium version: [7.4.0]

Did you rebuild the search indices?

Are you using the base product or variant url-type? If using the base product all the variants are persisted into one document that are used for search and the possibility to filter on individual variants values are not possible with the Lucene.Net implementation that you are using. This behavior have been rebuilt with Elasticsearch implementation that was released with Litium 7.4 where the document are per channels to make this type of filtering possible.

yes i did rebuild of indexes

That could be the issue. Let me have a look on how i can work with Elastic search implementation

We are using shared hosting for our customer. So can we use ElasticSearch implementation for the shared hosting

If the hosting is at Litium Cloud, you need to contact support@litium.com that can help you with an answer for the specific customer.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.