forked from pepite/elasticsearch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query DSL: Add limit filter, closes elastic#976.
- Loading branch information
Showing
11 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/LimitFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you 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.elasticsearch.common.lucene.search; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.DocIdSet; | ||
import org.elasticsearch.common.lucene.docset.GetDocSet; | ||
|
||
import java.io.IOException; | ||
|
||
public class LimitFilter extends NoCacheFilter { | ||
|
||
private final int limit; | ||
|
||
public LimitFilter(int limit) { | ||
this.limit = limit; | ||
} | ||
|
||
public int getLimit() { | ||
return limit; | ||
} | ||
|
||
@Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { | ||
return new LimitDocSet(reader.maxDoc(), limit); | ||
} | ||
|
||
public static class LimitDocSet extends GetDocSet { | ||
|
||
private final int limit; | ||
private int counter; | ||
|
||
public LimitDocSet(int maxDoc, int limit) { | ||
super(maxDoc); | ||
this.limit = limit; | ||
} | ||
|
||
@Override public boolean get(int doc) throws IOException { | ||
if (++counter > limit) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...les/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/NoCacheFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you 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.elasticsearch.common.lucene.search; | ||
|
||
import org.apache.lucene.search.Filter; | ||
|
||
/** | ||
* A marker interface for {@link org.apache.lucene.search.Filter} denoting the filter | ||
* as one that should not be cached, ever. | ||
*/ | ||
public abstract class NoCacheFilter extends Filter { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...lasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/LimitFilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you 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.elasticsearch.index.query.xcontent; | ||
|
||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
public class LimitFilterBuilder extends BaseFilterBuilder { | ||
|
||
private final int limit; | ||
|
||
public LimitFilterBuilder(int limit) { | ||
this.limit = limit; | ||
} | ||
|
||
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(LimitFilterParser.NAME); | ||
builder.field("value", limit); | ||
builder.endObject(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/LimitFilterParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Licensed to Elastic Search and Shay Banon under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Elastic Search licenses this | ||
* file to you 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.elasticsearch.index.query.xcontent; | ||
|
||
import org.apache.lucene.search.Filter; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.lucene.search.LimitFilter; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.index.AbstractIndexComponent; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.query.QueryParsingException; | ||
import org.elasticsearch.index.settings.IndexSettings; | ||
|
||
import java.io.IOException; | ||
|
||
public class LimitFilterParser extends AbstractIndexComponent implements XContentFilterParser { | ||
|
||
public static final String NAME = "limit"; | ||
|
||
@Inject public LimitFilterParser(Index index, @IndexSettings Settings settings) { | ||
super(index, settings); | ||
} | ||
|
||
@Override public String[] names() { | ||
return new String[]{NAME}; | ||
} | ||
|
||
@Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { | ||
XContentParser parser = parseContext.parser(); | ||
|
||
int limit = -1; | ||
String currentFieldName = null; | ||
XContentParser.Token token; | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
} else if (token.isValue()) { | ||
if ("value".equals(currentFieldName)) { | ||
limit = parser.intValue(); | ||
} | ||
} | ||
} | ||
|
||
if (limit == -1) { | ||
throw new QueryParsingException(index, "No value specified for limit filter"); | ||
} | ||
|
||
return new LimitFilter(limit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/limit-filter.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"filtered" : { | ||
"filter" : { | ||
"limit" : { | ||
"value" : 2 | ||
} | ||
}, | ||
"query" : { | ||
"term" : { | ||
"name.first" : "shay" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters