Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ICU collation based sorting for facets #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Expand Up @@ -148,6 +148,69 @@ Breaks text into words according to UAX #29: Unicode Text Segmentation ((http://
}
}

ICU Facets
----------

The ICU terms facet allows to sort by an ICU locale setting.

Be aware, the order of the attributes is sensitive, first the attribute "locale" needs to be declared, followed by the attribute "collator". The "collator" may have one of the values "term", "reverse\_term", or (for convenience) "count" and "reverse\_count".

For example, this facet sorts a name field by german phonebook order (DIN 5007-2).

{
"facets" : {
"facet1" : {
"icu" : {
"locale" : "de@collation=phonebook",
"collator" : "term",
"field" : "name"
}
}
}
}

The result looks like

{
"took" : 241,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.0,
"hits" : [...]
},
"facets" : {
"facet1" : {
"_type" : "icu",
"missing" : 0,
"total" : 5,
"other" : 0,
"terms" : [ {
"term" : "göbel",
"count" : 1
}, {
"term" : "goethe",
"count" : 1
}, {
"term" : "göthe",
"count" : 1
}, {
"term" : "götz",
"count" : 1
}, {
"term" : "goldmann",
"count" : 1
} ]
}
}
}
}


License
-------
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -31,7 +31,7 @@
</parent>

<properties>
<elasticsearch.version>0.19.3</elasticsearch.version>
<elasticsearch.version>0.19.10</elasticsearch.version>
</properties>

<repositories>
Expand Down
Expand Up @@ -27,6 +27,8 @@
import org.elasticsearch.plugins.AbstractPlugin;

import java.util.Collection;
import org.elasticsearch.search.facet.FacetModule;
import org.elasticsearch.search.facet.icu.TermsFacetProcessor;

/**
*
Expand Down Expand Up @@ -54,4 +56,9 @@ public Collection<Class<? extends Module>> modules() {
public void onModule(AnalysisModule module) {
module.addProcessor(new IcuAnalysisBinderProcessor());
}

public void onModule(FacetModule facetModule) {
facetModule.addFacetProcessor(TermsFacetProcessor.class);
}

}
@@ -0,0 +1,38 @@
/*
* 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.icu;

import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.InternalFacet;
import org.elasticsearch.search.facet.terms.strings.icu.InternalStringTermsFacet;

import java.util.List;

/**
*
*/
public abstract class InternalTermsFacet implements TermsFacet, InternalFacet {

public static void registerStreams() {
InternalStringTermsFacet.registerStream();
}

public abstract Facet reduce(String name, List<Facet> facets);
}
87 changes: 87 additions & 0 deletions src/main/java/org/elasticsearch/search/facet/icu/TermsFacet.java
@@ -0,0 +1,87 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch 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.icu;

import java.util.List;
import org.elasticsearch.search.facet.Facet;

/**
* Terms facet allows to return facets of the most popular terms within the search query.
*
*
*/
public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {

/**
* The type of the filter facet.
*/
public static final String TYPE = "icu";

public interface Entry extends Comparable<Entry> {

String term();

String getTerm();

int count();

int getCount();
}

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

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

/**
* The total count of terms.
*/
long totalCount();

/**
* The total count of terms.
*/
long getTotalCount();

/**
* The count of terms other than the one provided by the entries.
*/
long otherCount();

/**
* The count of terms other than the one provided by the entries.
*/
long getOtherCount();

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

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