MongoDB PyMongo - 创建索引并在两个字段中进行不区分大小写的搜索
MongoDB PyMongo - create an index and make a case insensitive search in two fields
感觉就像碰壁一样,非常感谢帮助!
我在数据库中有两个字段,都可以包含各种文本。
到目前为止,根据我所读到的内容,我需要先创建一个索引,但是因为我有两个字段而不是一个。我如何在其中进行搜索?另外,它必须不区分大小写。
使用 MongoDB 4.4
Pymongo 有自己的“文本”变量,以及自己的 create_index 调用,所以这是正确的吗?
collection.create_index([('author' , pymongo.TEXT), ('title' , pymongo.TEXT)])
我如何从这里开始进行不区分大小写的搜索 search_string?
此解决方案在特殊 $text
索引上同时对 author
和 title
字段进行不区分大小写、停用词敏感的查找:
rc = db.foo.create_index([('author' , pymongo.TEXT), ('title' , pymongo.TEXT)])
r = [
{"author":"Buzz","title":"Dangferous iewhf"}
,{"author":"Dave","title":"Corn"}
,{"author":"Dave","title":"The buzz about wheat"}
,{"author":"Chris","title":"Not in this film"}
,{"author":"Herbert","title":"Dune"}
,{"author":"Herbert","title":"Children of Dune"}
]
db.foo.insert_many(r)
searchstring = "Dune"
for doc in db.foo.find({"$text": { "$search": searchstring } } ):
print(doc)
有关精确短语、多个单词、单词排除等的更多详细信息,请参阅此处的文档:
MongoDB $text index features
感觉就像碰壁一样,非常感谢帮助!
我在数据库中有两个字段,都可以包含各种文本。 到目前为止,根据我所读到的内容,我需要先创建一个索引,但是因为我有两个字段而不是一个。我如何在其中进行搜索?另外,它必须不区分大小写。 使用 MongoDB 4.4 Pymongo 有自己的“文本”变量,以及自己的 create_index 调用,所以这是正确的吗?
collection.create_index([('author' , pymongo.TEXT), ('title' , pymongo.TEXT)])
我如何从这里开始进行不区分大小写的搜索 search_string?
此解决方案在特殊 $text
索引上同时对 author
和 title
字段进行不区分大小写、停用词敏感的查找:
rc = db.foo.create_index([('author' , pymongo.TEXT), ('title' , pymongo.TEXT)])
r = [
{"author":"Buzz","title":"Dangferous iewhf"}
,{"author":"Dave","title":"Corn"}
,{"author":"Dave","title":"The buzz about wheat"}
,{"author":"Chris","title":"Not in this film"}
,{"author":"Herbert","title":"Dune"}
,{"author":"Herbert","title":"Children of Dune"}
]
db.foo.insert_many(r)
searchstring = "Dune"
for doc in db.foo.find({"$text": { "$search": searchstring } } ):
print(doc)
有关精确短语、多个单词、单词排除等的更多详细信息,请参阅此处的文档: MongoDB $text index features