org.elasticsearch.search.facet.internal.InternalFacets Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* 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.facet.internal;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.Facets;
import org.elasticsearch.search.facet.filter.InternalFilterFacet;
import org.elasticsearch.search.facet.geodistance.InternalGeoDistanceFacet;
import org.elasticsearch.search.facet.histogram.InternalHistogramFacet;
import org.elasticsearch.search.facet.query.InternalQueryFacet;
import org.elasticsearch.search.facet.range.InternalRangeFacet;
import org.elasticsearch.search.facet.statistical.InternalStatisticalFacet;
import org.elasticsearch.search.facet.terms.InternalTermsFacet;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.collect.Maps.*;
/**
* @author kimchy (shay.banon)
*/
public class InternalFacets implements Facets, Streamable, ToXContent, Iterable {
private List facets = ImmutableList.of();
private Map facetsAsMap;
private InternalFacets() {
}
/**
* Constructs a new facets.
*/
public InternalFacets(List facets) {
this.facets = facets;
}
/**
* Iterates over the {@link Facet}s.
*/
@Override public Iterator iterator() {
return facets.iterator();
}
/**
* The list of {@link Facet}s.
*/
public List facets() {
return facets;
}
/**
* Returns the {@link Facet}s keyed by map.
*/
public Map getFacets() {
return facetsAsMap();
}
/**
* Returns the {@link Facet}s keyed by map.
*/
public Map facetsAsMap() {
if (facetsAsMap != null) {
return facetsAsMap;
}
Map facetsAsMap = newHashMap();
for (Facet facet : facets) {
facetsAsMap.put(facet.name(), facet);
}
this.facetsAsMap = facetsAsMap;
return facetsAsMap;
}
/**
* Returns the facet by name already casted to the specified type.
*/
@Override public T facet(Class facetType, String name) {
return facetType.cast(facet(name));
}
/**
* A facet of the specified name.
*/
@SuppressWarnings({"unchecked"}) @Override public T facet(String name) {
return (T) facetsAsMap().get(name);
}
static final class Fields {
static final XContentBuilderString FACETS = new XContentBuilderString("facets");
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.FACETS);
for (Facet facet : facets) {
((InternalFacet) facet).toXContent(builder, params);
}
builder.endObject();
}
public static InternalFacets readFacets(StreamInput in) throws IOException {
InternalFacets result = new InternalFacets();
result.readFrom(in);
return result;
}
@Override public void readFrom(StreamInput in) throws IOException {
int size = in.readVInt();
if (size == 0) {
facets = ImmutableList.of();
facetsAsMap = ImmutableMap.of();
} else {
facets = Lists.newArrayListWithCapacity(size);
for (int i = 0; i < size; i++) {
int id = in.readVInt();
if (id == Facet.Type.TERMS.id()) {
facets.add(InternalTermsFacet.readTermsFacet(in));
} else if (id == Facet.Type.QUERY.id()) {
facets.add(InternalQueryFacet.readCountFacet(in));
} else if (id == Facet.Type.STATISTICAL.id()) {
facets.add(InternalStatisticalFacet.readStatisticalFacet(in));
} else if (id == Facet.Type.HISTOGRAM.id()) {
facets.add(InternalHistogramFacet.readHistogramFacet(in));
} else if (id == Facet.Type.GEO_DISTANCE.id()) {
facets.add(InternalGeoDistanceFacet.readGeoDistanceFacet(in));
} else if (id == Facet.Type.RANGE.id()) {
facets.add(InternalRangeFacet.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 + "]");
}
}
}
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(facets.size());
for (Facet facet : facets) {
out.writeVInt(facet.type().id());
((InternalFacet) facet).writeTo(out);
}
}
}