我正在尝试使用 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 的版本,之前在一些字段上使用了旧标准索引的组合。这是否会导致一些“冲突”或阻止在相同字段上创建新索引?
编辑
根据@evancholy的建议,查询方法现在看起来像这样:
public List<Product> textSearch() {
Datastore ds = Dao.instance().getDatabase();
ds.ensureIndexes();
return ds.createQuery(Product.class).search("mens pants").order("companyKey").asList();
}
最佳答案
您需要运行datastore.ensureIndexes()
才能在数据库中实际创建索引。
关于mongodb - Mongo/Morphia - $text 查询只需要一个文本索引',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32442137/