简体   繁体   中英

MongoDB 3, java driver , full text search, how to?

You can see how to use full text search with java driver in mongodb 2

how to use java driver with mongodb 2, full text search

but in mongodb 3 this does not work, anybody know how to do full text search with java driver/mongodb 3?

This is just work if you have index, in the mongodb 3 full text search integrated with find.

public List<ArticleData> doAdvancedSearch(String searchString) {
        List<ArticleData> list = new ArrayList<>();

        DBCursor cursor = collection.find(new BasicDBObject("$text", new BasicDBObject("$search", searchString)));
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            ArticleData data = new ArticleData();
            data.setContent((String) document.get("content"));
            data.setTitle((String) document.get("title"));
            list.add(data);
        }

        return list;
    }

It was yet again different for me.

List<Document> list = new ArrayList<>();
FindIterable<Document> iter = collection.find(new BasicDBObject("$text", new BasicDBObject("$search", query)));

for (Document d : iter) {
    list.add(d);
}

return list;

I'm using

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver</artifactId>
    <version>3.2.2</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM