I am trying to create and use a full text search on a class using Mongo + Morphia.
I've annotated like this:
@Entity
@Indexes(@Index(fields = @Field(value = "$**", type = IndexType.TEXT)))
public class Product implements Comparable<Product>{
@Id
@Expose public ObjectId id;
@Expose public String name;
@Expose public String key;
@Expose public String category;
@Expose public String brand;
@Expose public String description;
@Expose public String department;
@Expose public String price;
@Expose public String sex;
@Expose public String companyKey;
@Expose public String url;
...
I'm trying to query the Collection with this:
Datastore ds = Dao.instance().getDatabase();
return ds.createQuery(Product.class).search("mens pants").order("companyKey").asList();
But I keep getting this error:
Query failed with error code 17007 and error message 'Unable to execute query: error processing query:
and
planner returned error: need exactly one text index for $text query'
I get the feeling that the index is not being created. Is there any reason why this might be so?
I've recently upgraded my version of Mongo and Morphia and previously was using a combination of older standard indexes on a few fields. Could this cause some 'conflict' or prevent a new index on the same fields being created?
EDIT
According to @evanchooly's suggestion the query method now looks like this:
public List<Product> textSearch() {
Datastore ds = Dao.instance().getDatabase();
ds.ensureIndexes();
return ds.createQuery(Product.class).search("mens pants").order("companyKey").asList();
}
What @evanchooly says in the comments is correct.
The problem seems to be that I had previously created the indexes and needed to delete them first.
So I did this via the MongoDB command line using:
db.Product.dropIndexes()
Doing this before I change the way indexes are annotated in Morphia (and hence how indexes are created) means I'm getting the indexes I want.
Was trying to avoid this since I didn't have the Mongo CLI installed, but in hindsight it makes perfect sense.