1

好吧,伙计们,我在这里陷入困境。

基本上我遇到的问题是,当人们在搜索某些东西时,他们在很多时候都错误地输入了代码,例如在搜索时:K00000WEFLZ他们错误地输入了 0,然后产品结果什么也没有返回,我基本上是想尝试让搜索检查搜索是否在字母“K”之后包含一定数量的 0(因为 K 总是会说 10+ 个 ID 号和至少 4-5 个 0),如果它会在搜索操作期间将其替换为“*”,并且无论他们输入的问题有多么错误,您仍然可以找到产品。

我知道我将不得不创建一个自定义类并覆盖它的默认值(但是其中很多无法访问/是私有的),因为默认搜索模式无法更改,因为这将为每个人更改它,我只想要它用于这个特定的站点。

我也不能在开头或结尾使用通配符,因为它有一个庞大的目录,它会将英里数与许多结果相匹配。

据我所知,这是处理默认逻辑类搜索的代码:

protected virtual IList<Product> CreateCustomCollection()
{
        var list = new List<Product>();

        switch (mode)
        {
            case ProductRepeaterMode.Search:

                if (Page.Request.QueryString["search"] != null && Page.Request.QueryString["search"].Length != 0)
                {
                    bool[] customString = new bool[5] { SearchCustomString1, SearchCustomString2, SearchCustomString3, SearchCustomString4, SearchCustomString5 };
                    IList<Product> results = Fabric.ObjectProvider.Get<IProductSearchCommand>().Search(Page.Request.QueryString["search"], out searchWords, IncludeSkus, IsPublicFacing, customString, CoreHttpModule.Session);

                    var retailOrder = WebStoreManager.CurrentOrder as IRetailOrder;
                    var accountOrder = WebStoreManager.CurrentOrder as IAccountOrder;

                    IList<Product> productsToRemove = new List<Product>();
                    IList<Product> productsToAdd = new List<Product>();

                    foreach (var product in results)
                    {
                        if (hideRestrictedProducts)
                        {
                            if (retailOrder != null)
                            {
                                if (!product.CanBePurchasedByRetailCustomer || product.AgentOnly)
                                    productsToRemove.Add(product);
                            }
                            else
                            {
                                if (accountOrder != null)
                                {
                                    var add = false;

                                    if (product.CanBePurchasedOnAccount)
                                        add = true;

                                    if (product.AgentOnly)
                                    {
                                        if (accountOrder.Agent != null)
                                            add = true;
                                        else
                                            add = false;
                                    }

                                    if (!add)
                                        productsToRemove.Add(product);
                                }
                            }
                        }

                        // Replace SKUs with lines
                        if (resolveSkusToLines)
                        {
                            var sku = product.Role as SkuProductRole;
                            if (sku != null)
                            {
                                productsToRemove.Add(product);
                                if (sku.Owner != null && sku.Owner.Product != null)
                                {
                                    var line = sku.Owner.Product;
                                    if (!results.Contains(line) && !productsToAdd.Contains(line))
                                        productsToAdd.Add(line);
                                }
                            }
                        }
                    }

                    foreach (Product product in productsToAdd)
                    {
                        results.Add(product);
                    }

                    foreach (Product product in productsToRemove)
                    {
                        results.Remove(product);
                    }

                    foreach (var result in results)
                        list.Add(result);
                }
                break;
        }
        return list;
    }
4

1 回答 1

2

逻辑模糊,喜欢。我这样做的方式是,从搜索字符串本身构建一个正则表达式,它并没有说明它应该如何完成,但我会尽我最大的努力。

将正则表达式构建器视为自定义构建的压缩操作。从您的字符数组开始并从中构建正则表达式搜索,每当您在一行中找到 2 个相同的字符时,将第二个字符(并忽略第二个字符之外的任何其他字符)替换为“+”字符,然后使用正则表达式运行生成的搜索而不是精确的字符串匹配。

K00000WEFLZ会变成K0+WEFLEZ,并匹配 K,1 个或多个 0,WEFLEZ。该算法需要对任何重复的字符执行此操作,因此它可能会有点傻。类似的事情KK000WWLEFF22会来K+0+W+LEF+2+。搜索字符串并没有那么好,并且可能匹配很多您不想要的东西......但有效。或者您可以将其限制为仅替换 0 或 0。等等等等......无论最终效果如何。

我推荐的另一种方式是实时过滤。但它的用处更多地取决于预期的正常功能。用户输入值会更常见,还是更常见的是他们从其他地方复制/粘贴它。在第二种情况下,实时过滤完全没用。否则...重新过滤每个 keyPress 或 TextChanged 事件的列表。至少那时他们可能会知道整个列表何时消失,因为输入了一个额外的 0。

编辑 - 添加代码示例

private string RegStringZipper(string searchString)
    {
        StringBuilder sb = new StringBuilder();
        char lastChar = new char();
        bool plusFlag = false;
        foreach (char c in searchString)
        {
            if (sb.Length == 0)
            {
                sb.Append(c);
                lastChar = c;
            }
            else
            {
                if (lastChar == c)
                {//we have a repeating character
                    //Note: Here is also where if you only wanted to filter a specific character, like 0, you would check for it.
                    if (!plusFlag)
                    {//we have not already added the +
                        sb.Append('+');
                        plusFlag = true;
                    }
                    //else do nothing, skip the characer
                }
                else
                {
                    sb.Append(c);
                    plusFlag = false;
                    lastChar = c;
                }
            }
        }
        return sb.ToString();
    }

至于我会将它放入您的代码中的哪个位置...这实际上取决于该搜索功能的实际工作方式,它不是我以前玩过的东西...说到这...如果它按看起来的方式工作喜欢它可能会工作,在上面的代码中用'+'换成'*'......

if (Page.Request.QueryString["search"] != null && Page.Request.QueryString["search"].Length != 0)
            {
                bool[] customString = new bool[5] { SearchCustomString1, SearchCustomString2, SearchCustomString3, SearchCustomString4, SearchCustomString5 };
                string SearchString = RegStringZipper(Page.Request.QueryString["search"]);
                //please note, given that I dont know what FabricProvider.Search works on, I dont actually know that this works as intended.
                IList<Product> results = Fabric.ObjectProvider.Get<IProductSearchCommand>().Search(SearchString, out searchWords, IncludeSkus, IsPublicFacing, customString, CoreHttpModule.Session);
于 2012-06-08T15:35:09.577 回答