geospatial - How can I query a MongoDB collection by both a geo spatial index and a text index quickly? -


given collection locations consisting of ~20,000,000 documents 3 properties:

{     _id,     name, // string     geo // coordinate pair, e.g. [-90.123456, 30.123456] } 

and index of name: 1 , geo index setup so:

{      "geo" : "2dsphere" }, {      "v" : 1,      "name" : "geo_2dsphere",      "ns" : "db.locations",      "min" : "-180.0",      "max" : "180.0",      "w" : 1.0,      "2dsphereindexversion" : 2 } 

how can performantly query against collection both on geo_2dsphere index , on name index?

when run $box query on geo index only, takes on 20 seconds return 50 results. when include search against name property goes further.

if run $near query, things can perform quickly, queries seem (very randomly) go ~200ms many seconds. see example difference 1 additional character on name index increases time:

200ms:

{name: /^mac/, geo: {$near: {$geometry: {type: "point", coordinates: [ -90.123456, 30.123456 ]}, $maxdistance: 20000}}} 

18,000ms:

 {name: /^macy/, geo: {$near: {$geometry: {type: "point", coordinates: [ -90.123456, 30.123456 ]}, $maxdistance: 20000}}} 

i can't understand why being more specific index slowing things down much. when more specific phrase, have drastically reduce $maxdistance 7,000 meters before query returns in reasonable amount of time.

is there better setup should doing here?

as has been pointed out me blakes seven, cannot search across multiple indexes in mongodb:

there "highlander rule" (there can one) in query evaluation denies usage of more "one" "special" index in query evaluation. cannot have multiple "text" or muliple "geospatial" or combination of "text" , "geospatial" or usage of of within $or condition, results in multiple index selection.

so, i've opted move on elasticsearch specific query, indexing need complete these multi-index queries, , use results load necessary mongo documents. works quickly, works well.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -