Skip to content

Commit 4296d62

Browse files
fhassakjyemin
authored andcommitted
JAVA-2793 : add method for wildcard text index
1 parent 654ec7f commit 4296d62

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

docs/reference/content/builders/indexes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ This example specifies a text index key for the `description` field:
7777
text("description")
7878
```
7979

80+
This example specifies a text index key for every field that contains string data:
81+
82+
```java
83+
text()
84+
```
85+
8086
### Hashed Index
8187

8288
To specify a [hashed]({{< docsref "core/index-hashed" >}}) index key, use the `hashed` method.

driver-core/src/main/com/mongodb/client/model/Indexes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ public static Bson text(final String fieldName) {
158158
return new BsonDocument(fieldName, new BsonString("text"));
159159
}
160160

161+
/**
162+
* Create an index key for a text index on every field that contains string data.
163+
*
164+
* @return the index specification
165+
* @mongodb.driver.manual core/text text index
166+
*/
167+
public static Bson text() {
168+
return text("$**");
169+
}
170+
161171
/**
162172
* Create an index key for a hashed index on the given field.
163173
*

driver-core/src/test/functional/com/mongodb/client/model/IndexesFunctionalSpecification.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ class IndexesFunctionalSpecification extends OperationFunctionalSpecification {
114114
getCollectionHelper().listIndexes()*.get('key').contains(parse('{_fts: "text", _ftsx: 1}'))
115115
}
116116

117+
def 'text wildcard'() {
118+
when:
119+
getCollectionHelper().createIndex(text())
120+
121+
then:
122+
getCollectionHelper().listIndexes()*.get('key').contains(parse('{_fts: "text", _ftsx: 1}'))
123+
}
124+
117125
def 'hashed'() {
118126
when:
119127
getCollectionHelper().createIndex(hashed('x'))

driver-core/src/test/unit/com/mongodb/client/model/IndexesSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class IndexesSpecification extends Specification {
6969

7070
def 'text'() {
7171
expect:
72-
toBson(text('x',)) == parse('{x : "text"}')
72+
toBson(text('x')) == parse('{x : "text"}')
73+
toBson(text()) == parse('{ "$**" : "text"}')
7374
}
7475

7576
def 'hashed'() {

0 commit comments

Comments
 (0)