Dan L
Dan L

Reputation: 4439

MongoDB indexing large text field doesn't seem to make query faster?

I have 1.5 million records, each with a text field "body" that contains a lot of text. I'm performing a full-text search against these documents using a regular expression but haven't noticed any difference in query times between indexing the data and not indexing it.

I ensured there was an index on the "body" field via

db.documents.ensureIndex({ body: 1 });

MongoDB took a few moments to index the data, and when I ran

db.documents.getIndexes()

it showed that I had an index on the collection's "body" field. But queries still take the same amount of time before and after indexing.

If I run the query

db.documents.find({ body: /test/i });

I would expect it to run faster because the data is indexed. When I do

db.documents.find({ body: /test/i }).explain();

mongo tells me that it's using the BTreeCursor on the body field.

Am I doing something wrong here? Why would there not be any decrease in query time after the text data has been indexed?

Upvotes: 3

Views: 1679

Answers (3)

Yathish Manjunath
Yathish Manjunath

Reputation: 2029

You need to create a TEXT search index on the field.

 db.documents.ensureIndex({ body: "text" });

once the TEXT search index is created, you can search as below :

 db.documents.find({ "$text": {"$search" : /test/i} });

Upvotes: 0

theAndroid
theAndroid

Reputation: 854

Full text search is a dedicated area where MongoDb doesn't really fit.

If you're looking for something open source & fast, you should try Apache SOLR. We've been using it for 4 years now, a great value!

http://lucene.apache.org/solr/

Upvotes: 2

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

Check the docs for indexes and regex queries:

http://www.mongodb.org/display/DOCS/Advanced+Queries

For simple prefix queries (also called rooted regexps) like /^prefix/, the database will use an index when available and appropriate (much like most SQL databases that use indexes for a LIKE 'prefix%' expression). This only works if you don't have i (case-insensitivity) in the flags.

Upvotes: 2

Related Questions