[SERVER-9148] In text searches with $near filter, results don't obey filter Created: 27/Mar/13  Updated: 27/Oct/15  Resolved: 25/Jun/13

Status: Closed
Project: Core Server
Component/s: Geo, Querying, Text Search
Affects Version/s: 2.4.1
Fix Version/s: 2.5.0

Type: Bug Priority: Major - P3
Reporter: J Rassi Assignee: hari.khalsa@10gen.com
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-3071 Index Intersection Closed
is related to DOCS-1719 $text search cannot be used with $nea... Closed
Operating System: ALL
Participants:

 Description   

If the text command is passed a filter with a $near query operator, the result set will include items that don't match the filter. $near needs to be resolved using a geospatial index, and I assume that the matcher currently does not use the index here.

The expected behavior would be for the search to resolve correctly using both indexes, or to just fail explicitly as unsupported.

To reproduce:

db.generated.ensureIndex({"location.coordinates":"2d"})
db.generated.ensureIndex({t:"text"})
db.generated.insert({t:"bike",location:{coordinates:[40,7.25]}})
 
// evaluates to 1:
db.generated.find({"location.coordinates": { $near: [40,7.25], $maxDistance: 0.01 }}).count()
 
// evaluates to 0:
db.generated.find({"location.coordinates": { $near: [-40,7.25], $maxDistance: 0.01 }}).count()
 
// evaluates to 1:
db.generated.runCommand("text", { search: "bike", filter: { "location.coordinates": { $near: [40, 7.25], $maxDistance: 0.01 }}}).stats.n
 
// evaluates to 1 (unexpected):
db.generated.runCommand("text", { search: "bike", filter: { "location.coordinates": { $near: [-40, 7.25], $maxDistance: 0.01 }}}).stats.n



 Comments   
Comment by brad davis [ 23/Sep/14 ]

Slightly confused. This ticket (and https://jira.mongodb.org/browse/DOCS-1719) seem to indicated this bug was fixed but I'm finding that 2.6.3 still reproduces issue and public documentation states this is known limitation. Is there not solution to use $near and $text in same query? Thanks.

Comment by J Rassi [ 25/Jun/13 ]

Doc changes needed: $near cannot be used as part of a text search "filter" predicate.

Comment by Daniel Pasette (Inactive) [ 25/Jun/13 ]

this query depends on index intersection, SERVER-3071, to actually be valid.

Comment by Daniel Pasette (Inactive) [ 25/Jun/13 ]

In 2.5.0, the new matcher rejects this query, which would require two indexes, with the following error message:

> db.generated.runCommand("text", { search: "bike", filter: { "location.coordinates": { $near: [40, 7.25], $maxDistance: 0.01 }}})
{
	"queryDebugString" : "bike||||||",
	"language" : "english",
	"errmsg" : "exception: bad query: BadValue not handled: $near",
	"code" : 16810,
	"ok" : 0
}

Generated at Thu Feb 08 03:19:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.