텍스트 인덱스 (Text Index)
Text Index
Create a text index on one field
db.collection.createIndex( { 'field': 'text'} )
Create text indexes on multiple fields
db.collection.createIndex( { 'field_1': 'text', 'field_2': 'text' } )
Specify the weight of an indexed
db.collection.createIndex( { 'field_1': 'text', 'field_2': 'text'}, {weights :{ 'field_1' : 3, 'field_2' : 2 } })
Text Search Operator
Search
db.collection.find( { '$text': {'$search' : 'VALUE' } } } ) db.collection.find( { '$text': {'$search' : ' \"VALUE\" ' } } } ) # Phrase db.collection.find( { '$text': {'$search' : ' \"VALUE_1\" \"VALUE_2\" ' } } } ) # AND db.collection.find( { '$text': {'$search' : ' VALUE_1 -VALUE_2 ' } } } ) # Exclude
Search with options
db.collection.find( { '$text': {'$search' : 'VALUE', '$caseSensitive' : True } } } ) # Case sensitive db.collection.find( { '$text': {'$search' : 'VALUE', '$language' : 'en' } } } ) # Specify a language
Include a score field
db.collection.find( { '$text': {'$search' : 'VALUE' } }, { 'score' : { '$meta' : 'textScore' } } ) db.collection.find( { '$text': {'$search' : 'VALUE' } }, { 'score' : { '$meta' : 'textScore' } } ) \ .sort( {'score': {'$meta': 'textScore’}} ) # With sorting
Last updated
Was this helpful?