Skip to content

Commit

Permalink
Search: Terms Stats Facet, closes #705.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Feb 19, 2011
1 parent 745614f commit 352cb74
Show file tree
Hide file tree
Showing 14 changed files with 2,212 additions and 1 deletion.
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.search.facet.statistical.StatisticalFacetBuilder;
import org.elasticsearch.search.facet.statistical.StatisticalScriptFacetBuilder;
import org.elasticsearch.search.facet.terms.TermsFacetBuilder;
import org.elasticsearch.search.facet.termsstats.TermsStatsFacetBuilder;

/**
* @author kimchy (shay.banon)
Expand All @@ -58,6 +59,10 @@ public static TermsFacetBuilder termsFacet(String facetName) {
return new TermsFacetBuilder(facetName);
}

public static TermsStatsFacetBuilder termsStats(String facetName) {
return new TermsStatsFacetBuilder(facetName);
}

public static StatisticalFacetBuilder statisticalFacet(String facetName) {
return new StatisticalFacetBuilder(facetName);
}
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.search.facet.range.RangeFacetProcessor;
import org.elasticsearch.search.facet.statistical.StatisticalFacetProcessor;
import org.elasticsearch.search.facet.terms.TermsFacetProcessor;
import org.elasticsearch.search.facet.termsstats.TermsStatsFacetProcessor;

import java.util.List;

Expand All @@ -49,6 +50,7 @@ public FacetModule() {
processors.add(RangeFacetProcessor.class);
processors.add(StatisticalFacetProcessor.class);
processors.add(TermsFacetProcessor.class);
processors.add(TermsStatsFacetProcessor.class);
}

public void addFacetProcessor(Class<? extends FacetProcessor> facetProcessor) {
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.search.facet.range.InternalRangeFacet;
import org.elasticsearch.search.facet.statistical.InternalStatisticalFacet;
import org.elasticsearch.search.facet.terms.InternalTermsFacet;
import org.elasticsearch.search.facet.termsstats.InternalTermsStatsFacet;

/**
* @author kimchy (shay.banon)
Expand All @@ -43,5 +44,6 @@ public class TransportFacetModule extends AbstractModule {
InternalRangeFacet.registerStreams();
InternalStatisticalFacet.registerStreams();
InternalTermsFacet.registerStreams();
InternalTermsStatsFacet.registerStreams();
}
}
@@ -0,0 +1,39 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.facet.termsstats;

import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.InternalFacet;
import org.elasticsearch.search.facet.termsstats.doubles.InternalTermsStatsDoubleFacet;
import org.elasticsearch.search.facet.termsstats.longs.InternalTermsStatsLongFacet;
import org.elasticsearch.search.facet.termsstats.strings.InternalTermsStatsStringFacet;

import java.util.List;

public abstract class InternalTermsStatsFacet implements TermsStatsFacet, InternalFacet {

public static void registerStreams() {
InternalTermsStatsStringFacet.registerStream();
InternalTermsStatsLongFacet.registerStream();
InternalTermsStatsDoubleFacet.registerStream();
}

public abstract Facet reduce(String name, List<Facet> facets);
}
@@ -0,0 +1,187 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.facet.termsstats;

import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.search.facet.Facet;

import java.util.Comparator;
import java.util.List;

public interface TermsStatsFacet extends Facet, Iterable<TermsStatsFacet.Entry> {

public static final String TYPE = "terms_stats";


/**
* The number of docs missing a value.
*/
long missingCount();

/**
* The number of docs missing a value.
*/
long getMissingCount();

/**
* The terms and counts.
*/
List<? extends TermsStatsFacet.Entry> entries();

/**
* The terms and counts.
*/
List<? extends TermsStatsFacet.Entry> getEntries();

/**
* Controls how the terms facets are ordered.
*/
public static enum ComparatorType {
/**
* Order by the (higher) count of each term.
*/
COUNT((byte) 0, new Comparator<Entry>() {

@Override public int compare(Entry o1, Entry o2) {
int i = o2.count() - o1.count();
if (i == 0) {
i = o2.term().compareTo(o1.term());
if (i == 0) {
i = System.identityHashCode(o2) - System.identityHashCode(o1);
}
}
return i;
}
}),
/**
* Order by the (lower) count of each term.
*/
REVERSE_COUNT((byte) 1, new Comparator<Entry>() {

@Override public int compare(Entry o1, Entry o2) {
return -COUNT.comparator().compare(o1, o2);
}
}),
/**
* Order by the terms.
*/
TERM((byte) 2, new Comparator<Entry>() {

@Override public int compare(Entry o1, Entry o2) {
return o1.compareTo(o2);
}
}),
/**
* Order by the terms.
*/
REVERSE_TERM((byte) 3, new Comparator<Entry>() {

@Override public int compare(Entry o1, Entry o2) {
return -TERM.comparator().compare(o1, o2);
}
}),

TOTAL((byte) 4, new Comparator<Entry>() {
@Override public int compare(Entry o1, Entry o2) {
return (o2.total() < o1.total() ? -1 : (o2.total() == o1.total() ? 0 : 1));
}
}),

REVERSE_TOTAL((byte) 5, new Comparator<Entry>() {
@Override public int compare(Entry o1, Entry o2) {
return (o1.total() < o2.total() ? -1 : (o1.total() == o2.total() ? 0 : 1));
}
});

private final byte id;

private final Comparator<Entry> comparator;

ComparatorType(byte id, Comparator<Entry> comparator) {
this.id = id;
this.comparator = comparator;
}

public byte id() {
return this.id;
}

public Comparator<Entry> comparator() {
return comparator;
}

public static ComparatorType fromId(byte id) {
if (id == COUNT.id()) {
return COUNT;
} else if (id == REVERSE_COUNT.id()) {
return REVERSE_COUNT;
} else if (id == TERM.id()) {
return TERM;
} else if (id == REVERSE_TERM.id()) {
return REVERSE_TERM;
} else if (id == TOTAL.id()) {
return TOTAL;
} else if (id == REVERSE_TOTAL.id()) {
return REVERSE_TOTAL;
}
throw new ElasticSearchIllegalArgumentException("No type argument match for terms facet comparator [" + id + "]");
}

public static ComparatorType fromString(String type) {
if ("count".equals(type)) {
return COUNT;
} else if ("term".equals(type)) {
return TERM;
} else if ("reverse_count".equals(type) || "reverseCount".equals(type)) {
return REVERSE_COUNT;
} else if ("reverse_term".equals(type) || "reverseTerm".equals(type)) {
return REVERSE_TERM;
} else if ("total".equals(type)) {
return TOTAL;
} else if ("reverse_total".equals(type) || "reverseTotal".equals(type)) {
return REVERSE_TOTAL;
}
throw new ElasticSearchIllegalArgumentException("No type argument match for terms stats facet comparator [" + type + "]");
}
}

public interface Entry extends Comparable<Entry> {

String term();

String getTerm();

Number termAsNumber();

Number getTermAsNumber();

int count();

int getCount();

double total();

double getTotal();

double mean();

double getMean();
}
}

0 comments on commit 352cb74

Please sign in to comment.