gpt4 book ai didi

java - 使用 Java 驱动程序的 MongoDB 文本索引

转载 作者:行者123 更新时间:2023-12-01 11:45:45 24 4
gpt4 key购买 nike

使用 MongoDB Java API,我无法使用文本搜索成功找到完整的示例。我使用的代码是这样的:

DBCollection coll;
String searchString = "Test String";

coll.createIndex(new BasicDBObject ("blogcomments", "text"));
DBObject q = start("blogcomments").text(searchString).get();

我正在执行搜索的 Collections 的名称是 blogcomments。 creatIndex() 是已弃用的方法 ensureIndex() 的替代方法。我看过有关如何使用 createIndex() 的示例,但没有看过如何使用 Java API 执行实际搜索。这是执行此操作的正确方法吗?

最佳答案

这不太正确。使用“文本”类型索引的查询无法在查询时指定字段名称。相反,要包含在索引中的字段名称是在创建索引时指定的。请参阅the documentation举些例子。您的查询将如下所示:

DBObject q = QueryBuilder.start().text(searchString).get();

关于java - 使用 Java 驱动程序的 MongoDB 文本索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29149719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com