感覺就像撞墻一樣,所以非常感謝幫助!
我在資料庫中有兩個欄位,都可以包含各種文本。從我到目前為止所讀到的內容來看,我需要先創建一個索引,但是由于我有兩個欄位而不是一個欄位。我如何在其中進行搜索?此外,它必須不區分大小寫。使用 MongoDB 4.4 Pymongo 有它自己的“文本”變數,以及自己的 create_index 呼叫,所以這是正確的嗎?
collection.create_index([('author' , pymongo.TEXT), ('title' , pymongo.TEXT)])
我如何從這里開始并對一個 search_string 進行不區分大小寫的搜索?
uj5u.com熱心網友回復:
此解決方案在特殊索引上同時對author和title欄位進行不區分大小寫、對停用詞敏感的查找$text:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362979.html
下一篇:查找物件陣列
