1

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?

Community
  • 1
  • 1
Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
  • Who is voting this down? I am having this exact same issue and this question was helpful to me. Please propose an edit or leave a comment if you think this question should be improved. – James Watkins Jun 26 '16 at 16:39

2 Answers2

1

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;
    }
Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
0

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>
James Watkins
  • 4,806
  • 5
  • 32
  • 42