In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Fuzzy query of information is often needed on some information management platforms, the earliest time is the fuzzy query on a certain field, but the information returned at this time will not be very accurate, because only A field or B field can be checked, and a very simple full-text search is realized in MongoDB.
Example: define a new collection
Db.news.insert ({"title": "stoneA", "content": "ttA"})
Db.news.insert ({"title": "stoneB", "content": "ttB"})
Db.news.insert ({"title": "stoneC", "content": "ttC"})
Db.news.insert ({"title": "stoneD", "content": "ttD"})
Example: create a full-text index
> db.news.createIndex ({"title": "text", "content": "text"})
{
"createdCollectionAutomatically": false
"numIndexesBefore": 1
"numIndexesAfter": 2
"ok": 1
}
Example: implement fuzzy query of data
If you want to represent a full-text search, use the "$text" judge, and if you want to query the data, use the "$search" operator:
Keyword specified by ● query: {"$search": "query keyword"}
● query multiple keywords (or relationships): {"$search": "query keywords query keywords."}
● query multiple keywords (and relations): {"$search": "\" query keywords\ "\" query keywords\ "..."}
● query multiple keywords (excluding one): {"$search": "query keywords query keywords...-troubleshoot keywords"}
Example: query individual content
> db.news.find ({"$text": {"$search": "stoneA"}})
{"_ id": ObjectId ("5992c4310184ff511bf02bbb"), "title": "stoneA", "content": "ttA"}
Example: the query contains information for "stoneA" and "stoneB"
> db.news.find ({"$text": {"$search": "stoneA stoneB"}})
{"_ id": ObjectId ("5992c4310184ff511bf02bbc"), "title": "stoneB", "content": "ttB"}
{"_ id": ObjectId ("5992c4310184ff511bf02bbb"), "title": "stoneA", "content": "ttA"}
Example: query contains both "ttC" and "ttD"
> db.news.find ({"$text": {"$search": "\" ttC\ "\" ttD\ "}})
{"_ id": ObjectId ("5992c61d0184ff511bf02bc1"), "title": "stoneC", "content": "ttC ttD ttE"}
{"_ id": ObjectId ("5992c61d0184ff511bf02bc2"), "title": "stoneD", "content": "ttC ttD ttF"}
Example: the query contains "ttE" but not "ttF"
> db.news.find ({"$text": {"$search": "ttE-ttF"}})
{"_ id": ObjectId ("5992c61d0184ff511bf02bc1"), "title": "stoneC", "content": "ttC ttD ttE"}
However, in the full-text retrieval operation, we can also use the similarity score to judge the retrieval results.
Example: score query results
> db.news.find ({"$text": {"$search": "ttC ttD ttE"}}, {"score": {"$meta": "textScore"}}) .sort ({"score": {"$meta": "textScore"}})
{"_ id": ObjectId ("5992c61d0184ff511bf02bc1"), "title": "stoneC", "content": "ttC ttD ttE", "score": 2}
{"_ id": ObjectId ("5992c61d0184ff511bf02bc2"), "title": "stoneD", "content": "ttC ttD ttF", "score": 1.3333333333333333}
Ranking according to the scored results, you can actually achieve a more accurate information search.
If there are too many fields in a collection, it is troublesome to set a full-text index for each field, which is simpler, and you can set a full-text index for all fields.
Example: set up a full-text index for all fields
> db.news.dropIndexes ()
{
"nIndexesWas": 2
"msg": "non-_id indexes dropped for collection"
"ok": 1
}
> db.news.createIndex ({"$* *": "text"})
{
"createdCollectionAutomatically": false
"numIndexesBefore": 1
"numIndexesAfter": 2
"ok": 1
}
This is the easiest way to set up a full-text index, but don't use it as much as possible and it will be slow.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.