mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-23 21:38:15 +00:00
refactor facets builder API to make it more manageable
This commit is contained in:
parent
5df2257319
commit
00d2abef40
@ -28,15 +28,12 @@ import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.client.action.support.BaseRequestBuilder;
|
||||
import org.elasticsearch.client.internal.InternalClient;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceFacetsBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceHighlightBuilder;
|
||||
import org.elasticsearch.search.facets.histogram.HistogramFacet;
|
||||
import org.elasticsearch.search.facets.AbstractFacetBuilder;
|
||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -48,9 +45,7 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
||||
|
||||
private SearchSourceBuilder sourceBuilder;
|
||||
|
||||
private SearchSourceFacetsBuilder facetsBuilder;
|
||||
|
||||
private SearchSourceHighlightBuilder highlightBuilder;
|
||||
private HighlightBuilder highlightBuilder;
|
||||
|
||||
public SearchRequestBuilder(InternalClient client) {
|
||||
super(client, new SearchRequest());
|
||||
@ -258,261 +253,10 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a query facet (which results in a count facet returned).
|
||||
*
|
||||
* @param name The logical name of the facet, it will be returned under the name
|
||||
* @param query The query facet
|
||||
* @see org.elasticsearch.search.facets.query.QueryFacet
|
||||
* Adds a facet to the search operation.
|
||||
*/
|
||||
public SearchRequestBuilder addFacetQuery(String name, XContentQueryBuilder query) {
|
||||
facetsBuilder().queryFacet(name, query);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetQuery(String name, XContentQueryBuilder query, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().queryFacet(name, query, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a query facet (which results in a count facet returned) with an option to
|
||||
* be global on the index or bounded by the search query.
|
||||
*
|
||||
* @param name The logical name of the facet, it will be returned under the name
|
||||
* @param query The query facet
|
||||
* @see org.elasticsearch.search.facets.query.QueryFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetGlobalQuery(String name, XContentQueryBuilder query) {
|
||||
facetsBuilder().queryFacetGlobal(name, query);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalQuery(String name, XContentQueryBuilder query, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().queryFacetGlobal(name, query, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a term facet for the provided field name.
|
||||
*
|
||||
* @param name The name of the facet
|
||||
* @param fieldName The field name to run the facet against
|
||||
* @param size The number of the terms
|
||||
* @see org.elasticsearch.search.facets.terms.TermsFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetTerms(String name, String fieldName, int size) {
|
||||
facetsBuilder().termsFacet(name, fieldName, size);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetTerms(String name, String fieldName, int size, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().termsFacet(name, fieldName, size, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a <b>global</b> term facet for the provided field name.
|
||||
*
|
||||
* @param name The name of the facet
|
||||
* @param fieldName The field name to run the facet against
|
||||
* @param size The number of the terms
|
||||
* @see org.elasticsearch.search.facets.terms.TermsFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetGlobalTerms(String name, String fieldName, int size) {
|
||||
facetsBuilder().termsFacetGlobal(name, fieldName, size);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalTerms(String name, String fieldName, int size, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().termsFacetGlobal(name, fieldName, size, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a numeric statistical facet for the provided field name.
|
||||
*
|
||||
* @param name The name of the facet
|
||||
* @param fieldName The name of the <b>numeric</b> field
|
||||
* @see org.elasticsearch.search.facets.statistical.StatisticalFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetStatistical(String name, String fieldName) {
|
||||
facetsBuilder().statisticalFacet(name, fieldName);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a numeric statistical facet for the provided field name.
|
||||
*
|
||||
* @param name The name of the facet
|
||||
* @param fieldName The name of the <b>numeric</b> field
|
||||
* @param filter An optional filter to reduce the scope of the facet
|
||||
* @see org.elasticsearch.search.facets.statistical.StatisticalFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetStatistical(String name, String fieldName, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().statisticalFacet(name, fieldName, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetStatisticalScript(String name, String script) {
|
||||
facetsBuilder().statisticalScriptFacet(name, script);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetStatisticalScript(String name, String script, @Nullable Map<String, Object> params) {
|
||||
facetsBuilder().statisticalScriptFacet(name, script, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetStatisticalScript(String name, String script, @Nullable Map<String, Object> params, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().statisticalScriptFacet(name, script, params, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalStatisticalScript(String name, String script) {
|
||||
facetsBuilder().statisticalScriptFacetGlobal(name, script);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalStatisticalScript(String name, String script, @Nullable Map<String, Object> params) {
|
||||
facetsBuilder().statisticalScriptFacetGlobal(name, script, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalStatisticalScript(String name, String script, @Nullable Map<String, Object> params, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().statisticalScriptFacetGlobal(name, script, params, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a numeric statistical <b>global</b> facet for the provided field name.
|
||||
*
|
||||
* @param name The name of the facet
|
||||
* @param fieldName The name of the <b>numeric</b> field
|
||||
* @see org.elasticsearch.search.facets.statistical.StatisticalFacet
|
||||
*/
|
||||
public SearchRequestBuilder addFacetGlobalStatistical(String name, String fieldName) {
|
||||
facetsBuilder().statisticalFacetGlobal(name, fieldName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetGlobalStatistical(String name, String fieldName, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().statisticalFacetGlobal(name, fieldName, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String fieldName, long interval) {
|
||||
facetsBuilder().histogramFacet(name, fieldName, interval);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String keyFieldName, String valueFieldName, long interval) {
|
||||
facetsBuilder().histogramFacet(name, keyFieldName, valueFieldName, interval);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String fieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacet(name, fieldName, interval, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String keyFieldName, String valueFieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacet(name, keyFieldName, valueFieldName, interval, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramFacet(name, fieldName, interval, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacet(name, fieldName, interval, comparatorType, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogram(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String fieldName, long interval) {
|
||||
facetsBuilder().histogramFacetGlobal(name, fieldName, interval);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String keyFieldName, String valueFieldName, long interval) {
|
||||
facetsBuilder().histogramFacetGlobal(name, keyFieldName, valueFieldName, interval);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String fieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacetGlobal(name, fieldName, interval, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String keyFieldName, String valueFieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacetGlobal(name, keyFieldName, valueFieldName, interval, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramFacetGlobal(name, fieldName, interval, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramFacetGlobal(name, keyFieldName, valueFieldName, interval, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacetGlobal(name, fieldName, interval, comparatorType, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramGlobal(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramFacetGlobal(name, keyFieldName, valueFieldName, interval, comparatorType, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScript(String name, String keyScript, String valueScript) {
|
||||
facetsBuilder().histogramScriptFacet(name, keyScript, valueScript);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScript(String name, String keyScript, String valueScript, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramScriptFacet(name, keyScript, valueScript, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScript(String name, String keyScript, String valueScript, @Nullable Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramScriptFacet(name, keyScript, valueScript, params, interval, comparatorType, filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScriptGlobal(String name, String keyScript, String valueScript) {
|
||||
facetsBuilder().histogramScriptFacetGlobal(name, keyScript, valueScript);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScriptGlobal(String name, String keyScript, String valueScript, HistogramFacet.ComparatorType comparatorType) {
|
||||
facetsBuilder().histogramScriptFacetGlobal(name, keyScript, valueScript, comparatorType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchRequestBuilder addFacetHistogramScriptGlobal(String name, String keyScript, String valueScript, @Nullable Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
facetsBuilder().histogramScriptFacetGlobal(name, keyScript, valueScript, params, interval, comparatorType, filter);
|
||||
public SearchRequestBuilder addFacet(AbstractFacetBuilder facet) {
|
||||
sourceBuilder().facet(facet);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -590,9 +334,6 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
||||
}
|
||||
|
||||
@Override protected void doExecute(ActionListener<SearchResponse> listener) {
|
||||
if (facetsBuilder != null) {
|
||||
sourceBuilder().facets(facetsBuilder);
|
||||
}
|
||||
if (highlightBuilder != null) {
|
||||
sourceBuilder().highlight(highlightBuilder);
|
||||
}
|
||||
@ -607,16 +348,9 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
||||
return sourceBuilder;
|
||||
}
|
||||
|
||||
private SearchSourceFacetsBuilder facetsBuilder() {
|
||||
if (facetsBuilder == null) {
|
||||
facetsBuilder = new SearchSourceFacetsBuilder();
|
||||
}
|
||||
return facetsBuilder;
|
||||
}
|
||||
|
||||
private SearchSourceHighlightBuilder highlightBuilder() {
|
||||
private HighlightBuilder highlightBuilder() {
|
||||
if (highlightBuilder == null) {
|
||||
highlightBuilder = new SearchSourceHighlightBuilder();
|
||||
highlightBuilder = new HighlightBuilder();
|
||||
}
|
||||
return highlightBuilder;
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.common.xcontent.builder.BinaryXContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
|
||||
import org.elasticsearch.search.facets.AbstractFacetBuilder;
|
||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@ -58,18 +60,11 @@ public class SearchSourceBuilder implements ToXContent {
|
||||
return new SearchSourceBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* A static factory method to construct new search facets.
|
||||
*/
|
||||
public static SearchSourceFacetsBuilder facets() {
|
||||
return new SearchSourceFacetsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* A static factory method to construct new search highlights.
|
||||
*/
|
||||
public static SearchSourceHighlightBuilder highlight() {
|
||||
return new SearchSourceHighlightBuilder();
|
||||
public static HighlightBuilder highlight() {
|
||||
return new HighlightBuilder();
|
||||
}
|
||||
|
||||
private XContentQueryBuilder queryBuilder;
|
||||
@ -88,9 +83,9 @@ public class SearchSourceBuilder implements ToXContent {
|
||||
|
||||
private List<ScriptField> scriptFields;
|
||||
|
||||
private SearchSourceFacetsBuilder facetsBuilder;
|
||||
private List<AbstractFacetBuilder> facets;
|
||||
|
||||
private SearchSourceHighlightBuilder highlightBuilder;
|
||||
private HighlightBuilder highlightBuilder;
|
||||
|
||||
private TObjectFloatHashMap<String> indexBoost = null;
|
||||
|
||||
@ -209,17 +204,20 @@ public class SearchSourceBuilder implements ToXContent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds facets to perform as part of the search.
|
||||
* Add a facet to perform as part of the search.
|
||||
*/
|
||||
public SearchSourceBuilder facets(SearchSourceFacetsBuilder facetsBuilder) {
|
||||
this.facetsBuilder = facetsBuilder;
|
||||
public SearchSourceBuilder facet(AbstractFacetBuilder facet) {
|
||||
if (facets == null) {
|
||||
facets = Lists.newArrayList();
|
||||
}
|
||||
facets.add(facet);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds highlight to perform as part of the search.
|
||||
*/
|
||||
public SearchSourceBuilder highlight(SearchSourceHighlightBuilder highlightBuilder) {
|
||||
public SearchSourceBuilder highlight(HighlightBuilder highlightBuilder) {
|
||||
this.highlightBuilder = highlightBuilder;
|
||||
return this;
|
||||
}
|
||||
@ -388,8 +386,13 @@ public class SearchSourceBuilder implements ToXContent {
|
||||
builder.endObject();
|
||||
}
|
||||
|
||||
if (facetsBuilder != null) {
|
||||
facetsBuilder.toXContent(builder, params);
|
||||
if (facets != null) {
|
||||
builder.field("facets");
|
||||
builder.startObject();
|
||||
for (AbstractFacetBuilder facet : facets) {
|
||||
facet.toXContent(builder, params);
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
|
||||
if (highlightBuilder != null) {
|
||||
|
@ -1,673 +0,0 @@
|
||||
/*
|
||||
* 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.builder;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
|
||||
import org.elasticsearch.search.facets.histogram.HistogramFacet;
|
||||
import org.elasticsearch.search.facets.histogram.HistogramFacetCollectorParser;
|
||||
import org.elasticsearch.search.facets.query.QueryFacetCollectorParser;
|
||||
import org.elasticsearch.search.facets.statistical.StatisticalFacetCollectorParser;
|
||||
import org.elasticsearch.search.facets.terms.TermsFacetCollectorParser;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.common.collect.Lists.*;
|
||||
|
||||
/**
|
||||
* A search source facets builder.
|
||||
*
|
||||
* @author kimchy (shay.banon)
|
||||
* @see SearchSourceBuilder#facets(SearchSourceFacetsBuilder)
|
||||
*/
|
||||
public class SearchSourceFacetsBuilder implements ToXContent {
|
||||
|
||||
private List<BuilderQueryFacet> queryFacets;
|
||||
private List<BuilderTermsFacet> termsFacets;
|
||||
private List<BuilderStatisticalFacet> statisticalFacets;
|
||||
private List<BuilderScriptStatisticalFacet> scriptStatisticalFacets;
|
||||
private List<BuilderHistogramFacet> histogramFacets;
|
||||
private List<BuilderScriptHistogramFacet> scriptHistogramFacets;
|
||||
|
||||
public SearchSourceFacetsBuilder queryFacet(String name, XContentQueryBuilder query) {
|
||||
return queryFacet(name, query, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a query facet (which results in a count facet returned).
|
||||
*
|
||||
* @param name The logical name of the facet, it will be returned under the name
|
||||
* @param query The query facet
|
||||
*/
|
||||
public SearchSourceFacetsBuilder queryFacet(String name, XContentQueryBuilder query, @Nullable XContentFilterBuilder filter) {
|
||||
if (queryFacets == null) {
|
||||
queryFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
queryFacets.add(new BuilderQueryFacet(name, query, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder queryFacetGlobal(String name, XContentQueryBuilder query) {
|
||||
return queryFacetGlobal(name, query, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a query facet (which results in a count facet returned) with an option to
|
||||
* be global on the index or bounded by the search query.
|
||||
*
|
||||
* @param name The logical name of the facet, it will be returned under the name
|
||||
* @param query The query facet
|
||||
*/
|
||||
public SearchSourceFacetsBuilder queryFacetGlobal(String name, XContentQueryBuilder query, @Nullable XContentFilterBuilder filter) {
|
||||
if (queryFacets == null) {
|
||||
queryFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
queryFacets.add(new BuilderQueryFacet(name, query, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder termsFacet(String name, String fieldName, int size) {
|
||||
return termsFacet(name, fieldName, size, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder termsFacet(String name, String fieldName, int size, @Nullable XContentFilterBuilder filter) {
|
||||
if (termsFacets == null) {
|
||||
termsFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
termsFacets.add(new BuilderTermsFacet(name, fieldName, size, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder termsFacetGlobal(String name, String fieldName, int size) {
|
||||
return termsFacetGlobal(name, fieldName, size, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder termsFacetGlobal(String name, String fieldName, int size, @Nullable XContentFilterBuilder filter) {
|
||||
if (termsFacets == null) {
|
||||
termsFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
termsFacets.add(new BuilderTermsFacet(name, fieldName, size, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalFacet(String name, String fieldName) {
|
||||
return statisticalFacet(name, fieldName, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalFacet(String name, String fieldName, @Nullable XContentFilterBuilder filter) {
|
||||
if (statisticalFacets == null) {
|
||||
statisticalFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
statisticalFacets.add(new BuilderStatisticalFacet(name, fieldName, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalFacetGlobal(String name, String fieldName) {
|
||||
return statisticalFacetGlobal(name, fieldName, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalFacetGlobal(String name, String fieldName, @Nullable XContentFilterBuilder filter) {
|
||||
if (statisticalFacets == null) {
|
||||
statisticalFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
statisticalFacets.add(new BuilderStatisticalFacet(name, fieldName, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacet(String name, String script) {
|
||||
return statisticalScriptFacet(name, script, null, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacet(String name, String script, @Nullable Map<String, Object> params) {
|
||||
return statisticalScriptFacet(name, script, params, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacet(String name, String script, @Nullable Map<String, Object> params, @Nullable XContentFilterBuilder filter) {
|
||||
if (scriptStatisticalFacets == null) {
|
||||
scriptStatisticalFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
scriptStatisticalFacets.add(new BuilderScriptStatisticalFacet(name, script, params, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacetGlobal(String name, String script) {
|
||||
return statisticalScriptFacetGlobal(name, script, null, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacetGlobal(String name, String script, @Nullable Map<String, Object> params) {
|
||||
return statisticalScriptFacetGlobal(name, script, params, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder statisticalScriptFacetGlobal(String name, String script, @Nullable Map<String, Object> params, @Nullable XContentFilterBuilder filter) {
|
||||
if (scriptStatisticalFacets == null) {
|
||||
scriptStatisticalFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
scriptStatisticalFacets.add(new BuilderScriptStatisticalFacet(name, script, params, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String fieldName, long interval) {
|
||||
return histogramFacet(name, fieldName, interval, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String keyFieldName, String valueFieldName, long interval) {
|
||||
return histogramFacet(name, keyFieldName, valueFieldName, interval, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String fieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacet(name, fieldName, interval, HistogramFacet.ComparatorType.KEY, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String keyFieldName, String valueFieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacet(name, keyFieldName, valueFieldName, interval, HistogramFacet.ComparatorType.KEY, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramFacet(name, fieldName, interval, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String keyFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacet(name, keyFieldName, null, interval, comparatorType, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacet(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
if (histogramFacets == null) {
|
||||
histogramFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
histogramFacets.add(new BuilderHistogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String fieldName, long interval) {
|
||||
return histogramFacet(name, fieldName, interval, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String keyFieldName, String valueFieldName, long interval) {
|
||||
return histogramFacet(name, keyFieldName, valueFieldName, interval, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String fieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacet(name, fieldName, interval, HistogramFacet.ComparatorType.KEY, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String keyFieldName, String valueFieldName, long interval, @Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacet(name, keyFieldName, valueFieldName, interval, HistogramFacet.ComparatorType.KEY, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramFacetGlobal(name, fieldName, interval, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramFacetGlobal(name, keyFieldName, valueFieldName, interval, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String fieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
return histogramFacetGlobal(name, fieldName, null, interval, comparatorType, filter);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramFacetGlobal(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
if (histogramFacets == null) {
|
||||
histogramFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
histogramFacets.add(new BuilderHistogramFacet(name, keyFieldName, valueFieldName, interval, comparatorType, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacet(String name, String keyScript, String valueScript) {
|
||||
return histogramScriptFacet(name, keyScript, valueScript, null, -1, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacet(String name, String keyScript, String valueScript, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramScriptFacet(name, keyScript, valueScript, null, -1, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacet(String name, String keyScript, String valueScript, @Nullable Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
if (scriptHistogramFacets == null) {
|
||||
scriptHistogramFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
scriptHistogramFacets.add(new BuilderScriptHistogramFacet(name, keyScript, valueScript, params, interval, comparatorType, filter, false));
|
||||
return this;
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacetGlobal(String name, String keyScript, String valueScript) {
|
||||
return histogramScriptFacetGlobal(name, keyScript, valueScript, null, -1, HistogramFacet.ComparatorType.KEY, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacetGlobal(String name, String keyScript, String valueScript, HistogramFacet.ComparatorType comparatorType) {
|
||||
return histogramScriptFacetGlobal(name, keyScript, valueScript, null, -1, comparatorType, null);
|
||||
}
|
||||
|
||||
public SearchSourceFacetsBuilder histogramScriptFacetGlobal(String name, String keyScript, String valueScript, @Nullable Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
@Nullable XContentFilterBuilder filter) {
|
||||
if (scriptHistogramFacets == null) {
|
||||
scriptHistogramFacets = newArrayListWithCapacity(2);
|
||||
}
|
||||
scriptHistogramFacets.add(new BuilderScriptHistogramFacet(name, keyScript, valueScript, params, interval, comparatorType, filter, true));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (queryFacets == null && termsFacets == null && statisticalFacets == null && histogramFacets == null && scriptStatisticalFacets == null && scriptHistogramFacets == null) {
|
||||
return;
|
||||
}
|
||||
builder.field("facets");
|
||||
|
||||
builder.startObject();
|
||||
|
||||
if (queryFacets != null) {
|
||||
for (BuilderQueryFacet queryFacet : queryFacets) {
|
||||
builder.startObject(queryFacet.name());
|
||||
builder.field(QueryFacetCollectorParser.NAME);
|
||||
queryFacet.queryBuilder().toXContent(builder, params);
|
||||
|
||||
if (queryFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
queryFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (queryFacet.global() != null) {
|
||||
builder.field("global", queryFacet.global());
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
if (termsFacets != null) {
|
||||
for (BuilderTermsFacet termsFacet : termsFacets) {
|
||||
builder.startObject(termsFacet.name());
|
||||
|
||||
builder.startObject(TermsFacetCollectorParser.NAME);
|
||||
builder.field("field", termsFacet.fieldName());
|
||||
builder.field("size", termsFacet.size());
|
||||
builder.endObject();
|
||||
|
||||
if (termsFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
termsFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
if (termsFacet.global() != null) {
|
||||
builder.field("global", termsFacet.global());
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
|
||||
if (statisticalFacets != null) {
|
||||
for (BuilderStatisticalFacet statisticalFacet : statisticalFacets) {
|
||||
builder.startObject(statisticalFacet.name());
|
||||
|
||||
builder.startObject(StatisticalFacetCollectorParser.NAME);
|
||||
builder.field("field", statisticalFacet.fieldName());
|
||||
builder.endObject();
|
||||
|
||||
if (statisticalFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
statisticalFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (statisticalFacet.global() != null) {
|
||||
builder.field("global", statisticalFacet.global());
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptStatisticalFacets != null) {
|
||||
for (BuilderScriptStatisticalFacet statisticalFacet : scriptStatisticalFacets) {
|
||||
builder.startObject(statisticalFacet.name());
|
||||
|
||||
builder.startObject(StatisticalFacetCollectorParser.NAME);
|
||||
builder.field("script", statisticalFacet.script());
|
||||
if (statisticalFacet.params() != null) {
|
||||
builder.field("params");
|
||||
builder.map(statisticalFacet.params());
|
||||
}
|
||||
builder.endObject();
|
||||
|
||||
if (statisticalFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
statisticalFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (statisticalFacet.global() != null) {
|
||||
builder.field("global", statisticalFacet.global());
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
|
||||
if (histogramFacets != null) {
|
||||
for (BuilderHistogramFacet histogramFacet : histogramFacets) {
|
||||
builder.startObject(histogramFacet.name());
|
||||
|
||||
builder.startObject(HistogramFacetCollectorParser.NAME);
|
||||
if (histogramFacet.valueFieldName() != null && !histogramFacet.keyFieldName().equals(histogramFacet.valueFieldName())) {
|
||||
builder.field("key_field", histogramFacet.keyFieldName());
|
||||
builder.field("value_field", histogramFacet.valueFieldName());
|
||||
} else {
|
||||
builder.field("field", histogramFacet.keyFieldName());
|
||||
}
|
||||
builder.field("interval", histogramFacet.interval());
|
||||
builder.field("comparator", histogramFacet.comparatorType().description());
|
||||
builder.endObject();
|
||||
|
||||
if (histogramFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
histogramFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (histogramFacet.global() != null) {
|
||||
builder.field("global", histogramFacet.global());
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptHistogramFacets != null) {
|
||||
for (BuilderScriptHistogramFacet histogramFacet : scriptHistogramFacets) {
|
||||
builder.startObject(histogramFacet.name());
|
||||
|
||||
builder.startObject(HistogramFacetCollectorParser.NAME);
|
||||
builder.field("key_script", histogramFacet.keyScript());
|
||||
builder.field("value_script", histogramFacet.valueScript());
|
||||
if (histogramFacet.interval() > 0) {
|
||||
builder.field("interval", histogramFacet.interval());
|
||||
}
|
||||
if (histogramFacet.params() != null) {
|
||||
builder.field("params");
|
||||
builder.map(histogramFacet.params());
|
||||
}
|
||||
builder.field("comparator", histogramFacet.comparatorType().description());
|
||||
builder.endObject();
|
||||
|
||||
if (histogramFacet.filter() != null) {
|
||||
builder.field("filter");
|
||||
histogramFacet.filter().toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (histogramFacet.global() != null) {
|
||||
builder.field("global", histogramFacet.global());
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
|
||||
private static class BuilderTermsFacet {
|
||||
private final String name;
|
||||
private final String fieldName;
|
||||
private final int size;
|
||||
private final Boolean global;
|
||||
private final XContentFilterBuilder filter;
|
||||
|
||||
private BuilderTermsFacet(String name, String fieldName, int size, XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.fieldName = fieldName;
|
||||
this.size = size;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String fieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return global;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuilderQueryFacet {
|
||||
private final String name;
|
||||
private final XContentQueryBuilder queryBuilder;
|
||||
private final XContentFilterBuilder filter;
|
||||
private final Boolean global;
|
||||
|
||||
private BuilderQueryFacet(String name, XContentQueryBuilder queryBuilder, XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.queryBuilder = queryBuilder;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public XContentQueryBuilder queryBuilder() {
|
||||
return queryBuilder;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return this.global;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuilderScriptStatisticalFacet {
|
||||
private final String name;
|
||||
private final String script;
|
||||
private final Map<String, Object> params;
|
||||
private final XContentFilterBuilder filter;
|
||||
private final Boolean global;
|
||||
|
||||
private BuilderScriptStatisticalFacet(String name, String script, Map<String, Object> params, XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.script = script;
|
||||
this.params = params;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String script() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public Map<String, Object> params() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return global;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuilderStatisticalFacet {
|
||||
private final String name;
|
||||
private final String fieldName;
|
||||
private final XContentFilterBuilder filter;
|
||||
private final Boolean global;
|
||||
|
||||
private BuilderStatisticalFacet(String name, String fieldName, XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.fieldName = fieldName;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String fieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return this.filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return this.global;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuilderScriptHistogramFacet {
|
||||
private final String name;
|
||||
private final String keyScript;
|
||||
private final String valueScript;
|
||||
private final Map<String, Object> params;
|
||||
private final long interval;
|
||||
private final HistogramFacet.ComparatorType comparatorType;
|
||||
private final XContentFilterBuilder filter;
|
||||
private final Boolean global;
|
||||
|
||||
private BuilderScriptHistogramFacet(String name, String keyScript, String valueScript, Map<String, Object> params, long interval, HistogramFacet.ComparatorType comparatorType, XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.keyScript = keyScript;
|
||||
this.valueScript = valueScript;
|
||||
this.params = params;
|
||||
this.interval = interval;
|
||||
this.comparatorType = comparatorType;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Map<String, Object> params() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public String keyScript() {
|
||||
return keyScript;
|
||||
}
|
||||
|
||||
public String valueScript() {
|
||||
return valueScript;
|
||||
}
|
||||
|
||||
public long interval() {
|
||||
return interval;
|
||||
}
|
||||
|
||||
public HistogramFacet.ComparatorType comparatorType() {
|
||||
return comparatorType;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return global;
|
||||
}
|
||||
}
|
||||
|
||||
private static class BuilderHistogramFacet {
|
||||
private final String name;
|
||||
private final String keyFieldName;
|
||||
private final String valueFieldName;
|
||||
private final long interval;
|
||||
private final HistogramFacet.ComparatorType comparatorType;
|
||||
private final XContentFilterBuilder filter;
|
||||
private final Boolean global;
|
||||
|
||||
private BuilderHistogramFacet(String name, String keyFieldName, String valueFieldName, long interval, XContentFilterBuilder filter, Boolean global) {
|
||||
this(name, keyFieldName, valueFieldName, interval, HistogramFacet.ComparatorType.KEY, filter, global);
|
||||
}
|
||||
|
||||
private BuilderHistogramFacet(String name, String keyFieldName, String valueFieldName, long interval, HistogramFacet.ComparatorType comparatorType,
|
||||
XContentFilterBuilder filter, Boolean global) {
|
||||
this.name = name;
|
||||
this.keyFieldName = keyFieldName;
|
||||
this.valueFieldName = valueFieldName;
|
||||
this.interval = interval;
|
||||
this.comparatorType = comparatorType;
|
||||
this.filter = filter;
|
||||
this.global = global;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String keyFieldName() {
|
||||
return keyFieldName;
|
||||
}
|
||||
|
||||
public String valueFieldName() {
|
||||
return valueFieldName;
|
||||
}
|
||||
|
||||
public long interval() {
|
||||
return this.interval;
|
||||
}
|
||||
|
||||
public HistogramFacet.ComparatorType comparatorType() {
|
||||
return this.comparatorType;
|
||||
}
|
||||
|
||||
public XContentFilterBuilder filter() {
|
||||
return this.filter;
|
||||
}
|
||||
|
||||
public Boolean global() {
|
||||
return this.global;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public abstract class AbstractFacetBuilder implements ToXContent {
|
||||
|
||||
protected final String name;
|
||||
|
||||
protected Boolean global;
|
||||
|
||||
protected XContentFilterBuilder filter;
|
||||
|
||||
protected AbstractFacetBuilder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public AbstractFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AbstractFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
|
||||
import org.elasticsearch.search.facets.histogram.HistogramFacetBuilder;
|
||||
import org.elasticsearch.search.facets.histogram.HistogramScriptFacetBuilder;
|
||||
import org.elasticsearch.search.facets.query.QueryFacetBuilder;
|
||||
import org.elasticsearch.search.facets.statistical.StatisticalFacetBuilder;
|
||||
import org.elasticsearch.search.facets.statistical.StatisticalScriptFacetBuilder;
|
||||
import org.elasticsearch.search.facets.terms.TermsFacetBuilder;
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class FacetBuilders {
|
||||
|
||||
public static QueryFacetBuilder queryFacet(String facetName) {
|
||||
return new QueryFacetBuilder(facetName);
|
||||
}
|
||||
|
||||
public static QueryFacetBuilder queryFacet(String facetName, XContentQueryBuilder query) {
|
||||
return new QueryFacetBuilder(facetName).query(query);
|
||||
}
|
||||
|
||||
public static TermsFacetBuilder termsFacet(String facetName) {
|
||||
return new TermsFacetBuilder(facetName);
|
||||
}
|
||||
|
||||
public static StatisticalFacetBuilder statisticalFacet(String facetName) {
|
||||
return new StatisticalFacetBuilder(facetName);
|
||||
}
|
||||
|
||||
public static StatisticalScriptFacetBuilder statisticalScriptFacet(String facetName) {
|
||||
return new StatisticalScriptFacetBuilder(facetName);
|
||||
}
|
||||
|
||||
public static HistogramFacetBuilder histogramFacet(String facetName) {
|
||||
return new HistogramFacetBuilder(facetName);
|
||||
}
|
||||
|
||||
public static HistogramScriptFacetBuilder histogramScriptFacet(String facetName) {
|
||||
return new HistogramScriptFacetBuilder(facetName);
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.histogram;
|
||||
|
||||
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 HistogramFacetBuilder extends AbstractFacetBuilder {
|
||||
private String keyFieldName;
|
||||
private String valueFieldName;
|
||||
private long interval = -1;
|
||||
private HistogramFacet.ComparatorType comparatorType;
|
||||
|
||||
public HistogramFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder field(String field) {
|
||||
this.keyFieldName = field;
|
||||
this.valueFieldName = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder keyField(String keyField) {
|
||||
this.keyFieldName = keyField;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder valueField(String valueField) {
|
||||
this.valueFieldName = valueField;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder interval(long interval) {
|
||||
this.interval = interval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder comparator(HistogramFacet.ComparatorType comparatorType) {
|
||||
this.comparatorType = comparatorType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (keyFieldName == null) {
|
||||
throw new SearchSourceBuilderException("field must be set on histogram facet for facet [" + name + "]");
|
||||
}
|
||||
if (interval < 0) {
|
||||
throw new SearchSourceBuilderException("interval must be set on histogram facet for facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
|
||||
builder.startObject(HistogramFacetCollectorParser.NAME);
|
||||
if (valueFieldName != null && !keyFieldName.equals(valueFieldName)) {
|
||||
builder.field("key_field", keyFieldName);
|
||||
builder.field("value_field", valueFieldName);
|
||||
} else {
|
||||
builder.field("field", keyFieldName);
|
||||
}
|
||||
builder.field("interval", interval);
|
||||
if (comparatorType != null) {
|
||||
builder.field("comparator", comparatorType.description());
|
||||
}
|
||||
builder.endObject();
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.histogram;
|
||||
|
||||
import org.elasticsearch.common.collect.Maps;
|
||||
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;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class HistogramScriptFacetBuilder extends AbstractFacetBuilder {
|
||||
private String keyScript;
|
||||
private String valueScript;
|
||||
private Map<String, Object> params;
|
||||
private long interval = -1;
|
||||
private HistogramFacet.ComparatorType comparatorType;
|
||||
|
||||
public HistogramScriptFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder keyScript(String keyScript) {
|
||||
this.keyScript = keyScript;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder valueScript(String valueScript) {
|
||||
this.valueScript = valueScript;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder interval(long interval) {
|
||||
this.interval = interval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder param(String name, Object value) {
|
||||
if (params == null) {
|
||||
params = Maps.newHashMap();
|
||||
}
|
||||
params.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder comparator(HistogramFacet.ComparatorType comparatorType) {
|
||||
this.comparatorType = comparatorType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HistogramScriptFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (keyScript == null) {
|
||||
throw new SearchSourceBuilderException("key_script must be set on histogram script facet for facet [" + name + "]");
|
||||
}
|
||||
if (valueScript == null) {
|
||||
throw new SearchSourceBuilderException("value_script must be set on histogram script facet for facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
|
||||
builder.startObject(HistogramFacetCollectorParser.NAME);
|
||||
builder.field("key_script", keyScript);
|
||||
builder.field("value_script", valueScript);
|
||||
if (interval > 0) { // interval is optional in script facet, can be defined by the key script
|
||||
builder.field("interval", interval);
|
||||
}
|
||||
if (this.params != null) {
|
||||
builder.field("params");
|
||||
builder.map(this.params);
|
||||
}
|
||||
if (comparatorType != null) {
|
||||
builder.field("comparator", comparatorType.description());
|
||||
}
|
||||
builder.endObject();
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.query;
|
||||
|
||||
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentFilterBuilder;
|
||||
import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilderException;
|
||||
import org.elasticsearch.search.facets.AbstractFacetBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class QueryFacetBuilder extends AbstractFacetBuilder {
|
||||
|
||||
private XContentQueryBuilder query;
|
||||
|
||||
public QueryFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public QueryFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryFacetBuilder query(XContentQueryBuilder query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (query == null) {
|
||||
throw new SearchSourceBuilderException("query must be set on query facet for facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
builder.field(QueryFacetCollectorParser.NAME);
|
||||
query.toXContent(builder, params);
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.statistical;
|
||||
|
||||
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 StatisticalFacetBuilder extends AbstractFacetBuilder {
|
||||
private String fieldName;
|
||||
|
||||
public StatisticalFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public StatisticalFacetBuilder field(String field) {
|
||||
this.fieldName = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StatisticalFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StatisticalFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (fieldName == null) {
|
||||
throw new SearchSourceBuilderException("field must be set on statistical facet for facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
|
||||
builder.startObject(StatisticalFacetCollectorParser.NAME);
|
||||
builder.field("field", fieldName);
|
||||
builder.endObject();
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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.statistical;
|
||||
|
||||
import org.elasticsearch.common.collect.Maps;
|
||||
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;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
public class StatisticalScriptFacetBuilder extends AbstractFacetBuilder {
|
||||
private String script;
|
||||
private Map<String, Object> params;
|
||||
|
||||
public StatisticalScriptFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public StatisticalScriptFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StatisticalScriptFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StatisticalScriptFacetBuilder script(String script) {
|
||||
this.script = script;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StatisticalScriptFacetBuilder param(String name, Object value) {
|
||||
if (params == null) {
|
||||
params = Maps.newHashMap();
|
||||
}
|
||||
params.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (script == null) {
|
||||
throw new SearchSourceBuilderException("script must be set on statistical script facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
|
||||
builder.startObject(StatisticalFacetCollectorParser.NAME);
|
||||
builder.field("script", script);
|
||||
if (this.params != null) {
|
||||
builder.field("params");
|
||||
builder.map(this.params);
|
||||
}
|
||||
builder.endObject();
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.terms;
|
||||
|
||||
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 TermsFacetBuilder extends AbstractFacetBuilder {
|
||||
private String fieldName;
|
||||
private int size = 10;
|
||||
|
||||
public TermsFacetBuilder(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public TermsFacetBuilder global(boolean global) {
|
||||
this.global = global;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TermsFacetBuilder filter(XContentFilterBuilder filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TermsFacetBuilder field(String field) {
|
||||
this.fieldName = field;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TermsFacetBuilder size(int size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (fieldName == null) {
|
||||
throw new SearchSourceBuilderException("field must be set on terms facet for facet [" + name + "]");
|
||||
}
|
||||
builder.startObject(name);
|
||||
|
||||
builder.startObject(TermsFacetCollectorParser.NAME);
|
||||
builder.field("field", fieldName);
|
||||
builder.field("size", size);
|
||||
builder.endObject();
|
||||
|
||||
if (filter != null) {
|
||||
builder.field("filter");
|
||||
filter.toXContent(builder, params);
|
||||
}
|
||||
if (global != null) {
|
||||
builder.field("global", global);
|
||||
}
|
||||
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.search.builder;
|
||||
package org.elasticsearch.search.highlight;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.builder.XContentBuilder;
|
||||
@ -31,9 +31,9 @@ import static org.elasticsearch.common.collect.Lists.*;
|
||||
* A builder for search highlighting.
|
||||
*
|
||||
* @author kimchy (shay.banon)
|
||||
* @see SearchSourceBuilder#highlight()
|
||||
* @see org.elasticsearch.search.builder.SearchSourceBuilder#highlight()
|
||||
*/
|
||||
public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
public class HighlightBuilder implements ToXContent {
|
||||
|
||||
private List<Field> fields;
|
||||
|
||||
@ -51,7 +51,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
*
|
||||
* @param name The field to highlight
|
||||
*/
|
||||
public SearchSourceHighlightBuilder field(String name) {
|
||||
public HighlightBuilder field(String name) {
|
||||
if (fields == null) {
|
||||
fields = newArrayList();
|
||||
}
|
||||
@ -66,7 +66,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
* @param name The field to highlight
|
||||
* @param fragmentSize The size of a fragment in characters
|
||||
*/
|
||||
public SearchSourceHighlightBuilder field(String name, int fragmentSize) {
|
||||
public HighlightBuilder field(String name, int fragmentSize) {
|
||||
if (fields == null) {
|
||||
fields = newArrayList();
|
||||
}
|
||||
@ -82,7 +82,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
* @param fragmentSize The size of a fragment in characters
|
||||
* @param numberOfFragments The (maximum) number of fragments
|
||||
*/
|
||||
public SearchSourceHighlightBuilder field(String name, int fragmentSize, int numberOfFragments) {
|
||||
public HighlightBuilder field(String name, int fragmentSize, int numberOfFragments) {
|
||||
if (fields == null) {
|
||||
fields = newArrayList();
|
||||
}
|
||||
@ -96,7 +96,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
*
|
||||
* @param schemaName The tag scheme name
|
||||
*/
|
||||
public SearchSourceHighlightBuilder tagsSchema(String schemaName) {
|
||||
public HighlightBuilder tagsSchema(String schemaName) {
|
||||
this.tagsSchema = schemaName;
|
||||
return this;
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
/**
|
||||
* Explicitly set the pre tags that will be used for highlighting.
|
||||
*/
|
||||
public SearchSourceHighlightBuilder preTags(String... preTags) {
|
||||
public HighlightBuilder preTags(String... preTags) {
|
||||
this.preTags = preTags;
|
||||
return this;
|
||||
}
|
||||
@ -112,7 +112,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
/**
|
||||
* Explicitly set the post tags that will be used for highlighting.
|
||||
*/
|
||||
public SearchSourceHighlightBuilder postTags(String... postTags) {
|
||||
public HighlightBuilder postTags(String... postTags) {
|
||||
this.postTags = postTags;
|
||||
return this;
|
||||
}
|
||||
@ -122,7 +122,7 @@ public class SearchSourceHighlightBuilder implements ToXContent {
|
||||
* highlighted text. Can be <tt>score</tt>, which then it will be ordered
|
||||
* by score of the fragments.
|
||||
*/
|
||||
public SearchSourceHighlightBuilder order(String order) {
|
||||
public HighlightBuilder order(String order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
@ -32,6 +32,7 @@ import org.elasticsearch.search.controller.SearchPhaseController;
|
||||
import org.elasticsearch.search.controller.ShardDoc;
|
||||
import org.elasticsearch.search.dfs.AggregatedDfs;
|
||||
import org.elasticsearch.search.dfs.DfsSearchResult;
|
||||
import org.elasticsearch.search.facets.FacetBuilders;
|
||||
import org.elasticsearch.search.facets.query.QueryFacet;
|
||||
import org.elasticsearch.search.fetch.FetchSearchRequest;
|
||||
import org.elasticsearch.search.fetch.FetchSearchResult;
|
||||
@ -149,7 +150,8 @@ public class SingleInstanceEmbeddedSearchTests extends AbstractNodesTests {
|
||||
@Test public void testSimpleQueryFacets() throws Exception {
|
||||
QuerySearchResult queryResult = searchService.executeQueryPhase(searchRequest(
|
||||
searchSource().query(wildcardQuery("name", "te*"))
|
||||
.facets(facets().queryFacet("age2", termQuery("age", 2)).queryFacet("age1", termQuery("age", 1)))
|
||||
.facet(FacetBuilders.queryFacet("age1", termQuery("age", 1)))
|
||||
.facet(FacetBuilders.queryFacet("age2", termQuery("age", 2)))
|
||||
));
|
||||
assertThat(queryResult.facets().facet(QueryFacet.class, "age2").count(), equalTo(4l));
|
||||
assertThat(queryResult.facets().facet(QueryFacet.class, "age1").count(), equalTo(1l));
|
||||
|
@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.builder.XContentBuilder;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.facets.FacetBuilders;
|
||||
import org.elasticsearch.search.facets.query.QueryFacet;
|
||||
import org.elasticsearch.test.integration.AbstractNodesTests;
|
||||
import org.testng.annotations.AfterClass;
|
||||
@ -255,7 +256,8 @@ public class TransportTwoServersSearchTests extends AbstractNodesTests {
|
||||
SearchSourceBuilder sourceBuilder = searchSource()
|
||||
.query(termQuery("multi", "test"))
|
||||
.from(0).size(20).explain(true)
|
||||
.facets(facets().queryFacetGlobal("all", termQuery("multi", "test")).queryFacet("test1", termQuery("name", "test1")));
|
||||
.facet(FacetBuilders.queryFacet("all", termQuery("multi", "test")).global(true))
|
||||
.facet(FacetBuilders.queryFacet("test1", termQuery("name", "test1")));
|
||||
|
||||
SearchResponse searchResponse = client.search(searchRequest("test").source(sourceBuilder)).actionGet();
|
||||
assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
|
||||
|
@ -36,6 +36,7 @@ import org.elasticsearch.search.controller.SearchPhaseController;
|
||||
import org.elasticsearch.search.controller.ShardDoc;
|
||||
import org.elasticsearch.search.dfs.AggregatedDfs;
|
||||
import org.elasticsearch.search.dfs.DfsSearchResult;
|
||||
import org.elasticsearch.search.facets.FacetBuilders;
|
||||
import org.elasticsearch.search.facets.query.QueryFacet;
|
||||
import org.elasticsearch.search.fetch.FetchSearchRequest;
|
||||
import org.elasticsearch.search.fetch.FetchSearchResult;
|
||||
@ -320,7 +321,8 @@ public class TwoInstanceEmbeddedSearchTests extends AbstractNodesTests {
|
||||
SearchSourceBuilder sourceBuilder = searchSource()
|
||||
.query(termQuery("multi", "test"))
|
||||
.from(0).size(20).explain(true).sort("age", false)
|
||||
.facets(facets().queryFacet("all", termQuery("multi", "test")).queryFacet("test1", termQuery("name", "test1")));
|
||||
.facet(FacetBuilders.queryFacet("all", termQuery("multi", "test")))
|
||||
.facet(FacetBuilders.queryFacet("test1", termQuery("name", "test1")));
|
||||
|
||||
Map<SearchShardTarget, QuerySearchResultProvider> queryResults = newHashMap();
|
||||
for (ShardsIterator shardsIt : indicesService.searchShards(clusterService.state(), new String[]{"test"}, null)) {
|
||||
|
@ -67,6 +67,7 @@ import static org.elasticsearch.common.collect.Maps.*;
|
||||
import static org.elasticsearch.common.unit.TimeValue.*;
|
||||
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
|
||||
import static org.elasticsearch.search.builder.SearchSourceBuilder.*;
|
||||
import static org.elasticsearch.search.facets.FacetBuilders.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@ -326,7 +327,8 @@ public class TwoInstanceUnbalancedShardsEmbeddedSearchTests extends AbstractNode
|
||||
SearchSourceBuilder sourceBuilder = searchSource()
|
||||
.query(termQuery("multi", "test"))
|
||||
.from(0).size(20).explain(true).sort("age", false)
|
||||
.facets(facets().queryFacet("all", termQuery("multi", "test")).queryFacet("test1", termQuery("name", "test1")));
|
||||
.facet(queryFacet("all").query(termQuery("multi", "test")))
|
||||
.facet(queryFacet("test1").query(termQuery("name", "test1")));
|
||||
|
||||
Map<SearchShardTarget, QuerySearchResultProvider> queryResults = newHashMap();
|
||||
for (ShardsIterator shardsIt : indicesService.searchShards(clusterService.state(), new String[]{"test"}, null)) {
|
||||
|
@ -34,6 +34,7 @@ import org.testng.annotations.Test;
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.*;
|
||||
import static org.elasticsearch.index.query.xcontent.FilterBuilders.*;
|
||||
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
|
||||
import static org.elasticsearch.search.facets.FacetBuilders.*;
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@ -86,8 +87,7 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
SearchResponse searchResponse = client.prepareSearch()
|
||||
.setSize(0)
|
||||
.setQuery(termQuery("stag", "111"))
|
||||
.addFacetTerms("facet1", "stag", 10)
|
||||
.addFacetTerms("facet2", "tag", 10)
|
||||
.addFacet(termsFacet("facet1").field("stag").size(10))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(searchResponse.hits().hits().length, equalTo(0));
|
||||
@ -102,8 +102,8 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
.setSearchType(SearchType.QUERY_AND_FETCH)
|
||||
.setSize(0)
|
||||
.setQuery(termQuery("stag", "111"))
|
||||
.addFacetTerms("facet1", "stag", 10)
|
||||
.addFacetTerms("facet2", "tag", 10)
|
||||
.addFacet(termsFacet("facet1").field("stag").size(10))
|
||||
.addFacet(termsFacet("facet2").field("tag").size(10))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(searchResponse.hits().hits().length, equalTo(0));
|
||||
@ -139,8 +139,8 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
|
||||
SearchResponse searchResponse = client.prepareSearch()
|
||||
.setQuery(termQuery("stag", "111"))
|
||||
.addFacetTerms("facet1", "stag", 10)
|
||||
.addFacetTerms("facet2", "tag", 10)
|
||||
.addFacet(termsFacet("facet1").field("stag").size(10))
|
||||
.addFacet(termsFacet("facet2").field("tag").size(10))
|
||||
.execute().actionGet();
|
||||
|
||||
TermsFacet facet = searchResponse.facets().facet(TermsFacet.class, "facet1");
|
||||
@ -157,7 +157,7 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
|
||||
searchResponse = client.prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.addFacetTerms("facet1", "stag", 10, termFilter("tag", "xxx"))
|
||||
.addFacet(termsFacet("facet1").field("stag").size(10).filter(termFilter("tag", "xxx")))
|
||||
.execute().actionGet();
|
||||
|
||||
facet = searchResponse.facets().facet(TermsFacet.class, "facet1");
|
||||
@ -190,9 +190,9 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
|
||||
SearchResponse searchResponse = client.prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.addFacetStatistical("stats1", "num")
|
||||
.addFacetStatistical("stats2", "multi_num")
|
||||
.addFacetStatisticalScript("stats3", "doc['num'].value * 2")
|
||||
.addFacet(statisticalFacet("stats1").field("num"))
|
||||
.addFacet(statisticalFacet("stats2").field("multi_num"))
|
||||
.addFacet(statisticalScriptFacet("stats3").script("doc['num'].value * 2"))
|
||||
.execute().actionGet();
|
||||
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
@ -262,10 +262,10 @@ public class SimpleFacetsTests extends AbstractNodesTests {
|
||||
|
||||
SearchResponse searchResponse = client.prepareSearch()
|
||||
.setQuery(matchAllQuery())
|
||||
.addFacetHistogram("stats1", "num", 100)
|
||||
.addFacetHistogram("stats2", "multi_num", 10)
|
||||
.addFacetHistogram("stats3", "num", "multi_num", 100)
|
||||
.addFacetHistogramScript("stats4", "doc['date'].date.minuteOfHour", "doc['num'].value")
|
||||
.addFacet(histogramFacet("stats1").field("num").interval(100))
|
||||
.addFacet(histogramFacet("stats2").field("multi_num").interval(10))
|
||||
.addFacet(histogramFacet("stats3").keyField("num").valueField("multi_num").interval(100))
|
||||
.addFacet(histogramScriptFacet("stats4").keyScript("doc['date'].date.minuteOfHour").valueScript("doc['num'].value"))
|
||||
.execute().actionGet();
|
||||
|
||||
if (searchResponse.failedShards() > 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user