[SERVER-19857] $text behaves differently on non-existent vs empty collection Created: 10/Aug/15  Updated: 06/Dec/22  Resolved: 21/Jul/17

Status: Closed
Project: Core Server
Component/s: Querying, Text Search
Affects Version/s: 2.6.10, 3.0.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kevin Pulo Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

Both an empty collection and a non-existent collection have no text index. Therefore, I expect their behaviour with respect to $text to be the same. However, the empty collection case returns an error, whereas on a non-existent collection the query apparently "succeeds" (even though it's probably short-circuiting somewhere).

> db.a.drop()
false
> db.createCollection("a")
{ "ok" : 1 }
> db.a.find( { "$text": { "$search": "foobar" } } )
Error: error: {
        "$err" : "Unable to execute query: error processing query: ns=test.a limit=0 skip=0\nTree: TEXT : query=foobar, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
        "code" : 17007
}
> db.a.drop()
true
> db.a.find( { "$text": { "$search": "foobar" } } )
> 

In 3.0.5 the explain plan is:

> db.a.find( { "$text": { "$search": "foobar" } } ).explain(true)
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "test.a",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$text" : {
                                "$search" : "foobar",
                                "$language" : ""
                        }
                },
                "winningPlan" : {
                        "stage" : "EOF"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 0,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 0,
                "executionStages" : {
                        "stage" : "EOF",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 1,
                        "advanced" : 0,
                        "needTime" : 0,
                        "needFetch" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0
                },
                "allPlansExecution" : [ ]
        },
        "serverInfo" : {
                "host" : "genique",
                "port" : 23249,
                "version" : "3.0.5",
                "gitVersion" : "8bc4ae20708dbb493cb09338d9e7be6698e4a3a3"
        },
        "ok" : 1
}

In 2.6.10 it is:

> db.a.find( { "$text": { "$search": "foobar" } } ).explain(true)
{
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 0,
        "nscanned" : 0,
        "nscannedObjectsAllPlans" : 0,
        "nscannedAllPlans" : 0,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "allPlans" : [
                {
                        "cursor" : "BasicCursor",
                        "n" : 0,
                        "nscannedObjects" : 0,
                        "nscanned" : 0
                }
        ],
        "server" : "genique:27017"



 Comments   
Comment by Asya Kamsky [ 21/Jul/17 ]

This is no longer an issue:

> db.a.drop()
true
> db.a.find( { "$text": { "$search": "foobar" } } )
Error: error: {
	"ok" : 0,
	"errmsg" : "text index required for $text query (no such collection 'test.a')",
	"code" : 27,
	"codeName" : "IndexNotFound"
}

Both 3.5.9 and 3.4.4 correctly give this error, so going to close this as gone away.

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