- Indexes >
- Indexing Tutorials >
- Text Search Tutorials >
- Control Search Results with Weights
Control Search Results with Weights¶
This document describes how to create a text
index with specified
weights for results fields.
By default, the text
command returns matching documents
based on scores, from highest to lowest. For a text
index, the
weight of an indexed field denotes the significance of the field
relative to the other indexed fields in terms of the score. The score
for a given word in a document is derived from the weighted sum of the
frequency for each of the indexed fields in that document.
The default weight is 1 for the indexed fields. To adjust the weights
for the indexed fields, include the weights
option in the
db.collection.ensureIndex()
method.
Warning
Choose the weights carefully in order to prevent the need to reindex.
A collection blog
has the following documents:
To create a text
index with different field weights for the
content
field and the keywords
field, include the weights
option to the ensureIndex()
method. For
example, the following command creates an index on three fields and
assigns weights to two of the fields:
The text
index has the following fields and weights:
content
has a weight of 10,keywords
has a weight of 5, andabout
has the default weight of 1.
These weights denote the relative significance of the indexed fields to
each other. For instance, a term match in the content
field has:
2
times (i.e.10:5
) the impact as a term match in thekeywords
field and10
times (i.e.10:1
) the impact as a term match in theabout
field.