Facets: Filter based facet, closes #289.

This commit is contained in:
kimchy 2010-08-02 19:17:57 +03:00
parent 4e6087ee2d
commit dc6ef326d9
28 changed files with 445 additions and 107 deletions

View File

@ -20,8 +20,11 @@
package org.elasticsearch.search.facets;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
@ -31,14 +34,14 @@ public abstract class AbstractFacetBuilder implements ToXContent {
protected Boolean global;
protected XContentFilterBuilder filter;
protected XContentFilterBuilder facetFilter;
protected AbstractFacetBuilder(String name) {
this.name = name;
}
public AbstractFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public AbstractFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -46,4 +49,15 @@ public abstract class AbstractFacetBuilder implements ToXContent {
this.global = global;
return this;
}
protected void addFilterFacetAndGlobal(XContentBuilder builder, Params params) throws IOException {
if (facetFilter != null) {
builder.field("facet_filter");
facetFilter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.search.facets;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.search.facets.filter.FilterFacet;
import org.elasticsearch.search.facets.geodistance.GeoDistanceFacet;
import org.elasticsearch.search.facets.histogram.HistogramFacet;
import org.elasticsearch.search.facets.query.QueryFacet;
@ -61,7 +62,11 @@ public interface Facet {
/**
* Geo Distance facet type, matching {@link RangeFacet}.
*/
RANGE(5, RangeFacet.class);
RANGE(5, RangeFacet.class),
/**
* Filter facet type, matching {@link FilterFacet}.
*/
FILTER(6, FilterFacet.class);
private int id;

View File

@ -19,7 +19,9 @@
package org.elasticsearch.search.facets;
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
import org.elasticsearch.search.facets.filter.FilterFacetBuilder;
import org.elasticsearch.search.facets.geodistance.GeoDistanceFacetBuilder;
import org.elasticsearch.search.facets.histogram.HistogramFacetBuilder;
import org.elasticsearch.search.facets.histogram.HistogramScriptFacetBuilder;
@ -43,6 +45,14 @@ public class FacetBuilders {
return new QueryFacetBuilder(facetName).query(query);
}
public static FilterFacetBuilder filterFacet(String facetName) {
return new FilterFacetBuilder(facetName);
}
public static FilterFacetBuilder filterFacet(String facetName, XContentFilterBuilder filter) {
return new FilterFacetBuilder(facetName).filter(filter);
}
public static TermsFacetBuilder termsFacet(String facetName) {
return new TermsFacetBuilder(facetName);
}

View File

@ -29,6 +29,7 @@ import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.collector.FacetCollectorParser;
import org.elasticsearch.search.facets.filter.FilterFacetCollectorParser;
import org.elasticsearch.search.facets.geodistance.GeoDistanceFacetCollectorParser;
import org.elasticsearch.search.facets.histogram.HistogramFacetCollectorParser;
import org.elasticsearch.search.facets.query.QueryFacetCollectorParser;
@ -72,6 +73,7 @@ public class FacetsParseElement implements SearchParseElement {
addFacetParser(builder, new HistogramFacetCollectorParser());
addFacetParser(builder, new GeoDistanceFacetCollectorParser());
addFacetParser(builder, new RangeFacetCollectorParser());
addFacetParser(builder, new FilterFacetCollectorParser());
this.facetCollectorParsers = builder.immutableMap();
}
@ -100,7 +102,7 @@ public class FacetsParseElement implements SearchParseElement {
if (token == XContentParser.Token.FIELD_NAME) {
facetFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("filter".equals(facetFieldName)) {
if ("facet_filter".equals(facetFieldName) || "facetFilter".equals(facetFieldName)) {
XContentIndexQueryParser indexQueryParser = (XContentIndexQueryParser) context.queryParser();
filter = indexQueryParser.parseInnerFilter(parser);
} else {
@ -108,7 +110,7 @@ public class FacetsParseElement implements SearchParseElement {
if (facetCollectorParser == null) {
throw new SearchParseException(context, "No facet type for [" + facetFieldName + "]");
}
facet = facetCollectorParser.parser(topLevelFieldName, parser, context);
facet = facetCollectorParser.parse(topLevelFieldName, parser, context);
}
} else if (token.isValue()) {
if ("global".equals(facetFieldName)) {

View File

@ -31,5 +31,5 @@ public interface FacetCollectorParser {
String[] names();
FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException;
FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException;
}

View File

@ -0,0 +1,40 @@
/*
* 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.search.facets.filter;
import org.elasticsearch.search.facets.Facet;
/**
* A query facets returns the count (number of hits) for a facet based on a query.
*
* @author kimchy (shay.banon)
*/
public interface FilterFacet extends Facet {
/**
* The count of the facet.
*/
long count();
/**
* The count of the facet.
*/
long getCount();
}

View File

@ -0,0 +1,73 @@
/*
* 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.search.facets.filter;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilderException;
import org.elasticsearch.search.facets.AbstractFacetBuilder;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class FilterFacetBuilder extends AbstractFacetBuilder {
private XContentFilterBuilder filter;
public FilterFacetBuilder(String name) {
super(name);
}
public FilterFacetBuilder global(boolean global) {
this.global = global;
return this;
}
public FilterFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
public FilterFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
return this;
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
if (filter == null) {
throw new SearchSourceBuilderException("filter must be set on filter facet for facet [" + name + "]");
}
builder.startObject(name);
builder.field(FilterFacetCollectorParser.NAME);
filter.toXContent(builder, params);
if (facetFilter != null) {
builder.field("filter");
facetFilter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
builder.endObject();
}
}

View File

@ -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.search.facets.filter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.lucene.docset.DocSet;
import org.elasticsearch.common.lucene.docset.DocSets;
import org.elasticsearch.index.cache.filter.FilterCache;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.support.AbstractFacetCollector;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class FilterFacetCollector extends AbstractFacetCollector {
private final Filter filter;
private DocSet docSet;
private int count = 0;
public FilterFacetCollector(String facetName, Filter filter, FilterCache filterCache) {
super(facetName);
this.filter = filterCache.cache(filter);
}
@Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException {
docSet = DocSets.convert(reader, filter.getDocIdSet(reader));
}
@Override protected void doCollect(int doc) throws IOException {
if (docSet.get(doc)) {
count++;
}
}
@Override public Facet facet() {
return new InternalFilterFacet(facetName, count);
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.search.facets.filter;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.xcontent.XContentIndexQueryParser;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.collector.FacetCollectorParser;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class FilterFacetCollectorParser implements FacetCollectorParser {
public static final String NAME = "filter";
@Override public String[] names() {
return new String[]{"filter"};
}
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
XContentIndexQueryParser indexQueryParser = (XContentIndexQueryParser) context.queryParser();
Filter facetFilter = indexQueryParser.parseInnerFilter(parser);
return new FilterFacetCollector(facetName, facetFilter, context.filterCache());
}
}

View File

@ -0,0 +1,113 @@
/*
* 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.search.facets.filter;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.internal.InternalFacet;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class InternalFilterFacet implements FilterFacet, InternalFacet {
private String name;
private long count;
private InternalFilterFacet() {
}
public InternalFilterFacet(String name, long count) {
this.name = name;
this.count = count;
}
@Override public Type type() {
return Type.FILTER;
}
@Override public Type getType() {
return type();
}
/**
* The "logical" name of the facet.
*/
public String name() {
return name;
}
@Override public String getName() {
return name();
}
/**
* The count of the facet.
*/
public long count() {
return count;
}
/**
* The count of the facet.
*/
public long getCount() {
return count;
}
@Override public Facet aggregate(Iterable<Facet> facets) {
int count = 0;
for (Facet facet : facets) {
if (facet.name().equals(name)) {
count += ((FilterFacet) facet).count();
}
}
return new InternalFilterFacet(name, count);
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name);
builder.field("_type", FilterFacetCollectorParser.NAME);
builder.field("count", count);
builder.endObject();
}
public static FilterFacet readFilterFacet(StreamInput in) throws IOException {
InternalFilterFacet result = new InternalFilterFacet();
result.readFrom(in);
return result;
}
@Override public void readFrom(StreamInput in) throws IOException {
name = in.readUTF();
count = in.readVLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(name);
out.writeVLong(count);
}
}

View File

@ -191,8 +191,8 @@ public class GeoDistanceFacetBuilder extends AbstractFacetBuilder {
return this;
}
public GeoDistanceFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public GeoDistanceFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -248,14 +248,7 @@ public class GeoDistanceFacetBuilder extends AbstractFacetBuilder {
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -53,7 +53,7 @@ public class GeoDistanceFacetCollectorParser implements FacetCollectorParser {
return new String[]{NAME, "geoDistance"};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String fieldName = null;
String valueFieldName = null;
String valueScript = null;

View File

@ -108,8 +108,8 @@ public class HistogramFacetBuilder extends AbstractFacetBuilder {
/**
* An additional filter used to further filter down the set of documents the facet will run on.
*/
public HistogramFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public HistogramFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -135,14 +135,7 @@ public class HistogramFacetBuilder extends AbstractFacetBuilder {
}
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -40,7 +40,7 @@ public class HistogramFacetCollectorParser implements FacetCollectorParser {
return new String[]{NAME};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String keyField = null;
String valueField = null;
String keyScript = null;

View File

@ -75,8 +75,8 @@ public class HistogramScriptFacetBuilder extends AbstractFacetBuilder {
return this;
}
public HistogramScriptFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public HistogramScriptFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -104,14 +104,7 @@ public class HistogramScriptFacetBuilder extends AbstractFacetBuilder {
}
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -29,6 +29,7 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.Facets;
import org.elasticsearch.search.facets.filter.InternalFilterFacet;
import org.elasticsearch.search.facets.geodistance.InternalGeoDistanceFacet;
import org.elasticsearch.search.facets.histogram.InternalHistogramFacet;
import org.elasticsearch.search.facets.query.InternalQueryFacet;
@ -148,6 +149,8 @@ public class InternalFacets implements Facets, Streamable, ToXContent, Iterable<
facets.add(InternalGeoDistanceFacet.readGeoDistanceFacet(in));
} else if (id == Facet.Type.RANGE.id()) {
facets.add(InternalRangeDistanceFacet.readRangeFacet(in));
} else if (id == Facet.Type.FILTER.id()) {
facets.add(InternalFilterFacet.readFilterFacet(in));
} else {
throw new IOException("Can't handle facet type with id [" + id + "]");
}

View File

@ -78,10 +78,6 @@ public class InternalQueryFacet implements QueryFacet, InternalFacet {
return count;
}
public void increment(long increment) {
count += increment;
}
@Override public Facet aggregate(Iterable<Facet> facets) {
int count = 0;
for (Facet facet : facets) {
@ -94,7 +90,7 @@ public class InternalQueryFacet implements QueryFacet, InternalFacet {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name);
builder.field("_type", "query");
builder.field("_type", QueryFacetCollectorParser.NAME);
builder.field("count", count);
builder.endObject();
}

View File

@ -43,8 +43,8 @@ public class QueryFacetBuilder extends AbstractFacetBuilder {
return this;
}
public QueryFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public QueryFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -61,14 +61,8 @@ public class QueryFacetBuilder extends AbstractFacetBuilder {
builder.field(QueryFacetCollectorParser.NAME);
query.toXContent(builder, params);
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
addFilterFacetAndGlobal(builder, params);
if (global != null) {
builder.field("global", global);
}
builder.endObject();
}
}

View File

@ -37,7 +37,7 @@ public class QueryFacetCollectorParser implements FacetCollectorParser {
return new String[]{"query"};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) {
XContentIndexQueryParser indexQueryParser = (XContentIndexQueryParser) context.queryParser();
Query facetQuery = indexQueryParser.parse(parser);
return new QueryFacetCollector(facetName, facetQuery, context.filterCache());

View File

@ -136,8 +136,8 @@ public class RangeFacetBuilder extends AbstractFacetBuilder {
/**
* An additional filter used to further filter down the set of documents the facet will run on.
*/
public RangeFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public RangeFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -179,14 +179,7 @@ public class RangeFacetBuilder extends AbstractFacetBuilder {
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -42,7 +42,7 @@ public class RangeFacetCollectorParser implements FacetCollectorParser {
return new String[]{NAME};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String keyField = null;
String valueField = null;
String keyScript = null;

View File

@ -99,8 +99,8 @@ public class RangeScriptFacetBuilder extends AbstractFacetBuilder {
return this;
}
public RangeScriptFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public RangeScriptFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -141,14 +141,7 @@ public class RangeScriptFacetBuilder extends AbstractFacetBuilder {
}
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -46,8 +46,8 @@ public class StatisticalFacetBuilder extends AbstractFacetBuilder {
return this;
}
public StatisticalFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public StatisticalFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -61,14 +61,7 @@ public class StatisticalFacetBuilder extends AbstractFacetBuilder {
builder.field("field", fieldName);
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -47,7 +47,7 @@ public class StatisticalFacetCollectorParser implements FacetCollectorParser {
return new String[]{NAME};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String field = null;
String currentFieldName = null;

View File

@ -44,8 +44,8 @@ public class StatisticalScriptFacetBuilder extends AbstractFacetBuilder {
return this;
}
public StatisticalScriptFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public StatisticalScriptFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -76,14 +76,7 @@ public class StatisticalScriptFacetBuilder extends AbstractFacetBuilder {
}
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -46,8 +46,8 @@ public class TermsFacetBuilder extends AbstractFacetBuilder {
return this;
}
public TermsFacetBuilder filter(XContentFilterBuilder filter) {
this.filter = filter;
public TermsFacetBuilder facetFilter(XContentFilterBuilder filter) {
this.facetFilter = filter;
return this;
}
@ -100,13 +100,7 @@ public class TermsFacetBuilder extends AbstractFacetBuilder {
}
builder.endObject();
if (filter != null) {
builder.field("filter");
filter.toXContent(builder, params);
}
if (global != null) {
builder.field("global", global);
}
addFilterFacetAndGlobal(builder, params);
builder.endObject();
}

View File

@ -40,7 +40,7 @@ public class TermsFacetCollectorParser implements FacetCollectorParser {
return new String[]{NAME};
}
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@Override public FacetCollector parse(String facetName, XContentParser parser, SearchContext context) throws IOException {
String field = null;
int size = 10;

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.facets.filter.FilterFacet;
import org.elasticsearch.search.facets.histogram.HistogramFacet;
import org.elasticsearch.search.facets.range.RangeFacet;
import org.elasticsearch.search.facets.statistical.StatisticalFacet;
@ -116,6 +117,40 @@ public class SimpleFacetsTests extends AbstractNodesTests {
assertThat(facet.entries().get(0).count(), equalTo(2));
}
@Test public void testFilterFacets() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test").execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("stag", "111")
.startArray("tag").value("xxx").value("yyy").endArray()
.endObject()).execute().actionGet();
client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();
client.prepareIndex("test", "type1").setSource(jsonBuilder().startObject()
.field("stag", "111")
.startArray("tag").value("zzz").value("yyy").endArray()
.endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(filterFacet("facet1").filter(termFilter("stag", "111")))
.addFacet(filterFacet("facet2").filter(termFilter("tag", "xxx")))
.addFacet(filterFacet("facet3").filter(termFilter("tag", "yyy")))
.execute().actionGet();
FilterFacet facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.count(), equalTo(2l));
}
@Test public void testTermsFacets() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
@ -158,7 +193,7 @@ public class SimpleFacetsTests extends AbstractNodesTests {
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("stag").size(10).filter(termFilter("tag", "xxx")))
.addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");