问题
I have a document with one field description
like this:
{
"_id": "item0",
"description": {
"parlist": [
{
"listitem": {
"text": {
"child": "page rous lady",
"keyword": "officer e"
}
}
},
{
"listitem": {
"text": "shepherd noble "
}
}
]
}
}
How to create text index on description
and search for specific word? I don't know how depth can description
go and how many children will description have. I tried with index creation like this:
db.collection.ensureIndex({description:"text"})
and then for query like this:
db.collection.runCommand("text",{$search:"shepherd"})
But it doesn't work.
回答1:
You can just simply build an text index for all text field:
db.collection.ensureIndex({ "$**": "text" })
Then search with keyword $text:
db.collection.find( { $text: { $search: "shepherd" } } )
回答2:
Text-indexes do not work on full sub-documents, but you can create a text index which includes more than one field:
db.collection.ensureIndex(
{
"description.parlist.listitem.text": "text",
"description.parlist.listitem.text.child": "text",
"description.parlist.listitem.text.keyword": "text"
}
)
来源:https://stackoverflow.com/questions/23240961/how-to-use-full-text-search-for-unknown-number-of-children-of-a-field-in-mongodb