我正尝试在使用Mongo + Morphia的类上创建和使用全文搜索。
我注释是这样的:
@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;
...
我试图查询这个集合:
Datastore ds = Dao.instance().getDatabase();
return ds.createQuery(Product.class).search("mens pants").order("companyKey").asList();
但我不断收到此错误:
Query failed with error code 17007 and error message 'Unable to execute query: error processing query:
和
planner returned error: need exactly one text index for $text query'
我感觉索引没有被创建。这有可能是这样吗?
我最近升级了我的Mongo和Morphia版本,之前在几个字段上使用了旧标准索引的组合。这是否会导致一些“冲突”或阻止创建相同字段上的新索引?
编辑
据@ evanchooly的建议查询方法现在看起来是这样的:
public List<Product> textSearch() {
Datastore ds = Dao.instance().getDatabase();
ds.ensureIndexes();
return ds.createQuery(Product.class).search("mens pants").order("companyKey").asList();
}
由于某些原因,无法正常工作。我想我首先需要删除现有的索引,需要设置shell。将在早上尝试。谢谢您的帮助! – Ankur
如果已存在的索引已经存在,并且该名称是根据键等自动生成的,则不会尝试创建新的索引。您可以尝试显式地将此索引命名为唯一或删除具有该索引名称的任何现有索引。 (这个名字是如何产生一个默认名字的,现在转义了我) – evanchooly
另外,如果你没有明确地在你的类所在的包中或类本身上显式调用morphia.map(),morphia当你调用'ensureIndexes()'时不会知道你的类。所以请确保您的类在尝试生成索引之前进行了映射。 – evanchooly