rename field facet to term facet

This commit is contained in:
kimchy 2010-06-04 07:03:30 +03:00
parent 6e15e3dd24
commit 0901322337
7 changed files with 42 additions and 36 deletions

View File

@ -272,13 +272,13 @@ public class SearchRequestBuilder {
return this; return this;
} }
public SearchRequestBuilder addFieldFacet(String name, String fieldName, int size) { public SearchRequestBuilder addTermFacet(String name, String fieldName, int size) {
facetsBuilder().fieldFacet(name, fieldName, size); facetsBuilder().termFacet(name, fieldName, size);
return this; return this;
} }
public SearchRequestBuilder addFieldFacet(String name, String fieldName, int size, boolean global) { public SearchRequestBuilder addTermFacet(String name, String fieldName, int size, boolean global) {
facetsBuilder().fieldFacet(name, fieldName, size, global); facetsBuilder().termFacet(name, fieldName, size, global);
return this; return this;
} }

View File

@ -20,6 +20,8 @@
package org.elasticsearch.search.builder; package org.elasticsearch.search.builder;
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder; import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
import org.elasticsearch.search.facets.collector.query.QueryFacetCollectorParser;
import org.elasticsearch.search.facets.collector.term.TermFacetCollectorParser;
import org.elasticsearch.util.xcontent.ToXContent; import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder; import org.elasticsearch.util.xcontent.builder.XContentBuilder;
@ -37,7 +39,7 @@ import static org.elasticsearch.util.collect.Lists.*;
public class SearchSourceFacetsBuilder implements ToXContent { public class SearchSourceFacetsBuilder implements ToXContent {
private List<QueryFacet> queryFacets; private List<QueryFacet> queryFacets;
private List<FieldFacet> fieldFacets; private List<TermFacet> termFacets;
/** /**
* Adds a query facet (which results in a count facet returned). * Adds a query facet (which results in a count facet returned).
@ -65,20 +67,20 @@ public class SearchSourceFacetsBuilder implements ToXContent {
return this; return this;
} }
public SearchSourceFacetsBuilder fieldFacet(String name, String fieldName, int size) { public SearchSourceFacetsBuilder termFacet(String name, String fieldName, int size) {
return fieldFacet(name, fieldName, size, null); return termFacet(name, fieldName, size, null);
} }
public SearchSourceFacetsBuilder fieldFacet(String name, String fieldName, int size, Boolean global) { public SearchSourceFacetsBuilder termFacet(String name, String fieldName, int size, Boolean global) {
if (fieldFacets == null) { if (termFacets == null) {
fieldFacets = newArrayListWithCapacity(2); termFacets = newArrayListWithCapacity(2);
} }
fieldFacets.add(new FieldFacet(name, fieldName, size, global)); termFacets.add(new TermFacet(name, fieldName, size, global));
return this; return this;
} }
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException { @Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
if (queryFacets == null && fieldFacets == null) { if (queryFacets == null && termFacets == null) {
return; return;
} }
builder.field("facets"); builder.field("facets");
@ -88,7 +90,7 @@ public class SearchSourceFacetsBuilder implements ToXContent {
if (queryFacets != null) { if (queryFacets != null) {
for (QueryFacet queryFacet : queryFacets) { for (QueryFacet queryFacet : queryFacets) {
builder.startObject(queryFacet.name()); builder.startObject(queryFacet.name());
builder.field("query"); builder.field(QueryFacetCollectorParser.NAME);
queryFacet.queryBuilder().toXContent(builder, params); queryFacet.queryBuilder().toXContent(builder, params);
if (queryFacet.global() != null) { if (queryFacet.global() != null) {
builder.field("global", queryFacet.global()); builder.field("global", queryFacet.global());
@ -96,17 +98,17 @@ public class SearchSourceFacetsBuilder implements ToXContent {
builder.endObject(); builder.endObject();
} }
} }
if (fieldFacets != null) { if (termFacets != null) {
for (FieldFacet fieldFacet : fieldFacets) { for (TermFacet termFacet : termFacets) {
builder.startObject(fieldFacet.name()); builder.startObject(termFacet.name());
builder.startObject("field"); builder.startObject(TermFacetCollectorParser.NAME);
builder.field("name", fieldFacet.fieldName()); builder.field("field", termFacet.fieldName());
builder.field("size", fieldFacet.size()); builder.field("size", termFacet.size());
builder.endObject(); builder.endObject();
if (fieldFacet.global() != null) { if (termFacet.global() != null) {
builder.field("global", fieldFacet.global()); builder.field("global", termFacet.global());
} }
builder.endObject(); builder.endObject();
@ -116,13 +118,13 @@ public class SearchSourceFacetsBuilder implements ToXContent {
builder.endObject(); builder.endObject();
} }
private static class FieldFacet { private static class TermFacet {
private final String name; private final String name;
private final String fieldName; private final String fieldName;
private final int size; private final int size;
private final Boolean global; private final Boolean global;
private FieldFacet(String name, String fieldName, int size, Boolean global) { private TermFacet(String name, String fieldName, int size, Boolean global) {
this.name = name; this.name = name;
this.fieldName = fieldName; this.fieldName = fieldName;
this.size = size; this.size = size;

View File

@ -23,8 +23,8 @@ import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.SearchParseException; import org.elasticsearch.search.SearchParseException;
import org.elasticsearch.search.facets.collector.FacetCollector; import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.collector.FacetCollectorParser; import org.elasticsearch.search.facets.collector.FacetCollectorParser;
import org.elasticsearch.search.facets.collector.field.FieldFacetCollectorParser;
import org.elasticsearch.search.facets.collector.query.QueryFacetCollectorParser; import org.elasticsearch.search.facets.collector.query.QueryFacetCollectorParser;
import org.elasticsearch.search.facets.collector.term.TermFacetCollectorParser;
import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.util.MapBuilder; import org.elasticsearch.util.MapBuilder;
import org.elasticsearch.util.collect.ImmutableMap; import org.elasticsearch.util.collect.ImmutableMap;
@ -60,8 +60,8 @@ public class FacetsParseElement implements SearchParseElement {
public FacetsParseElement() { public FacetsParseElement() {
MapBuilder<String, FacetCollectorParser> builder = newMapBuilder(); MapBuilder<String, FacetCollectorParser> builder = newMapBuilder();
builder.put("field", new FieldFacetCollectorParser()); builder.put(TermFacetCollectorParser.NAME, new TermFacetCollectorParser());
builder.put("query", new QueryFacetCollectorParser()); builder.put(QueryFacetCollectorParser.NAME, new QueryFacetCollectorParser());
this.facetCollectorParsers = builder.immutableMap(); this.facetCollectorParsers = builder.immutableMap();
} }

View File

@ -31,6 +31,8 @@ import org.elasticsearch.util.xcontent.XContentParser;
*/ */
public class QueryFacetCollectorParser implements FacetCollectorParser { public class QueryFacetCollectorParser implements FacetCollectorParser {
public static final String NAME = "query";
@Override public String name() { @Override public String name() {
return "query"; return "query";
} }

View File

@ -17,7 +17,7 @@
* under the License. * under the License.
*/ */
package org.elasticsearch.search.facets.collector.field; package org.elasticsearch.search.facets.collector.term;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Scorer;
@ -42,7 +42,7 @@ import static org.elasticsearch.index.field.FieldDataOptions.*;
/** /**
* @author kimchy (Shay Banon) * @author kimchy (Shay Banon)
*/ */
public class FieldFacetCollector extends FacetCollector { public class TermFacetCollector extends FacetCollector {
private static ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>> cache = new ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>>() { private static ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>> cache = new ThreadLocal<ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>>>() {
@Override protected ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>> initialValue() { @Override protected ThreadLocals.CleanableValue<Deque<TObjectIntHashMap<String>>> initialValue() {
@ -63,7 +63,7 @@ public class FieldFacetCollector extends FacetCollector {
private final TObjectIntHashMap<String> facets; private final TObjectIntHashMap<String> facets;
public FieldFacetCollector(String name, String fieldName, FieldDataCache fieldDataCache, int size) { public TermFacetCollector(String name, String fieldName, FieldDataCache fieldDataCache, int size) {
this.name = name; this.name = name;
this.fieldDataCache = fieldDataCache; this.fieldDataCache = fieldDataCache;
this.fieldName = fieldName; this.fieldName = fieldName;

View File

@ -17,7 +17,7 @@
* under the License. * under the License.
*/ */
package org.elasticsearch.search.facets.collector.field; package org.elasticsearch.search.facets.collector.term;
import org.elasticsearch.search.facets.collector.FacetCollector; import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.collector.FacetCollectorParser; import org.elasticsearch.search.facets.collector.FacetCollectorParser;
@ -29,10 +29,12 @@ import java.io.IOException;
/** /**
* @author kimchy (Shay Banon) * @author kimchy (Shay Banon)
*/ */
public class FieldFacetCollectorParser implements FacetCollectorParser { public class TermFacetCollectorParser implements FacetCollectorParser {
public static final String NAME = "term";
@Override public String name() { @Override public String name() {
return "field"; return NAME;
} }
@Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException { @Override public FacetCollector parser(String facetName, XContentParser parser, SearchContext context) throws IOException {
@ -45,13 +47,13 @@ public class FieldFacetCollectorParser implements FacetCollectorParser {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
termFieldName = parser.currentName(); termFieldName = parser.currentName();
} else if (token.isValue()) { } else if (token.isValue()) {
if ("name".equals(termFieldName)) { if ("field".equals(termFieldName)) {
field = parser.text(); field = parser.text();
} else if ("size".equals(termFieldName)) { } else if ("size".equals(termFieldName)) {
size = parser.intValue(); size = parser.intValue();
} }
} }
} }
return new FieldFacetCollector(facetName, field, context.fieldDataCache(), size); return new TermFacetCollector(facetName, field, context.fieldDataCache(), size);
} }
} }

View File

@ -76,8 +76,8 @@ public class SimpleFacetsTests extends AbstractNodesTests {
SearchResponse searchResponse = client.prepareSearch() SearchResponse searchResponse = client.prepareSearch()
.setQuery(termQuery("stag", "111")) .setQuery(termQuery("stag", "111"))
.addFieldFacet("facet1", "stag", 10) .addTermFacet("facet1", "stag", 10)
.addFieldFacet("facet2", "tag", 10) .addTermFacet("facet2", "tag", 10)
.execute().actionGet(); .execute().actionGet();
MultiCountFacet<String> facet = (MultiCountFacet<String>) searchResponse.facets().facet("facet1"); MultiCountFacet<String> facet = (MultiCountFacet<String>) searchResponse.facets().facet("facet1");