如何在MongoDB中搜索动态字段并首先对最佳匹配结果进行排序 [英] How to search on dynamic field and sort best match result first in MongoDB

查看:71
本文介绍了如何在MongoDB中搜索动态字段并首先对最佳匹配结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个文档,需要对文档进行排序,而排序字段名称未知,如果输入搜索字段与任何文档匹配,则对文档进行排序,并根据最佳结果进行排序.并且搜索输入不是确切的名称,它可能是正则表达式示例数据:

<预><代码>[{cat_id":1,类别代码":类别代码1",类别名称":类别名称1",cat_type":A",描述":男士上衣"},{cat_id":2,categoryCode":categoryCode2",类别名称":类别名称2",cat_type":A",描述":衬衫"},{cat_id":3,categoryCode":categoryCode3","categoryName":"categoryName3",cat_type":女士上衣",描述":布"},{cat_id":4,categoryCode":categoryCode4","categoryName":"categoryName4",cat_type":A",描述":女性"}]

如果 search='Shirt' 那么响应是:

<预><代码>[{cat_id":2,categoryCode":categoryCode2",类别名称":类别名称2",cat_type":A",描述":衬衫"},{cat_id":1,类别代码":类别代码1",类别名称":类别名称1",cat_type":A",描述":男士上衣"}]

如果 search='Women Top Wear' 那么响应是:

<预><代码>[{cat_id":3,categoryCode":categoryCode3","categoryName":"categoryName3",cat_type":女士上衣",描述":布"},{cat_id":4,categoryCode":categoryCode4","categoryName":"categoryName4",cat_type":A",描述":女性"}]

当您必须搜索任何输入时,它会检查该值是否存在于所有 5 个属性中,如果存在,则返回该文档并按最佳匹配排序

解决方案

您为此使用了错误的数据库,Mongo 不是搜索引擎,因此使用它来满足这些需求总是有其局限性.我建议您考虑更合适的解决方案,例如 elasticsearchmongo 图集搜索.

为此使用 Mongo 的最大问题是您的要求:

<块引用>

按最佳匹配排序

最佳匹配的确切定义是什么?我建议的搜索引擎对这些事情有一个内置的评分机制.但如果我们要在 Mongo 中进行,您将必须自己定义逻辑,这基本上等同于为数据库构建一个完整的评分方法(如果您希望它稳定和成功,这不是简单的任务).

我不会费心为此添加 puesdo 代码,因为我建议不要这样做,但是正如您提到的,您基本上必须进行正则表达式搜索.一旦您匹配了文档,那么就只剩下基于您要定义的用于对最佳"进行排序的逻辑的分数计算了.匹配.

I have multiple documents and need to sort the document while sorting field name is unknown sort the document if input search field matches any of the document and sort based on the best result. and also searching input is not exact name it could be regex Sample Data :

[
   {
      "cat_id":1,
      "categoryCode":"categoryCode1",
      "categoryName":"categoryName1",
      "cat_type":"A",
      "description":"Mens Upper Shirt"
   },
   {
      "cat_id":2,
      "categoryCode":"categoryCode2",
      "categoryName":"categoryName2",
      "cat_type":"A",
      "description":"Shirt"
   },
   {
      "cat_id":3,
      "categoryCode":"categoryCode3",
      "categoryName":"categoryName3",
      "cat_type":"Women Top wear",
      "description":"cloths"
   },
   {
      "cat_id":4,
      "categoryCode":"categoryCode4",
      "categoryName":"categoryName4",
      "cat_type":"A",
      "description":"Women"
   }
]

if search='Shirt' then the response is:

[
   {
      "cat_id":2,
      "categoryCode":"categoryCode2",
      "categoryName":"categoryName2",
      "cat_type":"A",
      "description":"Shirt"
   },
   {
      "cat_id":1,
      "categoryCode":"categoryCode1",
      "categoryName":"categoryName1",
      "cat_type":"A",
      "description":"Mens Upper Shirt"
   }
]

if search='Women Top Wear' then response is :

[
   {
      "cat_id":3,
      "categoryCode":"categoryCode3",
      "categoryName":"categoryName3",
      "cat_type":"Women Top wear",
      "description":"cloths"
   },
   {
      "cat_id":4,
      "categoryCode":"categoryCode4",
      "categoryName":"categoryName4",
      "cat_type":"A",
      "description":"Women"
   }
]

while any input you have to search it will check this value exist in all 5 attributes if exists then return that document and sort by best match

解决方案

You are using the wrong database for this, Mongo is not a search engine and thus using it for these needs will always have it's limits. I recommend you consider a more proper solution like elasticsearch or mongo atlas search.

The biggest issue with using Mongo for this is your requirement of:

sort by best match

What exactly defines a best match? the search engines i suggest have a built in scoring mechanism for such things. but if we're going to do it in Mongo you will have to define the logic yourself which is basically equal to building an entire scoring method for a database ( which if no simple task if you want it stable and successful ).

I will not bother with adding puesdo code to this as I suggest against doing this, but as you mentioned you basically have to do a regex search. once you matched the documents then just score calculations are left based on the logic you want to define for sorting the "best" match.

这篇关于如何在MongoDB中搜索动态字段并首先对最佳匹配结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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