Database Reference
In-Depth Information
They include four languages (in the lingvo fields), and if we keep any one default then we need to specify
which language we will be searching within. Because we have specified the language the given content is in, we can
use this field as a language override and the given language will be used rather than the default. We can create an
index with this as follows:
db.texttest.ensureIndex( { content : "text" }, { language_override : "lingvo" } );
Thus the default language for those documents will be the one provided, and any documents lacking a lingvo
field will use the default index, in this case English.
Compound Indexing with Text Indexes
Although it is true that you can only have one text index on a collection, you can have that text index cover a number
of fields in a document or even all fields. You can specify extra fields just as you would for a normal index. Let's say we
want to index both the content and any comments; we can do that as follows. Now we can make text searches on
both fields.
db.texttest.ensureIndex( { content : "text", comments : "text" });
You may even want to create a text index on all the fields in a document. MongoDB has a wildcard specifier
that can be used to reference all text elements of all documents; the notation is "$**" . If you wish to specify this
as the form for your text index, you will need to add the index option for a name to your document. This way, the
automatically generated name will not be used and cause problems with the index by being too long a field. The
maximum length of an index is 121 characters, which includes the names of the collection, the database, and the
fields to be indexed.
it is strongly recommended that you specify a name with any compound index that has a text field to avoid
running into issues caused by the name length.
Note
This gives us the following syntax for creating a text index named alltextindex on all string elements of all
documents in the texttest collection:
db.texttest.ensureIndex( { "$**": "text" }, { name: "alltextindex" } )
The next thing you can do with a compound text index is specifying weights for different text fields on that index.
You do this by adding weight values above the default of one to each field that you will index. The values will then
increase the importance of the given index's results in a ratio of N:1. Take the following example index:
db.texttest.ensureIndex( { content : "text", comments : "text"}, { weights : { content: 10,
comments: 5, } } );
This index will mean that the content portion of the document will be given 10:5 more precedence than the
comments values. Any other field will have the default weight of 1, compared to the weight of 5 for comments and 10
for content. You can also combine weights and the wildcard text search parameters to weight specific fields.
Be aware that if you have too many text indexes, you will get a “too many text indexes” error. if that happens,
you should drop one of your existing text indexes to allow you to create a new one.
Note
 
 
Search WWH ::




Custom Search