在MongoDB全文搜索中搜索特定字段中的值 [英] Searching a value in a specific field in MongoDB Full Text Search

查看:471
本文介绍了在MongoDB全文搜索中搜索特定字段中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(MongoDB全文搜索)

你好,
我在索引中放了一些字段,这就是我可以搜索 EM> SEARCH_KEYWORD 即可。

  BasicDBObject search = new BasicDBObject($ search,search_keyword); 
BasicDBObject textSearch = new BasicDBObject($ text,search);

DBCursor cursor = users.find(textSearch);

我不希望 search_keyword 在索引中指定的所有字段中搜索。 *是否有任何方法指定 search_keyword 在索引的特定字段中搜索?*



所以,请给我一些想法 Java



谢谢。

解决方案

如果您想索引单个字段并搜索它,那么它就是默认情况下的工作方式。假设你想索引字段 companyName 。当您对此集合执行 $ text 搜索时,仅会使用 companyName 字段中的数据,因为您只包含该字段在您的索引中。

现在第二种情况,您的 $ text 索引包含多个字段。在这种情况下,您不能将搜索限制为仅查找从特定字段索引的值。 $ text 索引是在集合级别构建的,集合最多只能有一个 $ text 索引。在这种情况下,您可以选择限制搜索特定字段,而不是使用 regex 。 MongoDB具有满足其他场景需求的灵活性,但是如果您的应用程序严重受到搜索驱动,而且您主要是在全面开发之后使用其他技术,那么您也可以使用其他技术进行评估。

文本搜索引擎,用于通过丰富的查询语法按关键字查找文档。 ElasticSearch 可能是此处的替代方案。它真的取决于应用程序的类型和您的需求。


(MongoDB Full Text Search)

Hello, I have put some fields in index and this is how I could search for a search_keyword.

BasicDBObject search = new BasicDBObject("$search", "search_keyword"); 
BasicDBObject textSearch = new BasicDBObject("$text", search);

DBCursor cursor = users.find(textSearch);

I don't want search_keyword to be searched in all the fields specified in the index. *Is there any method to specify search_keyword to be searched in specific fields from the index??*

If so, please give me some idea how to do it in Java.

Thank you.

解决方案

If you want to index a single field and search for it then it is the way it works by default. Lets say you want to index the field companyName. When you perform $text search on this collection, only the data from the companyName field will be used because you only included that field in your index.

Now the second scenario, your $text index includes more than one field. In this case you cannot limit the search to only look for values indexed from a specific field. The $text index is constructed on the collection level and a collection can have at most one $text index. Your option to limit search on specific field in this case may be to use regex instead.

MongoDB has the flexibility to fulfil requirements of other scenarios, but you can also evaluate using other technologies if your application is heavily search-driven and you are primarily after a full-text search engine for locating documents by keyword with a rich query syntax. ElasticSearch might be an alternative here. It really depends on the type of the application and your needs.

这篇关于在MongoDB全文搜索中搜索特定字段中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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