File: text-search.md

package info (click to toggle)
mongo-java-driver 3.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 16,112 kB
  • sloc: java: 102,506; xml: 395; sh: 43; makefile: 4
file content (130 lines) | stat: -rw-r--r-- 5,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
+++
date = "2016-06-07T23:28:50-04:00"
title = "Text Search"
[menu.main]
parent = "Sync Tutorials"
identifier = "Text Search"
weight = 80
pre = "<i class='fa'></i>"
+++

## Text Search

MongoDB supports query operations that perform a [text search]({{<docsref "text-search">}}) of string content. To perform text search, MongoDB uses a [text index]({{<docsref "core/index-text">}}) and the [`$text` query operator]({{<docsref "reference/operator/query/text">}}).

The Java driver provides the [`Filters.text()`]({{<apiref "com/mongodb/client/model/Filters.html#text-java.lang.String-com.mongodb.client.model.TextSearchOptions-">}}) helper to facilitate the creation of text search query filters.

## Prerequisites

- The example below requires a ``restaurants`` collection in the ``test`` database. To create and populate the collection, follow the directions in [github] (https://github.com/mongodb/docs-assets/tree/drivers).

- Include the following import statements:

     ```java
     import com.mongodb.Block;
     import com.mongodb.MongoClient;
     import com.mongodb.client.MongoCollection;
     import com.mongodb.client.MongoDatabase;

     import com.mongodb.client.model.Indexes;
     import com.mongodb.client.model.Filters;
     import com.mongodb.client.model.Sorts;
     import com.mongodb.client.model.TextSearchOptions;
     import com.mongodb.client.model.Projections;
     import org.bson.Document;
     ```

- Include the following code which the examples in the tutorials will use to print the results of the text search:

     ```java
     Block<Document> printBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
     ```

## Connect to a MongoDB Deployment

Connect to a MongoDB deployment and declare and define a `MongoDatabase` instance.

For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port `27017` and define `database` to refer to the `test` database:

```java
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("test");
```

For additional information on connecting to MongoDB, see [Connect to MongoDB]({{< ref "connect-to-mongodb.md">}}).

## Create the `text` Index

To create a [text index]({{<docsref "core/index-text">}}), use the [`Indexes.text`]({{< relref "builders/indexes.md#text-index">}})
static helper to create a specification for a text index and pass to [`MongoCollection.createIndex()`]({{<apiref "com/mongodb/client/MongoCollection.html#createIndex-org.bson.conversions.Bson-">}}) method.

The following example creates a text index on the `name` field for the `restaurants` collection.

```java
MongoCollection<Document> collection = database.getCollection("restaurants");
collection.createIndex(Indexes.text("name"));
```

## Perform Text Search

To perform text search, use the [`Filters.text()`]({{<apiref "com/mongodb/client/model/Filters.html#text-java.lang.String-com.mongodb.client.model.TextSearchOptions-">}}) helper to specify the text search query filter.

For example, the following code performs a text search on the `name` field for the word `"bakery"` or `"coffee"`.

```java
long matchCount = collection.count(Filters.text("bakery coffee"));
System.out.println("Text search matches: " + matchCount);
```

The example should print the following output:

```json
Text search matches: 2
```

For more information on the text search, see [`$text` operator]({{<docsref "reference/operator/query/text">}}).

### Text Score

For each matching document, text search assigns a score, representing the relevance of a document to the specified text search query filter. To return and sort by score, use the [`$meta`]({{<docsref "reference/operator/query/text/#sort-by-text-search-score">}}) operator in the projection document and the sort expression.


```java
collection.find(Filters.text("bakery cafe"))
                              .projection(Projections.metaTextScore("score"))
                              .sort(Sorts.metaTextScore("score")).forEach(printBlock);
```

The example should print the following output:



### Specify a Text Search Option

The  [`Filters.text()`]({{<apiref "com/mongodb/client/model/Filters.html#text-java.lang.String-com.mongodb.client.model.TextSearchOptions-">}}) helper can accept various [text search options]({{<docsref "reference/operator/query/text">}}). The Java driver provides [`TextSearchOptions`]({{<apiref "com/mongodb/client/model/TextSearchOptions.html">}}) class to specify these options.

For example, the following text search specifies the [text search language]({{<docsref "reference/text-search-languages">}}) option when performing text search for the word `cafe`:

```java
long matchCountEnglish = collection.count(Filters.text("cafe", new TextSearchOptions().language("english")));
System.out.println("Text search matches (english): " + matchCountEnglish);
```

The example should print the following output:

```json
Text search matches (english): 1
```

For more information about text search see the following sections in the MongoDB Server Manual:

- [`$text` query operator]({{< docsref "reference/operator/query/text">}})

- [`text` index]({{< docsref "core/index-text" >}})

- [Text Search Languages]({{<docsref "reference/text-search-languages">}})