nicer code, internalize Facets and create a clean external interface
This commit is contained in:
parent
81deb833de
commit
f0c56ce18e
|
@ -32,7 +32,7 @@
|
||||||
<option name="LOCAL" value="true" />
|
<option name="LOCAL" value="true" />
|
||||||
</RunnerSettings>
|
</RunnerSettings>
|
||||||
<RunnerSettings RunnerId="Profile ">
|
<RunnerSettings RunnerId="Profile ">
|
||||||
<option name="myExternalizedOptions" value=" snapshots-dir= additional-options2=onexit\=snapshot " />
|
<option name="myExternalizedOptions" value=" additional-options2=onexit\=snapshot " />
|
||||||
</RunnerSettings>
|
</RunnerSettings>
|
||||||
<RunnerSettings RunnerId="Run" />
|
<RunnerSettings RunnerId="Run" />
|
||||||
<ConfigurationWrapper RunnerId="Debug" />
|
<ConfigurationWrapper RunnerId="Debug" />
|
||||||
|
|
|
@ -23,11 +23,10 @@ import org.elasticsearch.ElasticSearchException;
|
||||||
import org.elasticsearch.action.ActionListener;
|
import org.elasticsearch.action.ActionListener;
|
||||||
import org.elasticsearch.action.ListenableActionFuture;
|
import org.elasticsearch.action.ListenableActionFuture;
|
||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
|
import org.elasticsearch.util.collect.Lists;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.elasticsearch.util.collect.Lists.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kimchy (shay.banon)
|
* @author kimchy (shay.banon)
|
||||||
*/
|
*/
|
||||||
|
@ -67,7 +66,7 @@ public abstract class AbstractListenableActionFuture<T, L> extends AdapterAction
|
||||||
((List) this.listeners).add(listener);
|
((List) this.listeners).add(listener);
|
||||||
} else {
|
} else {
|
||||||
Object orig = listeners;
|
Object orig = listeners;
|
||||||
listeners = newArrayListWithExpectedSize(2);
|
listeners = Lists.newArrayListWithCapacity(2);
|
||||||
((List) listeners).add(orig);
|
((List) listeners).add(orig);
|
||||||
((List) listeners).add(listener);
|
((List) listeners).add(listener);
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,6 @@ import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.elasticsearch.util.MapBuilder.*;
|
import static org.elasticsearch.util.MapBuilder.*;
|
||||||
import static org.elasticsearch.util.collect.Lists.*;
|
|
||||||
import static org.elasticsearch.util.collect.Sets.*;
|
import static org.elasticsearch.util.collect.Sets.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,7 +158,7 @@ public class MetaData implements Iterable<IndexMetaData> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<String> actualIndices = newArrayListWithExpectedSize(indices.length);
|
ArrayList<String> actualIndices = Lists.newArrayListWithCapacity(indices.length);
|
||||||
for (String index : indices) {
|
for (String index : indices) {
|
||||||
String[] actualLst = aliasAndIndexToIndexMap.get(index);
|
String[] actualLst = aliasAndIndexToIndexMap.get(index);
|
||||||
if (actualLst == null) {
|
if (actualLst == null) {
|
||||||
|
|
|
@ -21,6 +21,7 @@ package org.elasticsearch.cluster.routing;
|
||||||
|
|
||||||
import org.elasticsearch.index.shard.ShardId;
|
import org.elasticsearch.index.shard.ShardId;
|
||||||
import org.elasticsearch.util.collect.ImmutableList;
|
import org.elasticsearch.util.collect.ImmutableList;
|
||||||
|
import org.elasticsearch.util.collect.Lists;
|
||||||
import org.elasticsearch.util.collect.UnmodifiableIterator;
|
import org.elasticsearch.util.collect.UnmodifiableIterator;
|
||||||
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
|
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
|
@ -97,7 +98,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ShardRouting> backupsShards() {
|
public List<ShardRouting> backupsShards() {
|
||||||
List<ShardRouting> backupShards = newArrayListWithExpectedSize(2);
|
List<ShardRouting> backupShards = Lists.newArrayListWithCapacity(2);
|
||||||
for (ShardRouting shardRouting : this) {
|
for (ShardRouting shardRouting : this) {
|
||||||
if (!shardRouting.primary()) {
|
if (!shardRouting.primary()) {
|
||||||
backupShards.add(shardRouting);
|
backupShards.add(shardRouting);
|
||||||
|
|
|
@ -29,8 +29,6 @@ import java.util.Comparator;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.elasticsearch.util.collect.Lists.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kimchy (shay.banon)
|
* @author kimchy (shay.banon)
|
||||||
*/
|
*/
|
||||||
|
@ -50,7 +48,7 @@ public class ElectMasterService extends AbstractComponent {
|
||||||
if (sortedNodes == null) {
|
if (sortedNodes == null) {
|
||||||
return new DiscoveryNode[0];
|
return new DiscoveryNode[0];
|
||||||
}
|
}
|
||||||
List<DiscoveryNode> nextPossibleMasters = newArrayListWithExpectedSize(numberOfPossibleMasters);
|
List<DiscoveryNode> nextPossibleMasters = Lists.newArrayListWithCapacity(numberOfPossibleMasters);
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for (DiscoveryNode nextPossibleMaster : sortedNodes) {
|
for (DiscoveryNode nextPossibleMaster : sortedNodes) {
|
||||||
if (++counter >= numberOfPossibleMasters) {
|
if (++counter >= numberOfPossibleMasters) {
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.elasticsearch.threadpool.ThreadPool;
|
||||||
import org.elasticsearch.util.SizeUnit;
|
import org.elasticsearch.util.SizeUnit;
|
||||||
import org.elasticsearch.util.SizeValue;
|
import org.elasticsearch.util.SizeValue;
|
||||||
import org.elasticsearch.util.TimeValue;
|
import org.elasticsearch.util.TimeValue;
|
||||||
|
import org.elasticsearch.util.collect.Lists;
|
||||||
import org.elasticsearch.util.inject.Inject;
|
import org.elasticsearch.util.inject.Inject;
|
||||||
import org.elasticsearch.util.io.stream.DataInputStreamInput;
|
import org.elasticsearch.util.io.stream.DataInputStreamInput;
|
||||||
import org.elasticsearch.util.io.stream.DataOutputStreamOutput;
|
import org.elasticsearch.util.io.stream.DataOutputStreamOutput;
|
||||||
|
@ -54,7 +55,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import static org.elasticsearch.index.translog.TranslogStreams.*;
|
import static org.elasticsearch.index.translog.TranslogStreams.*;
|
||||||
import static org.elasticsearch.util.collect.Lists.*;
|
|
||||||
import static org.elasticsearch.util.io.FileSystemUtils.*;
|
import static org.elasticsearch.util.io.FileSystemUtils.*;
|
||||||
import static org.elasticsearch.util.lucene.Directories.*;
|
import static org.elasticsearch.util.lucene.Directories.*;
|
||||||
|
|
||||||
|
@ -377,7 +377,7 @@ public class FsIndexShardGateway extends AbstractIndexShardComponent implements
|
||||||
File recoveryTranslogFile = new File(locationTranslog, "translog-" + recoveryTranslogId);
|
File recoveryTranslogFile = new File(locationTranslog, "translog-" + recoveryTranslogId);
|
||||||
raf = new RandomAccessFile(recoveryTranslogFile, "r");
|
raf = new RandomAccessFile(recoveryTranslogFile, "r");
|
||||||
int numberOfOperations = raf.readInt();
|
int numberOfOperations = raf.readInt();
|
||||||
ArrayList<Translog.Operation> operations = newArrayListWithExpectedSize(numberOfOperations);
|
ArrayList<Translog.Operation> operations = Lists.newArrayListWithCapacity(numberOfOperations);
|
||||||
for (int i = 0; i < numberOfOperations; i++) {
|
for (int i = 0; i < numberOfOperations; i++) {
|
||||||
operations.add(readTranslogOperation(new DataInputStreamInput(raf)));
|
operations.add(readTranslogOperation(new DataInputStreamInput(raf)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,8 @@ import org.elasticsearch.search.SearchShardTarget;
|
||||||
import org.elasticsearch.search.dfs.AggregatedDfs;
|
import org.elasticsearch.search.dfs.AggregatedDfs;
|
||||||
import org.elasticsearch.search.dfs.DfsSearchResult;
|
import org.elasticsearch.search.dfs.DfsSearchResult;
|
||||||
import org.elasticsearch.search.facets.Facet;
|
import org.elasticsearch.search.facets.Facet;
|
||||||
import org.elasticsearch.search.facets.Facets;
|
import org.elasticsearch.search.facets.internal.InternalFacet;
|
||||||
import org.elasticsearch.search.facets.InternalFacet;
|
import org.elasticsearch.search.facets.internal.InternalFacets;
|
||||||
import org.elasticsearch.search.fetch.FetchSearchResult;
|
import org.elasticsearch.search.fetch.FetchSearchResult;
|
||||||
import org.elasticsearch.search.fetch.FetchSearchResultProvider;
|
import org.elasticsearch.search.fetch.FetchSearchResultProvider;
|
||||||
import org.elasticsearch.search.internal.InternalSearchHit;
|
import org.elasticsearch.search.internal.InternalSearchHit;
|
||||||
|
@ -148,7 +148,7 @@ public class SearchPhaseController {
|
||||||
|
|
||||||
public InternalSearchResponse merge(ShardDoc[] sortedDocs, Map<SearchShardTarget, ? extends QuerySearchResultProvider> queryResults, Map<SearchShardTarget, ? extends FetchSearchResultProvider> fetchResults) {
|
public InternalSearchResponse merge(ShardDoc[] sortedDocs, Map<SearchShardTarget, ? extends QuerySearchResultProvider> queryResults, Map<SearchShardTarget, ? extends FetchSearchResultProvider> fetchResults) {
|
||||||
// merge facets
|
// merge facets
|
||||||
Facets facets = null;
|
InternalFacets facets = null;
|
||||||
if (!queryResults.isEmpty()) {
|
if (!queryResults.isEmpty()) {
|
||||||
// we rely on the fact that the order of facets is the same on all query results
|
// we rely on the fact that the order of facets is the same on all query results
|
||||||
QuerySearchResult queryResult = queryResults.values().iterator().next().queryResult();
|
QuerySearchResult queryResult = queryResults.values().iterator().next().queryResult();
|
||||||
|
@ -163,7 +163,7 @@ public class SearchPhaseController {
|
||||||
for (Facet facet : queryResult.facets().facets()) {
|
for (Facet facet : queryResult.facets().facets()) {
|
||||||
mergedFacets.add(((InternalFacet) facet).aggregate(allFacets));
|
mergedFacets.add(((InternalFacet) facet).aggregate(allFacets));
|
||||||
}
|
}
|
||||||
facets = new Facets(mergedFacets);
|
facets = new InternalFacets(mergedFacets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,9 @@
|
||||||
package org.elasticsearch.search.facets;
|
package org.elasticsearch.search.facets;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
||||||
|
import org.elasticsearch.search.facets.query.QueryFacet;
|
||||||
|
import org.elasticsearch.search.facets.statistical.StatisticalFacet;
|
||||||
|
import org.elasticsearch.search.facets.terms.TermsFacet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A search facet.
|
* A search facet.
|
||||||
|
@ -28,21 +31,50 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
||||||
*/
|
*/
|
||||||
public interface Facet {
|
public interface Facet {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of the facet.
|
||||||
|
*/
|
||||||
enum Type {
|
enum Type {
|
||||||
TERMS(0),
|
/**
|
||||||
QUERY(1),
|
* Terms facet type, matching {@link TermsFacet}.
|
||||||
STATISTICAL(2);
|
*/
|
||||||
|
TERMS(0, TermsFacet.class),
|
||||||
|
/**
|
||||||
|
* Query facet type, matching {@link QueryFacet}.
|
||||||
|
*/
|
||||||
|
QUERY(1, QueryFacet.class),
|
||||||
|
/**
|
||||||
|
* Statistical facet type, matching {@link StatisticalFacet}.
|
||||||
|
*/
|
||||||
|
STATISTICAL(2, StatisticalFacet.class);
|
||||||
|
|
||||||
int id;
|
private int id;
|
||||||
|
|
||||||
Type(int id) {
|
private Class<? extends Facet> type;
|
||||||
|
|
||||||
|
Type(int id, Class<? extends Facet> type) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The facet class type.
|
||||||
|
*/
|
||||||
|
public Class<? extends Facet> type() {
|
||||||
|
return this.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The facet class type.
|
||||||
|
*/
|
||||||
|
public Class<? extends Facet> getType() {
|
||||||
|
return type();
|
||||||
|
}
|
||||||
|
|
||||||
public static Type fromId(int id) {
|
public static Type fromId(int id) {
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
return TERMS;
|
return TERMS;
|
||||||
|
|
|
@ -19,138 +19,38 @@
|
||||||
|
|
||||||
package org.elasticsearch.search.facets;
|
package org.elasticsearch.search.facets;
|
||||||
|
|
||||||
import org.elasticsearch.search.facets.query.InternalQueryFacet;
|
|
||||||
import org.elasticsearch.search.facets.statistical.InternalStatisticalFacet;
|
|
||||||
import org.elasticsearch.search.facets.terms.InternalTermsFacet;
|
|
||||||
import org.elasticsearch.util.collect.ImmutableList;
|
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
|
||||||
import org.elasticsearch.util.io.stream.StreamOutput;
|
|
||||||
import org.elasticsearch.util.io.stream.Streamable;
|
|
||||||
import org.elasticsearch.util.xcontent.ToXContent;
|
|
||||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.elasticsearch.util.collect.Lists.*;
|
|
||||||
import static org.elasticsearch.util.collect.Maps.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Facets of search action.
|
* Facets of search action.
|
||||||
*
|
*
|
||||||
* @author kimchy (shay.banon)
|
* @author kimchy (shay.banon)
|
||||||
*/
|
*/
|
||||||
public class Facets implements Streamable, ToXContent, Iterable<Facet> {
|
public interface Facets extends Iterable<Facet> {
|
||||||
|
|
||||||
private final List<Facet> EMPTY = ImmutableList.of();
|
|
||||||
|
|
||||||
private List<Facet> facets = EMPTY;
|
|
||||||
|
|
||||||
private Map<String, Facet> facetsAsMap;
|
|
||||||
|
|
||||||
private Facets() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new facets.
|
|
||||||
*/
|
|
||||||
public Facets(List<Facet> facets) {
|
|
||||||
this.facets = facets;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Iterates over the {@link Facet}s.
|
|
||||||
*/
|
|
||||||
@Override public Iterator<Facet> iterator() {
|
|
||||||
return facets.iterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of {@link Facet}s.
|
* The list of {@link Facet}s.
|
||||||
*/
|
*/
|
||||||
public List<Facet> facets() {
|
List<Facet> facets();
|
||||||
return facets;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link Facet}s keyed by map.
|
* Returns the {@link Facet}s keyed by facet name.
|
||||||
*/
|
*/
|
||||||
public Map<String, Facet> getFacets() {
|
Map<String, Facet> getFacets();
|
||||||
return facetsAsMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link Facet}s keyed by map.
|
* Returns the {@link Facet}s keyed by facet name.
|
||||||
*/
|
*/
|
||||||
public Map<String, Facet> facetsAsMap() {
|
Map<String, Facet> facetsAsMap();
|
||||||
if (facetsAsMap != null) {
|
|
||||||
return facetsAsMap;
|
|
||||||
}
|
|
||||||
Map<String, Facet> 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.
|
* Returns the facet by name already casted to the specified type.
|
||||||
*/
|
*/
|
||||||
public <T extends Facet> T facet(Class<T> facetType, String name) {
|
<T extends Facet> T facet(Class<T> facetType, String name);
|
||||||
return facetType.cast(facet(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A facet of the specified name.
|
* A facet of the specified name.
|
||||||
*/
|
*/
|
||||||
public Facet facet(String name) {
|
Facet facet(String name);
|
||||||
return facetsAsMap().get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
||||||
builder.startObject("facets");
|
|
||||||
for (Facet facet : facets) {
|
|
||||||
((InternalFacet) facet).toXContent(builder, params);
|
|
||||||
}
|
|
||||||
builder.endObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Facets readFacets(StreamInput in) throws IOException {
|
|
||||||
Facets result = new Facets();
|
|
||||||
result.readFrom(in);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public void readFrom(StreamInput in) throws IOException {
|
|
||||||
int size = in.readVInt();
|
|
||||||
if (size == 0) {
|
|
||||||
facets = EMPTY;
|
|
||||||
} else {
|
|
||||||
facets = 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 {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.elasticsearch.index.mapper.DocumentMapper;
|
||||||
import org.elasticsearch.search.SearchParseElement;
|
import org.elasticsearch.search.SearchParseElement;
|
||||||
import org.elasticsearch.search.SearchPhase;
|
import org.elasticsearch.search.SearchPhase;
|
||||||
import org.elasticsearch.search.facets.collector.FacetCollector;
|
import org.elasticsearch.search.facets.collector.FacetCollector;
|
||||||
|
import org.elasticsearch.search.facets.internal.InternalFacets;
|
||||||
import org.elasticsearch.search.internal.SearchContext;
|
import org.elasticsearch.search.internal.SearchContext;
|
||||||
import org.elasticsearch.search.query.QueryPhaseExecutionException;
|
import org.elasticsearch.search.query.QueryPhaseExecutionException;
|
||||||
import org.elasticsearch.util.collect.ImmutableMap;
|
import org.elasticsearch.util.collect.ImmutableMap;
|
||||||
|
@ -98,6 +99,6 @@ public class FacetsPhase implements SearchPhase {
|
||||||
facets.add(facetCollector.facet());
|
facets.add(facetCollector.facet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
context.queryResult().facets(new Facets(facets));
|
context.queryResult().facets(new InternalFacets(facets));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.elasticsearch.search.facets;
|
package org.elasticsearch.search.facets.internal;
|
||||||
|
|
||||||
|
import org.elasticsearch.search.facets.Facet;
|
||||||
import org.elasticsearch.util.io.stream.Streamable;
|
import org.elasticsearch.util.io.stream.Streamable;
|
||||||
import org.elasticsearch.util.xcontent.ToXContent;
|
import org.elasticsearch.util.xcontent.ToXContent;
|
||||||
|
|
|
@ -0,0 +1,157 @@
|
||||||
|
/*
|
||||||
|
* 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.internal;
|
||||||
|
|
||||||
|
import org.elasticsearch.search.facets.Facet;
|
||||||
|
import org.elasticsearch.search.facets.Facets;
|
||||||
|
import org.elasticsearch.search.facets.query.InternalQueryFacet;
|
||||||
|
import org.elasticsearch.search.facets.statistical.InternalStatisticalFacet;
|
||||||
|
import org.elasticsearch.search.facets.terms.InternalTermsFacet;
|
||||||
|
import org.elasticsearch.util.collect.ImmutableList;
|
||||||
|
import org.elasticsearch.util.collect.ImmutableMap;
|
||||||
|
import org.elasticsearch.util.collect.Lists;
|
||||||
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
|
import org.elasticsearch.util.io.stream.StreamOutput;
|
||||||
|
import org.elasticsearch.util.io.stream.Streamable;
|
||||||
|
import org.elasticsearch.util.xcontent.ToXContent;
|
||||||
|
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.elasticsearch.util.collect.Maps.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kimchy (shay.banon)
|
||||||
|
*/
|
||||||
|
public class InternalFacets implements Facets, Streamable, ToXContent, Iterable<Facet> {
|
||||||
|
|
||||||
|
private List<Facet> facets = ImmutableList.of();
|
||||||
|
|
||||||
|
private Map<String, Facet> facetsAsMap;
|
||||||
|
|
||||||
|
private InternalFacets() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new facets.
|
||||||
|
*/
|
||||||
|
public InternalFacets(List<Facet> facets) {
|
||||||
|
this.facets = facets;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterates over the {@link Facet}s.
|
||||||
|
*/
|
||||||
|
@Override public Iterator<Facet> iterator() {
|
||||||
|
return facets.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of {@link Facet}s.
|
||||||
|
*/
|
||||||
|
public List<Facet> facets() {
|
||||||
|
return facets;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link Facet}s keyed by map.
|
||||||
|
*/
|
||||||
|
public Map<String, Facet> getFacets() {
|
||||||
|
return facetsAsMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link Facet}s keyed by map.
|
||||||
|
*/
|
||||||
|
public Map<String, Facet> facetsAsMap() {
|
||||||
|
if (facetsAsMap != null) {
|
||||||
|
return facetsAsMap;
|
||||||
|
}
|
||||||
|
Map<String, Facet> 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.
|
||||||
|
*/
|
||||||
|
public <T extends Facet> T facet(Class<T> facetType, String name) {
|
||||||
|
return facetType.cast(facet(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A facet of the specified name.
|
||||||
|
*/
|
||||||
|
public Facet facet(String name) {
|
||||||
|
return facetsAsMap().get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
|
builder.startObject("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 {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package org.elasticsearch.search.facets.query;
|
package org.elasticsearch.search.facets.query;
|
||||||
|
|
||||||
import org.elasticsearch.search.facets.Facet;
|
import org.elasticsearch.search.facets.Facet;
|
||||||
import org.elasticsearch.search.facets.InternalFacet;
|
import org.elasticsearch.search.facets.internal.InternalFacet;
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
import org.elasticsearch.util.io.stream.StreamOutput;
|
import org.elasticsearch.util.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package org.elasticsearch.search.facets.statistical;
|
package org.elasticsearch.search.facets.statistical;
|
||||||
|
|
||||||
import org.elasticsearch.search.facets.Facet;
|
import org.elasticsearch.search.facets.Facet;
|
||||||
import org.elasticsearch.search.facets.InternalFacet;
|
import org.elasticsearch.search.facets.internal.InternalFacet;
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
import org.elasticsearch.util.io.stream.StreamOutput;
|
import org.elasticsearch.util.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package org.elasticsearch.search.facets.terms;
|
package org.elasticsearch.search.facets.terms;
|
||||||
|
|
||||||
import org.elasticsearch.search.facets.Facet;
|
import org.elasticsearch.search.facets.Facet;
|
||||||
import org.elasticsearch.search.facets.InternalFacet;
|
import org.elasticsearch.search.facets.internal.InternalFacet;
|
||||||
import org.elasticsearch.util.BoundedTreeSet;
|
import org.elasticsearch.util.BoundedTreeSet;
|
||||||
import org.elasticsearch.util.ThreadLocals;
|
import org.elasticsearch.util.ThreadLocals;
|
||||||
import org.elasticsearch.util.collect.ImmutableList;
|
import org.elasticsearch.util.collect.ImmutableList;
|
||||||
|
|
|
@ -21,6 +21,7 @@ package org.elasticsearch.search.internal;
|
||||||
|
|
||||||
import org.elasticsearch.search.SearchHits;
|
import org.elasticsearch.search.SearchHits;
|
||||||
import org.elasticsearch.search.facets.Facets;
|
import org.elasticsearch.search.facets.Facets;
|
||||||
|
import org.elasticsearch.search.facets.internal.InternalFacets;
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
import org.elasticsearch.util.io.stream.StreamOutput;
|
import org.elasticsearch.util.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.util.io.stream.Streamable;
|
import org.elasticsearch.util.io.stream.Streamable;
|
||||||
|
@ -29,7 +30,6 @@ import org.elasticsearch.util.xcontent.builder.XContentBuilder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.elasticsearch.search.facets.Facets.*;
|
|
||||||
import static org.elasticsearch.search.internal.InternalSearchHits.*;
|
import static org.elasticsearch.search.internal.InternalSearchHits.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,12 +39,12 @@ public class InternalSearchResponse implements Streamable, ToXContent {
|
||||||
|
|
||||||
private InternalSearchHits hits;
|
private InternalSearchHits hits;
|
||||||
|
|
||||||
private Facets facets;
|
private InternalFacets facets;
|
||||||
|
|
||||||
private InternalSearchResponse() {
|
private InternalSearchResponse() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public InternalSearchResponse(InternalSearchHits hits, Facets facets) {
|
public InternalSearchResponse(InternalSearchHits hits, InternalFacets facets) {
|
||||||
this.hits = hits;
|
this.hits = hits;
|
||||||
this.facets = facets;
|
this.facets = facets;
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ public class InternalSearchResponse implements Streamable, ToXContent {
|
||||||
@Override public void readFrom(StreamInput in) throws IOException {
|
@Override public void readFrom(StreamInput in) throws IOException {
|
||||||
hits = readSearchHits(in);
|
hits = readSearchHits(in);
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
facets = readFacets(in);
|
facets = InternalFacets.readFacets(in);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.search.query;
|
||||||
import org.apache.lucene.search.TopDocs;
|
import org.apache.lucene.search.TopDocs;
|
||||||
import org.elasticsearch.search.SearchShardTarget;
|
import org.elasticsearch.search.SearchShardTarget;
|
||||||
import org.elasticsearch.search.facets.Facets;
|
import org.elasticsearch.search.facets.Facets;
|
||||||
|
import org.elasticsearch.search.facets.internal.InternalFacets;
|
||||||
import org.elasticsearch.util.io.stream.StreamInput;
|
import org.elasticsearch.util.io.stream.StreamInput;
|
||||||
import org.elasticsearch.util.io.stream.StreamOutput;
|
import org.elasticsearch.util.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.util.io.stream.Streamable;
|
import org.elasticsearch.util.io.stream.Streamable;
|
||||||
|
@ -29,7 +30,6 @@ import org.elasticsearch.util.io.stream.Streamable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.elasticsearch.search.SearchShardTarget.*;
|
import static org.elasticsearch.search.SearchShardTarget.*;
|
||||||
import static org.elasticsearch.search.facets.Facets.*;
|
|
||||||
import static org.elasticsearch.util.lucene.Lucene.*;
|
import static org.elasticsearch.util.lucene.Lucene.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,7 +47,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
|
||||||
|
|
||||||
private TopDocs topDocs;
|
private TopDocs topDocs;
|
||||||
|
|
||||||
private Facets facets;
|
private InternalFacets facets;
|
||||||
|
|
||||||
private boolean searchTimedOut;
|
private boolean searchTimedOut;
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
|
||||||
return facets;
|
return facets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void facets(Facets facets) {
|
public void facets(InternalFacets facets) {
|
||||||
this.facets = facets;
|
this.facets = facets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
|
||||||
size = in.readVInt();
|
size = in.readVInt();
|
||||||
topDocs = readTopDocs(in);
|
topDocs = readTopDocs(in);
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
facets = readFacets(in);
|
facets = InternalFacets.readFacets(in);
|
||||||
}
|
}
|
||||||
searchTimedOut = in.readBoolean();
|
searchTimedOut = in.readBoolean();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue