MongoDB的索引(四)

太阳2年前技术文章951

九、Text Indexes

示例集合

> db.ttlsa_com.find()
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9904"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9905"), "song" : "2. Hotel California", "lyrics" : "Up ahead in the distance, I saw a shimmering light. My head grew heavy and my sight grew dim." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9906"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9907"), "song" : "4. Hotel California", "lyrics" : "Some dance to remember, some dance to forget." }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9908"), "song" : "5. Hotel California", "lyrics" : "Welcome to the Hotel California" }
{ "_id" : ObjectId("5d2f35f6c1aace30b3ce9909"), "song" : "hell world", "lyrics" : "Welcome to beijing" }

1、语法

一个集合只能有一个text索引,但是该text可以是多个字段的复合索引

db.quotes.createIndex({ content : "text" })
db.reviews.createIndex({subject: "text",comments: "text"})

2、权重

1)创建全文索引默认权重为1

2)各字段的权重分布会影响到查询时的优先策略

db.blob.insertMany([{_id: 1,content: "This morning I had a cup of coffee.",about: "beverage",keywords: ["coffee"]},
{_id: 2,content: "Who doesn't like cake?",about: "food",keywords: [ "cake", "food", "dessert" ]}])

db.blog.createIndex(
   {
     content: "text",
     keywords: "text",
     about: "text"
   },
   {
     weights: {                     //执行权重
       content: 10,
       keywords: 5
     },
     name: "TextIndex"              //指定索引名字
   }
 )

3、通配符文本索引 - 表示在所有字段创建一个全文索引

db.ttlsa_com.ensureIndex({"$**": "text"})

4、复合全文索引

> db.inventory.find()
{ "_id" : 1, "dept" : "tech", "description" : "lime green computer" }
{ "_id" : 2, "dept" : "tech", "description" : "wireless red mouse" }
{ "_id" : 3, "dept" : "kitchen", "description" : "green placemat" }
{ "_id" : 4, "dept" : "kitchen", "description" : "red peeler" }
{ "_id" : 5, "dept" : "food", "description" : "green apple" }
{ "_id" : 6, "dept" : "food", "description" : "red potato" }

> db.inventory.createIndex(
...    {
...      dept: 1,
...      description: "text"
...    }
... )

> db.inventory.find( { dept: "kitchen", $text: { $search: "green" } } )
{ "_id" : 3, "dept" : "kitchen", "description" : "green placemat" }

5、查询语法

db..find({
 $text:
   {
     $search: <string>,
     $language: <string>,
     $caseSensitive: <boolean>,
     $diacriticSensitive: <boolean>
   }
})


十、2dsphere Indexes

1、2dsphere索引支持对于球体的地理位置计算

2、2dsphere索引默认为稀疏索引,如果某一文档缺少2dsphere字段(null或为空),那么该文档不会创建索引。对于一个包含其它类型的复合2dsphere索引,该文档索引的使用仅仅与2dsphere字段有关。

> db.places.find()
{ "_id" : ObjectId("5d2fd9a67737353186206a70"), "loc" : { "type" : "Point", "coordinates" : [ -73.97, 40.77 ] }, "name" : "Central Park", "category" : "Parks" }
{ "_id" : ObjectId("5d2fd9a67737353186206a71"), "loc" : { "type" : "Point", "coordinates" : [ -73.88, 40.78 ] }, "name" : "La Guardia Airport", "category" : "Airport" }

> db.places.createIndex( { category : 1 , loc : "2dsphere" } )

> db.places.find({category:"Airport"}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "test.places",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"category" : {
				"$eq" : "Airport"
			}
		},
		"winningPlan" : {
			"stage" : "COLLSCAN",                       //全文档扫描
			"filter" : {
				"category" : {
					"$eq" : "Airport"
				}
			},
			"direction" : "forward"
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "dbslave2",
		"port" : 28002,
		"version" : "4.0.10-5",
		"gitVersion" : "7dab0a3a7b7b40cf71724b5a11eff871f8c3885c"
	},
	"ok" : 1
}

3、version 2 之后,2dsphere索引支持GeoJSON格式对象写入

4、2dsphere索引没有办法作为分片键

5、2dsphere索引字段必须是坐标或者 GeoJSON 类型,否则会报错

6、创建2dsphere索引语法:

1)2dsphere索引

db.places.createIndex( { loc : "2dsphere" } )

2)复合2dsphere索引

与2d索引不同,2dsphere索引不需要将location字段放在最左前缀。

db.places.createIndex( { category : 1 , loc : "2dsphere" } )
db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )

7、查询语法

Polygon相关查询

1)查询指定地址位置内所有的点

语法:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $geometry :
                             { type : "Polygon" ,                   
                               coordinates : [ <coordinates> ]
                      } } } } )

示例:查询由coordinates指定的多边形内所有的点和形状

db.places.find( { loc :
                  { $geoWithin :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates : [ [
                                          [ 0 , 0 ] ,
                                          [ 3 , 6 ] ,
                                          [ 6 , 1 ] ,
                                          [ 0 , 0 ]
                                        ] ]
                } } } } )

2)交集

1.语法

db.<collection>.find( { <location field> :
                         { $geoIntersects :
                           { $geometry :
                             { type : "<GeoJSON object type>" ,
                               coordinates : [ <coordinates> ]
                      } } } } )

2.示例:查找与coordinates点组成多边形所有相交的点和形状

db.places.find( { loc :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates: [ [
                                         [ 0 , 0 ] ,
                                         [ 3 , 6 ] ,
                                         [ 6 , 1 ] ,
                                         [ 0 , 0 ]
                                       ] ]
                } } } } )

Point的点相关查询

3)临近GeoJSON Point的点

语法:

db.<collection>.find( { <location field> :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ <longitude> , <latitude> ] } ,
                             $maxDistance : <distance in meters>
                      } } } )

示例:

db.places.find( { loc :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ -88 , 30 ] } ,
                             $maxDistance : 3963
                      } } } )

4)指定point以及半径内所有的点

语法:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $centerSphere :
                              [ [ <x>, <y> ] , <radius> ] }
                      } } )

示例:

db.places.find( { loc :
                  { $geoWithin :
                    { $centerSphere :
                       [ [ -88 , 30 ] , 10 / 3963.2 ]
                } } } )

8、2dsphere 支持 Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon、Geometry Collection的查询


返回列表

上一篇:RBAC

下一篇:MongoDB的索引(五)

相关文章

Kubernetes源码解读(四)--Lister&Watcher源码分析

Kubernetes源码解读(四)--Lister&Watcher源码分析

Lister&&Watcher是Reflector的一个主要能力提供者,我们来看看Lister&&Watcher是如何实现List()和watch()的过程的。List...

聊一聊什么是分布式系统

聊一聊什么是分布式系统

分布式系统原理1 概念1.1 模型节点在具体的工程项目中,一个节点往往是一个操作系统上的进程。在本文的模型中,认为节点是一个完整的、不可分的整体,如果某个程序进程实际上由若干相对独立部分构成,则在模型...

ACOS无数据告警实践

ACOS无数据告警实践

1.说明在实现数据监控的过程中告警能力无疑是重中之重,无数据告警亦是告警能力中重要的场景,这里我们聊聊关于无数据一些场景和实践方法。2.无数据可能场景对于运维监控平台来说无数据是一个比较复杂的情况,从...

MySQL运维实战(5.1) 字符和编码的基本概念

MySQL运维实战(5.1) 字符和编码的基本概念

字符和编码字符字符是符号,是人们用于交流的各类符号,如26个英文字母、汉字、标点符号、数学运算符、其他语言的字母和符号。编码编码是计算机中以二进制方式存储字符的方式。字符集字符集是字符和编码的映射表。...

MongoDB复制原理

一、Initial Sync大体来说,MongoDB副本集同步主要包含两个步骤: 1. Initial Sync,全量同步 2. Replication,即sync oplog 先通过init syn...

Hbase 存储相关知识

1.Hbase的写流程Client 写入-> 存入MemStore,一直到MemStore 满-> Flush 成一个StoreFile,直至增长到一定阈值-> 触发Compact...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。