Browse Source
We now support partial filter expression on indexes via Index.partial(…). This allows to create partial indexes that only index the documents in a collection that meet a specified filter expression.
new Index().named("idx").on("k3y", ASC).partial(filter(where("age").gte(10)))
The filter expression can be set via a plain DBObject or a CriteriaDefinition and is mapped against the associated domain type.
Original pull request: #431.
pull/410/merge
9 changed files with 414 additions and 60 deletions
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Copyright 2016. the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.core.index; |
||||
|
||||
import com.mongodb.DBObject; |
||||
|
||||
/** |
||||
* Use {@link IndexFilter} to create the partial filter expression used when creating |
||||
* <a href="https://docs.mongodb.com/manual/core/index-partial/">Partial Indexes</a>. |
||||
* |
||||
* @author Christoph Strobl |
||||
* @since 1.10 |
||||
*/ |
||||
public interface IndexFilter { |
||||
|
||||
/** |
||||
* Get the raw (unmapped) filter expression. |
||||
* |
||||
* @return |
||||
*/ |
||||
DBObject getFilterObject(); |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
/* |
||||
* Copyright 2016. the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.mongodb.core.index; |
||||
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import com.mongodb.DBObject; |
||||
|
||||
/** |
||||
* {@link IndexFilter} implementation for usage with plain {@link DBObject} as well as {@link CriteriaDefinition} filter |
||||
* expressions. |
||||
* |
||||
* @author Christoph Strobl |
||||
* @since 1.10 |
||||
*/ |
||||
public class PartialIndexFilter implements IndexFilter { |
||||
|
||||
private final Object filterExpression; |
||||
|
||||
private PartialIndexFilter(Object filterExpression) { |
||||
|
||||
Assert.notNull(filterExpression, "FilterExpression must not be null!"); |
||||
this.filterExpression = filterExpression; |
||||
} |
||||
|
||||
/** |
||||
* Create new {@link PartialIndexFilter} for given {@link DBObject filter expression}. |
||||
* |
||||
* @param where must not be {@literal null}. |
||||
* @return |
||||
*/ |
||||
public static PartialIndexFilter filter(DBObject where) { |
||||
return new PartialIndexFilter(where); |
||||
} |
||||
|
||||
/** |
||||
* Create new {@link PartialIndexFilter} for given {@link CriteriaDefinition filter expression}. |
||||
* |
||||
* @param where must not be {@literal null}. |
||||
* @return |
||||
*/ |
||||
public static PartialIndexFilter filter(CriteriaDefinition where) { |
||||
return new PartialIndexFilter(where); |
||||
} |
||||
|
||||
public DBObject getFilterObject() { |
||||
|
||||
if (filterExpression instanceof DBObject) { |
||||
return (DBObject) filterExpression; |
||||
} |
||||
|
||||
if (filterExpression instanceof CriteriaDefinition) { |
||||
return ((CriteriaDefinition) filterExpression).getCriteriaObject(); |
||||
} |
||||
|
||||
throw new IllegalArgumentException( |
||||
String.format("Unknown type %s used as filter expression.", filterExpression.getClass())); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue