【发布时间】:2015-09-07 15:38:16
【问题描述】:
我正在尝试使用 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();
}
【问题讨论】:
标签: mongodb indexing full-text-search morphia