Searching a value in a specific field in MongoDB Full Text Search

别来无恙 提交于 2019-12-18 05:57:34

问题


(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.


回答1:


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.



来源:https://stackoverflow.com/questions/24636473/searching-a-value-in-a-specific-field-in-mongodb-full-text-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!