mongodb的文本索引-pymongo

445 阅读1分钟

批量创建索引:



import pymongo
import pandas as pd
from pymongo import IndexModel, ASCENDING, DESCENDING, HASHED,TEXT
client = pymongo.MongoClient(host="192.168.9.108", port=27017)

# 8 个国家站点
db_names=['CA_AMZ','DE_AMZ','ES_AMZ','FR_AMZ','IT_AMZ','JP_AMZ','UK_AMZ',]  # 'US_AMZ'
table_names=['slope_2022_market']



default_language_amz={
'CA_AMZ': 'english',
'UK_AMZ': 'english',
'DE_AMZ': 'german',
'FR_AMZ': 'french',
'IT_AMZ': 'italian',
'ES_AMZ': 'spanish',
'JP_AMZ': 'hans',

}

'''
https://mongoing.com/docs/reference/text-search-languages.html 
db.quotes.createIndex(
   { content : "text" },
   { default_language: "spanish" }
)
'''

def createIndexPython(db_name,table_name):

    db = client[db_name]
    collection = db[table_name]
    #collection.drop_indexes()
    #collection.drop_index('searchTerm_text')
    index1 = IndexModel([("searchTerm", TEXT),('default_language','spanish')], background=True)  # 索引1
    collection.create_indexes([index1])

    index_name=collection.index_information()
    return  index_name


for db in db_names:
    for table in  table_names:
        print(db, table, )
        res = createIndexPython(db, table)
        print(res)

一次性创建多个索引:

image.png

查看索引

> db.slope_2022_market.getIndexes();
[        {                "v" : 2,                "key" : {                        "_id" : 1                },                "name" : "_id_"        },        {                "v" : 2,                "key" : {                        "_fts" : "text",                        "_ftsx" : 1                },                "name" : "searchTerm_text",                "background" : true,                "weights" : {                        "searchTerm" : 1                },                "default_language" : "english",                "language_override" : "language",                "textIndexVersion" : 3        }]
>

由此可见:创建的索引默认语言是 English 索引的名称为 'searchTerm_text'

更改索引的 文本索引的语言

文本索引语言链接:mongoing.com/docs/refere… 文本索引文档:docs.mongoing.com/indexes/tex… 文本索引英文文档:www.mongodb.com/docs/manual…

正常运行:



import pymongo
import pandas as pd
from pymongo import IndexModel, ASCENDING, DESCENDING, HASHED,TEXT
client = pymongo.MongoClient(host="192.168.9.108", port=27017)

# 8 个国家站点
db_names=['DE_AMZ','CA_AMZ','ES_AMZ','FR_AMZ','IT_AMZ','UK_AMZ','JP_AMZ']  # 'US_AMZ',
table_names=['slope_2022_market']



default_language_amz={
'CA_AMZ': 'english',
'UK_AMZ': 'english',
'DE_AMZ': 'german',
'FR_AMZ': 'french',
'IT_AMZ': 'italian',
'ES_AMZ': 'spanish',
'JP_AMZ': 'hant',

}

'''
https://mongoing.com/docs/reference/text-search-languages.html 
db.quotes.createIndex(
   { content : "text" },
   { default_language: "spanish" }
)
'''

def createIndexPython(db_name,table_name):

    db = client[db_name]
    collection = db[table_name]
    #collection.drop_indexes()
    # collection.drop_index('searchTerm_text')
    index1 = IndexModel([("searchTerm", TEXT)],default_language=default_language_amz[db_name],
                        background=True)  # 索引1
    collection.create_indexes([index1])

    index_name = collection.index_information()
    return  index_name



for db in db_names[:6]:
    for table in  table_names:
        print(db, table, )
        res = createIndexPython(db, table)
        print(res)

image.png

日语 搞不定:其他语言均正常、

image.png 意大利的正常;

在命令行中运行:

> db.slope_2022_market.find({'searchTerm':'lego'});
{ "_id" : ObjectId("62417914c2c314a4dcbb4380"), "this_week" : "2022-03-19", "this_week_rank" : 9, "last_week_rank" : 8, "slop
e_seq" : 4, "start_date" : "2019-05-18", "lack_count" : 0, "week_three_slope" : 1.5, "one_month_slope" : -0.1, "two_month_slo
pe" : -0.71, "three_month_slope" : -0.77, "six_month_slope" : -0.07, "nine_month_slope" : 0.05, "twelve_month_slope" : -0.03,
 "kinds" : "others", "searchTerm" : "lego", "wordsLength" : 1, "this_click_focus" : 13, "this_con_focus" : 9 }
> db.slope_2022_market.ensureIndex({'searchTerm':"text"},{'default_language': "german"});
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
>

将文本索引设置为 德语,成功运行

image.png

命令行 索引操作:

  • 了解索引的影响
  • 如何创建索引:createIndex()
  • 如何查找索引:getindexes()
  • 如何删除索引:dropindex()