MongoDB的索引(四)
九、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的查询