我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用pymongo.TEXT。
def _create_text_index(self, fields=None): """Create a text index for the database collection. Takes a list of fields and creates a text index based on the Engine's schema, if it has one. Otherwise, creates a text index for any field that contains string content. """ collection = self._collection index_name = 'TextIndex' if fields: index = [] for field in self.schema: if field.field_type in self.TEXT_INDEXES: index.append((field.field_name, pymongo.TEXT)) return collection.create_index(index, background=True, name=index_name) else: return collection.create_index([('$**', pymongo.TEXT)], name=index_name)
def initialize_indexes(database): """Ensure the necessary indexes exist.""" submissions = database['submissions'] comments = database['comments'] index_id = pymongo.IndexModel('reddit_id') index_created = pymongo.IndexModel([('created', pymongo.DESCENDING)]) index_text_title_and_body = pymongo.IndexModel([('title', pymongo.TEXT), ('body', pymongo.TEXT)]) index_text_body = pymongo.IndexModel([('body', pymongo.TEXT)]) submissions.create_indexes([index_id, index_created, index_text_title_and_body]) comments.create_indexes([index_id, index_created, index_text_body])
def get(self): args = self.parser.parse_args() query = args["query"] maxResults = args["maxResults"] if query == None: return jsonify({"results": {}}) searchResults = {} isDone = False for collectionAttrs in SEARCHABLE_COLLECTION_ATTRIBUTES: if isDone: break collectionResults = [] collection = db[collectionAttrs["collectionName"]] collection.create_index([("$**", TEXT)]) rawCollectionResults = collection.find({"$text": {"$search": query}}) for res in rawCollectionResults: if len(searchResults) >= maxResults: isDone = True break collectionResults.append({"title": res[collectionAttrs['nameField']], "url": collectionAttrs['linkLead']+str(res["_id"])}) if len(collectionResults) > 0: searchResults[collectionAttrs["collectionName"]] = {"name": collectionAttrs["categoryName"], "results": collectionResults} return jsonify({"results": searchResults})
def prepare_index(): await db.tracks.create_index([ ("title", pymongo.TEXT), ("performer", pymongo.TEXT) ]) await db.tracks.create_index([ ("file_id", pymongo.ASCENDING) ]) await db.users.create_index("id")
def execute_query(self, query, session): ''' Get the results of ``query``. This method does flush in a transaction, so any objects retrieved which are not in the cache which would be updated when the transaction finishes will be stale ''' self.auto_ensure_indexes(query.type) kwargs = dict() if query._get_fields(): if PYMONGO_3: # pragma: nocover kwargs['projection'] = query._fields_expression() else: # pragma: nocover kwargs['fields'] = query._fields_expression() collection = self.db[query.type.get_collection_name()] if query._search: index_fields = query._createIndex if index_fields: # create new index if type(index_fields) is list: index_list = [] for field in index_fields: index_list.append ((field, pymongo.TEXT)) collection.create_index(index_list, name='search_index', default_language='english') else: raise InvalidConfigException() cursor = collection.find(query.query, {'__index_score': {'$meta': "textScore"}}, **kwargs) cursor.sort([('__index_score', {'$meta': 'textScore'})]) elif query._rawquery: if query._query_type=='aggregate': cursor = collection.aggregate(query.query, **kwargs) elif query._query_type=='map_reduce': cursor = collection.map_reduce( query._mapreduce_mapper, query._mapreduce_reducer, query._mapreduce_key, query=query._mapreduce_query) else: cursor = collection.find(query.query, **kwargs) if query._sort: cursor.sort(query._sort) elif query.type.config_default_sort: cursor.sort(query.type.config_default_sort) if query.hints: cursor.hint(query.hints) if query._get_limit() is not None: cursor.limit(query._get_limit()) if query._get_skip() is not None: cursor.skip(query._get_skip()) return QueryResult(session, cursor, query.type, raw_output=query._raw_output, fields=query._get_fields())
def connect(self): self.init() # Create indexes self.files.create_index('md5') self.files.create_index('sha1') self.files.create_index('sha256') self.files.create_index([("$**", TEXT)], background=True) self.analysis.create_index('date') self.analysis.create_index([("$**", TEXT)], background=True)
def create_assets_secondary_index(conn, dbname): logger.info('Create `assets` secondary index.') # unique index on the id of the asset. # the id is the txid of the transaction that created the asset conn.conn[dbname]['assets'].create_index('id', name='asset_id', unique=True) # full text search index conn.conn[dbname]['assets'].create_index([('$**', TEXT)], name='text')
def create_metadata_secondary_index(conn, dbname): logger.info('Create `metadata` secondary index.') # unique index on the id of the metadata. # the id is the txid of the transaction for which the metadata # was specified conn.conn[dbname]['metadata'].create_index('id', name='transaction_id', unique=True) # full text search index conn.conn[dbname]['metadata'].create_index([('$**', TEXT)], name='text')
def explicit_key(index): if isinstance(index, (list, tuple)): assert len(index) == 2, 'Must be a (`key`, `direction`) tuple' return index elif index.startswith('+'): return (index[1:], ASCENDING) elif index.startswith('-'): return (index[1:], DESCENDING) elif index.startswith('$'): return (index[1:], TEXT) elif index.startswith('#'): return (index[1:], HASHED) else: return (index, ASCENDING)
def startup(self): index_date = IndexModel([('date', DESCENDING)]) index_text = IndexModel([('content', TEXT)]) await self._db.create_indexes([index_date, index_text])