MongoDB的子产品搜索按最高的匹配顺序 [英] MongoDB substring product search order by highest match

查看:134
本文介绍了MongoDB的子产品搜索按最高的匹配顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不MongoDB中那么好。这就是使用这个我的第一个项目。我工作的一些商店的网站。我们使用C#和MongoDB的最新的C#驱动程序。我需要一个想法如何实现我的算法过滤根据用户的输入法产品。以下是我的算法工作/应该工作。




  1. 用户类型的东西,如蓝色阿迪达斯男士蜘蛛侠纯棉T恤

  2. 我分裂整个短语成单个的词,所以我有蓝色,阿迪达斯,男性,蜘蛛侠,棉,T恤

  3. 我对证零售商,颜色,类别名称的每个字在我的分贝。因此,这里是怎么一回事呢。




    • 蓝 - 哦,这是一个彩色滤光片让我MongoDB的文档中筛选该对正确的字段

    • 阿迪达斯 - 好吧,这是零售商。执行相同点上方

    • 人 - 这是一个类别。好让我们只搜索这一类

    • 棉 - 我不通过织物过滤。无法通过这个过滤器

    • 蜘蛛侠 - definetly不是一个零售商,颜色等不能过滤使用该

    • T恤 - 确定。它的出现为子类别的孩子,男人,女人类的名字,但因为我已经有人作为我的类我只是去一点点深入到我的品类结构 - 现在更新我的categoryId我要过滤反对它的词典定义。在男人类别恤类别。




    • 确定。我分析几个过滤器PARAMS来自用户的输入,但我仍然有棉花和蜘蛛侠字。而这也正是我迷路了。





我想不要忽视这个蜘蛛侠和棉字。我想真正得到从数据库才能在其产品名称的所有资料字覆盖,所以我预期的结果(OFC所有的零售商,颜色等滤镜应用先出):



产品名称中既有蜘蛛侠和棉产品
产品名称中无论是蜘蛛侠或棉



我怎么能做到这一点?


解决方案

首先,你可以使用的 &安培; 运营商的是这样的:

  VAR建设者=建设者<产品与GT; .Filter; 
FilterDefinition<产品与GT;过滤= builder.Empty;
滤波器放大器; = builder.Eq(颜色,蓝);
滤波器放大器; = builder.Eq(零售商,阿迪达斯);
滤波器放大器; = builder.Eq(类别,男);



然后,您可以使用的 正则表达式 过滤产品的名称,其中包含任何休息的话/其他的所有的话。


搜索(名称中包含棉花或蜘蛛侠)

  VAR restWords =新的字符串[] {棉,蜘蛛侠}; 
变种orReg =新System.Text.RegularExpressions.Regex(的string.join(|,restWords));
滤波器放大器; = builder.Regex(姓名,BsonRegularExpression.Create(orReg));

名单,LT;产品与GT; filteredList = products.Find(过滤器).ToListAsync()结果。

搜索(名称中包含棉和蜘蛛侠)

 的foreach(在restWords VAR字)
{
滤波器放大器; = builder.Regex(名称BsonRegularExpression.Create(新System.Text.RegularExpressions.Regex(字)));
}

名单,LT;产品与GT; filteredList = products.Find(过滤器).ToListAsync()结果。


I'm not so good in Mongodb. That's my first project using this. I'm working on some sort of shop website. We're using C# and newest C# driver for mongodb. I need an idea how to implement my algorithm to filter products based on user's input. Here's how my algorithm works/should work.

  1. User type something like "Blue adidas men spiderman cotton t-shirt"
  2. I split whole phrase into single words so I have "blue", "adidas", "men", "spiderman", "cotton", "t-shirt"
  3. I check each word against retailer, color, category names in my db. So here's how it goes.

    • "Blue" - oh, it's a color filter let's filter this against proper field in my mongodb document.
    • "adidas" - ok, it's retailer. Do the same as in point above
    • "men" - it's a category. Ok so let's search only in this category.
    • "cotton" - I don't filter by textile. Can't filter by this.
    • "spiderman" - definetly not a retailer, color etc. Can't filter using this.
    • "t-shirt" - Ok. It's category name that appears as child category for kids, men, women but since I already have "men" as my category I just go a little bit deeper into my category structure - Now I update my categoryId I want to filter against and it's t-shirt category under men category.

      1. Ok. I parsed few filter params from user input but I still have cotton and spiderman word. And that's where I'm lost.

I would like to not ignore this "spiderman" and "cotton" words. I would like to actually get ALL items from database order by word coverage in its product name so my expected result (ofc with all retailer, color etc filters applied first) are:

products that names contain both "spiderman" and "cotton" products products that names contain either "spiderman" or "cotton"

How can I do that?

解决方案

First of all, you can combine multiple filters with using & operator like this:

var builder = Builders<Product>.Filter;
FilterDefinition<Product> filter = builder.Empty;
filter &= builder.Eq("Color", "blue");
filter &= builder.Eq("Retailer", "adidas");
filter &= builder.Eq("Category", "men");

Then, you can use Regex to filter the products the names of which contain any rest words/all rest words.
OR search (the name contains "cotton" OR "spiderman")

var restWords = new string[] { "cotton", "spiderman" };
var orReg = new System.Text.RegularExpressions.Regex(string.Join("|", restWords));
filter &= builder.Regex("Name", BsonRegularExpression.Create(orReg));

List<Product> filteredList = products.Find(filter).ToListAsync().Result;

AND search (the name contains "cotton" AND "spiderman")

foreach (var word in restWords)
{
    filter &= builder.Regex("Name", BsonRegularExpression.Create(new System.Text.RegularExpressions.Regex(word)));
}

List<Product> filteredList = products.Find(filter).ToListAsync().Result;

这篇关于MongoDB的子产品搜索按最高的匹配顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆