Skip to content

Add hidden support for index #4349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ private static Converter<IndexDefinition, IndexOptions> getIndexDefinitionIndexO
ops.wildcardProjection(indexOptions.get("wildcardProjection", Document.class));
}

if (indexOptions.containsKey("hidden")) {
ops = ops.hidden((Boolean) indexOptions.get("hidden"));
}

return ops;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum Duplicates {
private boolean unique = false;
private boolean sparse = false;
private boolean background = false;
private boolean hidden = false;
private long expire = -1;
private Optional<IndexFilter> filter = Optional.empty();
private Optional<Collation> collation = Optional.empty();
Expand Down Expand Up @@ -106,6 +107,18 @@ public Index background() {
return this;
}

/**
* Hidden indexes are not visible to the query planner and cannot be used to support a query.
*
* @return this.
* @see <a href=
* "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
*/
public Index hidden() {
this.hidden = true;
return this;
}

/**
* Specifies TTL in seconds.
*
Expand Down Expand Up @@ -211,6 +224,9 @@ public Document getIndexOptions() {
if (background) {
document.put("background", true);
}
if (hidden) {
document.put("hidden", true);
}
if (expire >= 0) {
document.put("expireAfterSeconds", expire);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class IndexInfo {
private final boolean unique;
private final boolean sparse;
private final String language;
private final boolean hidden;
private @Nullable Duration expireAfter;
private @Nullable String partialFilterExpression;
private @Nullable Document collation;
Expand All @@ -64,6 +65,17 @@ public IndexInfo(List<IndexField> indexFields, String name, boolean unique, bool
this.unique = unique;
this.sparse = sparse;
this.language = language;
this.hidden = false;
}

public IndexInfo(List<IndexField> indexFields, String name, boolean unique, boolean sparse, String language, boolean hidden) {

this.indexFields = Collections.unmodifiableList(indexFields);
this.name = name;
this.unique = unique;
this.sparse = sparse;
this.language = language;
this.hidden = hidden;
}

/**
Expand Down Expand Up @@ -138,6 +150,8 @@ public static IndexInfo indexInfoOf(Document sourceDocument) {
info.wildcardProjection = sourceDocument.get("wildcardProjection", Document.class);
}

boolean hidden = sourceDocument.containsKey("hidden") ? (Boolean) sourceDocument.get("hidden") : false;

return info;
}

Expand Down Expand Up @@ -259,12 +273,17 @@ public boolean isWildcard() {
return getIndexFields().stream().anyMatch(IndexField::isWildcard);
}

public boolean isHidden() {
return hidden;
}


@Override
public String toString() {

return "IndexInfo [indexFields=" + indexFields + ", name=" + name + ", unique=" + unique + ", sparse=" + sparse
+ ", language=" + language + ", partialFilterExpression=" + partialFilterExpression + ", collation=" + collation
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + "]";
+ ", expireAfterSeconds=" + ObjectUtils.nullSafeToString(expireAfter) + ", hidden=" + hidden + "]";
}

@Override
Expand All @@ -279,6 +298,7 @@ public int hashCode() {
result += 31 * ObjectUtils.nullSafeHashCode(partialFilterExpression);
result += 31 * ObjectUtils.nullSafeHashCode(collation);
result += 31 * ObjectUtils.nullSafeHashCode(expireAfter);
result += 31 * ObjectUtils.nullSafeHashCode(hidden);
return result;
}

Expand Down Expand Up @@ -326,6 +346,9 @@ public boolean equals(Object obj) {
if (!ObjectUtils.nullSafeEquals(expireAfter, other.expireAfter)) {
return false;
}
if (hidden != other.hidden) {
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void testEnsureIndex() throws Exception {
p2.setAge(40);
template.insert(p2);

template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique().hidden());

MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
List<org.bson.Document> indexInfo = new ArrayList<>();
Expand All @@ -350,6 +350,7 @@ public void testEnsureIndex() throws Exception {
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique()).isTrue();
assertThat(ii.isSparse()).isFalse();
assertThat(ii.isHidden()).isTrue();

List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);
Expand Down