Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb Index (3)

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Full-text index

MongoDB has a special index for searching for text in documents. Previous blogs used exact matches to query strings, and these techniques have certain limitations. The speed of searching large chunks of text is very slow, and can not deal with the issue of natural language etiquette. Full-text indexing is done using the idea of "inverted indexing", which is the same as the current very open source lucene (full-text retrieval) project. Full-text indexing can be used to search text very quickly. MongoDB supports multiple languages, but unfortunately, Chinese is not supported in the free version. Looking up MongoDB's official website, you can see that installing a third-party plug-in in the enterprise version can support full-text indexing in Chinese.

To use full-text retrieval, you need to specifically turn on this feature before you can use it. Full-text indexing can be enabled by specifying the-- setParameter textSearchEnabled=true option when starting MongoDB, or by executing the setParameter command at run time. Such as db.adminCommand ({"setParameter": 1, "textSearchEnabled": true})

Example:

Db.news.insert ({"title": "SEOUL", "context": "SEOUL, June 10 (Reuters)-South Korean prosecutors raided the offices of Lotte Group, the country's fifth-largest conglomerate, and several affiliates on Friday, dealing a further blow to its hotel unit's planned IPO, billed as the world's biggest this year."})

Db.news.insert ({"title": "Many Chinese people", "context": "Many Chinese people think that a job on a diplomatic team is too good to quit. So when 28-year-old Liu Xiaoxi left her embassy post as an attache late last year to start a career in photography, she quickly became a popular topic among the Chinese online community."})

Db.news.insert ({"title": "About", "context": "About 200 investigators searched 17 locations including group headquarters in central Seoul and the homes of Chairman Shin Dong-bin and other key executives, local news agency Yonhap reported, citing the Seoul Central Prosecutor's office."})

Db.news.insert ({"title": "Three people", "context": "Three people with direct knowledge of the matter told Reuters that Friday's raids were part of an investigation into a possible slush fund. They also declined to be identified."})

Db.news.insert ({"title": "A Lotte Group spokesman", "context": "A Lotte Group spokesman on Friday declined to comment on the reason for the raid, when asked whether it concerned a possible slush fund. He noted, however, that the situation was difficult given the IPO plans and Lotte Chemical's Axiall bid."})

Db.news.insert ({"title": "According", "context": "According to bourse rules, the deadline for Hotel Lotte to list is July 27, six months from the preliminary approval for the IPO. If it needed to refile its prospectus to warn investors about risks from Friday's probe, which appeared likely, it would probably not be able to meet that deadline, an exchange official told Reuters on Friday."})

Db.news.insert ({"title": "Friday", "context": "On Friday, dozens of Chinese tourists queued as usual to access elevators to the flagship Lotte Duty Free outlet in the group's headquarters complex, as TV cameras waited for investigators to emerge from office doors around the corner."})

Db.news.insert ({"title": "Named", "context": "Named after the heroine of an 18th century Goethe novel, Lotte has grown from its founding in Japan 68 years ago as a maker of chewing gum to a corporate giant with interests ranging from hotels and retail to food and chemicals. The group has annual revenue of around $60 billion in Korea."})

Db.news.insert ({"title": "Hotel Lotte's", "context": "Hotel Lotte's planned flotation of around 35 percent of its shares was intended to bring transparency and improve corporate governance at a group whose ownership structure is convoluted even by the opaque standards of South Korea's conglomerates."})

Db.news.insert ({"title": "Shares", "context": "Shares in Lotte Shopping (023530.KS), whose units Lotte Department Store and Lotte Home Shopping were raided, fell 1.6 percent on Friday. Lotte Himart (071840.KS), a consumer electronics retailer, dropped 2.1 percent."})

> db.news.ensureIndex ({"title": "text", "context": "text"}, {"weights": {"title": 2, "context": 1}})

{

"createdCollectionAutomatically": false

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

}

> db.news.find ({$text: {$search: "flotation"}})

{"_ id": ObjectId ("57aa8a28fe5f255ba4328c0e"), "title": "Hotel Lotte's", "context": "Hotel Lotte's planned flotation of around 35 percent of its shares was intended to bring transparency and improve corporate governance at a group whose ownership structure is convoluted even by the opaque standards of South Korea's conglomerates."}

>

There can be at most one full-text index on each collection, but a full-text index can contain multiple fields. A full-text index is different from a "normal" multi-key index in that the order of fields in a full-text index is not important: each field is treated equally, and each field can be assigned a different weight to control the relative importance of different fields.

Create a full-text index for the title and context fields, giving weight to title field 2 and context field 1. (the range of weight can be 1, 1, 1, 000.)

2d index

The 2d index is also one of the most commonly used geospatial indexes in MongoDB, which is used for game maps. The 2d index is used for flat surfaces, not for spherical surfaces. If used on the surface of a sphere, there will be a lot of distortion near the pole.

> var map = [{

... "gis": {

... "x": 185

... "y": 150

...}

...}, {

... "gis": {

... "x": 70

... "y": 180

...}

...}, {

... "gis": {

... "x": 75

... "y": 180

...}

...}, {

... "gis": {

... "x": 185

... "y": 185

...}

...}, {

... "gis": {

... "x": 65

... "y": 185

...}

...}, {

... "gis": {

... "x": 50

... "y": 50

...}

...}, {

... "gis": {

... "x": 50

... "y": 50

...}

...}, {

... "gis": {

... "x": 60

... "y": 55

...}

...}, {

... "gis": {

... "x": 65

... "y": 80

...}

...}, {

... "gis": {

... "x": 55

... "y": 80

...}

...}, {

... "gis": {

... "x": 0

... "y": 0

...}

...}, {

... "gis": {

... "x": 0

... "y": 200

...}

...}, {

... "gis": {

... "x": 200

... "y": 0

...}

...}, {

... "gis": {

... "x": 200

... "y": 200

...}

...}]

>

> for (var iTuno db.map.ensureIndex ({gis: "2d"}, {min:-1,max:201})

{

"createdCollectionAutomatically": false

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

}

Query (70180) the nearest 3 points

> db.map.find ({"gis": {$near: [70180]}}) .limit (3)

{"_ id": ObjectId ("57ab8ac5893a4e222430fcc1"), "gis": {"x": 70, "y": 180}}

{"_ id": ObjectId ("57ab8ac5893a4e222430fcc2"), "gis": {"x": 75, "y": 180}}

{"_ id": ObjectId ("57ab8ac5893a4e222430fcc4"), "gis": {"x": 65, "y": 185}}

>

Query all points in a square with points (505050) and points (190190) as diagonals:

> db.map.find ({gis: {"$within": {$box: [[50 id:0,gis:1 50], [190190]]}, {_ id:0,gis:1})

{"gis": {"x": 50, "y": 50}}

{"gis": {"x": 50, "y": 50}}

{"gis": {"x": 60, "y": 55}}

{"gis": {"x": 55, "y": 80}}

{"gis": {"x": 65, "y": 80}}

{"gis": {"x": 65, "y": 185}}

{"gis": {"x": 70, "y": 180}}

{"gis": {"x": 75, "y": 180}}

{"gis": {"x": 185, "y": 150}}

{"gis": {"x": 185, "y": 185}}

Query the points in the area of the circle with the center of the circle (56.80) and a radius of 50, as shown below:

> db.map.find ({gis: {"$within": {$center: [[56 id:0,gis:1 80], 50]}, {_ id:0,gis:1})

{"gis": {"x": 50, "y": 50}}

{"gis": {"x": 50, "y": 50}}

{"gis": {"x": 60, "y": 55}}

{"gis": {"x": 55, "y": 80}}

{"gis": {"x": 65, "y": 80}}

View all the points within the triangle, as follows:

> db.map.find ({"gis": {"$within": {"$polygon": [[50 Magi 70], [70 Magi 90], [90 J 70]]})

{"_ id": ObjectId ("57ab8ac5893a4e222430fcc8"), "gis": {"x": 65, "y": 80}}

> polygons, you can specify $polygon ($ploygon accepts an array of multiple elements, each element corresponding to the points of the polygon)

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: 283

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

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report