如何在文档字段MongoDB中找到相似性?(How to find similarity in document field MongoDB?)
给定的数据如下:
{'_id': 'foobar1', 'about': 'similarity in comparison', 'categories': ['one', 'two', 'three']} {'_id': 'foobar2', 'about': 'perfect similarity in comparison', 'categories': ['one']} {'_id': 'foobar3', 'about': 'partial similarity', 'categories': ['one', 'two']} {'_id': 'foobar4', 'about': 'none', 'categories': ['one', 'two']}
我想找到一种方法来获得单个项目与集合中所有其他项目之间的相似性,然后按照最高相似度的顺序返回它们。 相似性是基于共同的单词数,已经有一个
int similar(String one, String two)
的函数int similar(String one, String two)
例如:如果我想要
foobar1
about
字段的相似性列表,它将返回
[{'_id': 'foobar2'}, {'_id': 'foobar3'}, {'_id': 'foobar4'}]
我正在使用morphia这样做,但只有mongoDB实现,我可以想出其余部分
Given data that looks like this:
{'_id': 'foobar1', 'about': 'similarity in comparison', 'categories': ['one', 'two', 'three']} {'_id': 'foobar2', 'about': 'perfect similarity in comparison', 'categories': ['one']} {'_id': 'foobar3', 'about': 'partial similarity', 'categories': ['one', 'two']} {'_id': 'foobar4', 'about': 'none', 'categories': ['one', 'two']}
I would like to find a way to get a similarity between a single item and all other items in the collection then return them in order of highest similarity. Similarity is based on number of words in common, there is already a function
int similar(String one, String two)
For example: if I want the similarity list for
about
field offoobar1
, it would return
[{'_id': 'foobar2'}, {'_id': 'foobar3'}, {'_id': 'foobar4'}]
I am doing this with morphia, but with just the mongoDB implementation, I could figure the rest out
原文:https://stackoverflow.com/questions/38365414
最满意答案
不会。创建新任务后,您的try块将退出。
你可以捕捉异常。 请参阅: 使用Task.Factory获取更多信息时出现捕获错误 。
No. Your try block will exit after the new task is created.
You can catch exceptions though. Please see: Catching Error when using Task.Factory for more information.
相关问答
更多-
由于方法签名是相同的,你会得到一个模糊的调用错误。 返回值不是签名的一部分。 由于您没有提供明确的返回类型,因此编译器不知道要采取哪种方式。 C#中的方法签名 You get an ambiguous call error because the method signature is the same. Return values are not part of the signature. Since you don't provide an explicit return type, the comp ...
-
Task.Run是具有特定安全参数的Task.Run的缩写: Task.Factory.StartNew( action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); 它被添加到.Net 4.5中,以帮助日益频繁地将async和卸载工作用于ThreadPool 。 Task.Factory.StartNew (在.Net 4.0中添加TPL)更 ...
-
您的LoadSummoner方法应该返回Task : private async Task LoadSummonerAndLeaguesAsync() { summoner = await riotApi.GetSummonerAsync(region, summonerName); leagues = await summoner.GetLeaguesAsync(); } 然后你就这样称呼它: LoadSummonerAndLeaguesAsync().ContinueWith(init ...
-
我发现Stephen Toub发表了这篇伟大的文章,它解释说,当使用new Task(...).Start() ,起始方法需要使用同步来确保任务只被安排一次。 他的建议是喜欢使用.net 4.0的Task.Factory.StartNew 。 对于.net 4.5 Task.Run是更好的选择 。 I found this great article by Stephen Toub, which explains that there is actually a performance penalty wh ...
-
您需要使用_synchronizationContext.Send或_synchronizationContext.Post与UI线程进行通信。 另外,根据你提到的内容,我认为你还需要在调用上述任一函数时传递“false”,以便它知道该函数不是从UI线程调用的。 _synchronizationContext = SynchronizationContext.Current As @r00tdev explained, using the SynchronizationContext() class is ...
-
在MVC 4异步ApiController中使用Task.Factory.StartNew?(Use Task.Factory.StartNew in MVC 4 async ApiController?)[2022-10-30]
是的,你说的大部分内容都是正确的。 甚至完成端口等细节。 这是一个小错误: 我假设原始的工作线程正在等待这个UploadFromStream完成? 只有你的任务线程正在运行。 毕竟你使用的是异步管道。 它不会等待任务完成,它只是连接一个延续。 (就像使用HttpClient.GetAsync一样)。 Yes, most of what you say is correct. Even down to the details with completion ports and such. Here is a ... -
Task.Factory.StartNew在部署时不执行任务(Task.Factory.StartNew not executing the task when deployed)[2022-02-25]
有时候这种行为是一个重载的ThreadPool的指示。 看到这些是长时间运行/阻塞任务,它们不应该被安排在ThreadPool运行,这是Task.Factory.StartNew将使用默认的TaskScheduler发送它们的地方。 国际海事组织, Task.Factory.StartNew可能不是最适合这个,你最好旋转自己的线程来运行这些循环。 ThreadStart action=()=>{ //do your thing }; Thread thread=new Thread(action){ ... -
try / catch是否在Task.Factory.StartNew之外工作?(Does try/catch work outside of Task.Factory.StartNew? [duplicate])[2022-09-13]
不会。创建新任务后,您的try块将退出。 你可以捕捉异常。 请参阅: 使用Task.Factory获取更多信息时出现捕获错误 。 No. Your try block will exit after the new task is created. You can catch exceptions though. Please see: Catching Error when using Task.Factory for more information. -
Rx Task.Factory.StartNew在.Net 3.5问题上启动两个任务(Rx Task.Factory.StartNew starts two tasks on .Net 3.5 issue)[2022-08-12]
你在lambda中使用循环变量吗? 例如 foreach (var foo in blah) { // You're capturing foo inside a lambda -- this probably won't do what you think it will! Task.Factory.StartNew(() => foo.Something()); } 请参阅为什么在lambda表达式中使用迭代变量不好? 更新2012年11月 :新的C#5编译器通过改变捕获foreach循 ... -
通常,您通过调用Task.WaitAll来阻止主线程。 很可能Foo类试图将回调发布到该线程上,这将永远不会成功。 这可以解释为什么你从未真正“完成”任务。 通常,更好的方法是尽可能避免轮询(例如,一个选项是将Task / Task
暴露给调用者,因此您可以在那里附加延续)。 但是,如果您无法更改Foo的实现,则可以将其切换为处理将数据设置为延续而不是阻止: // Don't block // Task.WaitAll(new Task[] { waitForComplete }); // Inste ...