Skip to content

Commit 15167a5

Browse files
author
Clément Tourrière
committed
Initial commit for path hierarchy aggregation implementation elastic/elasticsearch#8896
0 parents  commit 15167a5

File tree

11 files changed

+1029
-0
lines changed

11 files changed

+1029
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Elasticsearch Aggregation Path Hierarchy Plugin
2+
=========================================
3+
4+
This is a multi bucket aggregation.
5+
6+
| elasticsearch | Path hierarchy plugin |
7+
|---------------|---------------------------|
8+
| 1.6.0 | 1.6.0.0 |
9+
10+
11+
Installation
12+
------------
13+
14+
`bin/plugin --install envelope_aggregation --url "https://github.com/opendatasoft/elasticsearch-aggregation-pathhierarchy/releases/download/v1.6.0.0/elasticsearch-envelope-pathhierarchy-1.6.0.0.zip"`
15+
16+
17+
Usage
18+
-----
19+
20+
### Parameters
21+
22+
- `field` or `script` : field to aggregate on
23+
- `separator` : separator for path hierarchy (default to "/")
24+
- `order` : order parameter to define how to sort result. Allowed parameters are `_term`, `_count` or sub aggregation name. Default to {"_count": "desc}.
25+
- `max_depth`: Set maximum depth level. `-1` means no limit. Default to 3.
26+
27+
28+
For a complete example see https://github.com/elastic/elasticsearch/issues/8896
29+
30+
License
31+
-------
32+
33+
This software is under The MIT License (MIT)

pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.opendatasoft.elasticsearch</groupId>
9+
<artifactId>elasticsearch-aggregation-pathhierarchy</artifactId>
10+
<version>1.6.0.0-SNAPSHOT</version>
11+
12+
<parent>
13+
<groupId>org.sonatype.oss</groupId>
14+
<artifactId>oss-parent</artifactId>
15+
<version>7</version>
16+
</parent>
17+
18+
<properties>
19+
<elasticsearch.version>1.6.0</elasticsearch.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.elasticsearch</groupId>
25+
<artifactId>elasticsearch</artifactId>
26+
<version>${elasticsearch.version}</version>
27+
<scope>compile</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>2.3.2</version>
37+
<configuration>
38+
<source>1.7</source>
39+
<target>1.7</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-surefire-plugin</artifactId>
45+
<version>2.11</version>
46+
<configuration>
47+
<includes>
48+
<include>**/*Tests.java</include>
49+
</includes>
50+
</configuration>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-source-plugin</artifactId>
55+
<version>2.1.2</version>
56+
<executions>
57+
<execution>
58+
<id>attach-sources</id>
59+
<goals>
60+
<goal>jar</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
<plugin>
66+
<artifactId>maven-assembly-plugin</artifactId>
67+
<version>2.3</version>
68+
<configuration>
69+
<appendAssemblyId>false</appendAssemblyId>
70+
<outputDirectory>${project.build.directory}/releases/</outputDirectory>
71+
<descriptors>
72+
<descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
73+
</descriptors>
74+
</configuration>
75+
<executions>
76+
<execution>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>single</goal>
80+
</goals>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
88+
</project>

src/main/assemblies/plugin.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<assembly>
3+
<id>plugin</id>
4+
<formats>
5+
<format>zip</format>
6+
</formats>
7+
<includeBaseDirectory>false</includeBaseDirectory>
8+
<dependencySets>
9+
<dependencySet>
10+
<outputDirectory>/</outputDirectory>
11+
<useProjectArtifact>true</useProjectArtifact>
12+
<useTransitiveFiltering>true</useTransitiveFiltering>
13+
<excludes>
14+
<exclude>org.elasticsearch:elasticsearch</exclude>
15+
<exclude>log4j:log4j</exclude>
16+
</excludes>
17+
</dependencySet>
18+
</dependencySets>
19+
</assembly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.opendatasoft.elasticsearch.plugin.pathhierarchyaggregation;
2+
3+
import com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation.PathHierarchyParser;
4+
import com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation.InternalPathHierarchy;
5+
import org.elasticsearch.plugins.AbstractPlugin;
6+
import org.elasticsearch.search.aggregations.AggregationModule;
7+
8+
public class PathHierarchyAggregationPlugin extends AbstractPlugin {
9+
@Override
10+
public String name() {
11+
return "Path Hierarchy";
12+
}
13+
14+
@Override
15+
public String description() {
16+
return "Return a path hierarchy aggregation";
17+
}
18+
19+
public void onModule(AggregationModule aggModule) {
20+
aggModule.addAggregatorParser(PathHierarchyParser.class);
21+
InternalPathHierarchy.registerStreams();
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation;
20+
21+
import org.elasticsearch.common.io.stream.StreamInput;
22+
import org.elasticsearch.common.io.stream.StreamOutput;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
25+
26+
import java.io.IOException;
27+
import java.util.Comparator;
28+
29+
30+
class InternalOrder extends PathHierarchy.Order {
31+
32+
final byte id;
33+
final String key;
34+
final boolean asc;
35+
final Comparator<InternalPathHierarchy.Bucket> comparator;
36+
37+
InternalOrder(byte id, String key, boolean asc, Comparator<InternalPathHierarchy.Bucket> comparator) {
38+
this.id = id;
39+
this.key = key;
40+
this.asc = asc;
41+
this.comparator = comparator;
42+
}
43+
44+
byte id() {
45+
return id;
46+
}
47+
48+
String key() {
49+
return key;
50+
}
51+
52+
boolean asc() {
53+
return asc;
54+
}
55+
56+
@Override
57+
Comparator<InternalPathHierarchy.Bucket> comparator() {
58+
return comparator;
59+
}
60+
61+
@Override
62+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
63+
return builder.startObject().field(key, asc ? "asc" : "desc").endObject();
64+
}
65+
66+
static class Aggregation extends com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation.InternalOrder {
67+
68+
static final byte ID = 0;
69+
70+
Aggregation(String key, boolean asc) {
71+
super(ID, key, asc, new MultiBucketsAggregation.Bucket.SubAggregationComparator<InternalPathHierarchy.Bucket>(key, asc));
72+
}
73+
74+
private static String key(String aggName, String valueName) {
75+
return (valueName == null) ? aggName : aggName + "." + valueName;
76+
}
77+
78+
}
79+
80+
static class Streams {
81+
82+
/**
83+
* Writes the given order to the given output (based on the id of the order).
84+
*/
85+
public static void writeOrder(InternalOrder order, StreamOutput out) throws IOException {
86+
out.writeByte(order.id());
87+
if (order instanceof com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation.InternalOrder.Aggregation) {
88+
out.writeBoolean(order.asc());
89+
out.writeString(order.key());
90+
}
91+
}
92+
93+
/**
94+
* Reads an order from the given input (based on the id of the order).
95+
*/
96+
public static InternalOrder readOrder(StreamInput in) throws IOException {
97+
byte id = in.readByte();
98+
switch (id) {
99+
case 1: return (InternalOrder) PathHierarchy.Order.KEY_ASC;
100+
case 2: return (InternalOrder) PathHierarchy.Order.KEY_DESC;
101+
case 3: return (InternalOrder) PathHierarchy.Order.COUNT_ASC;
102+
case 4: return (InternalOrder) PathHierarchy.Order.COUNT_DESC;
103+
case 0:
104+
boolean asc = in.readBoolean();
105+
String key = in.readString();
106+
return new com.opendatasoft.elasticsearch.search.aggregations.bucket.pathhierarchyaggregation.InternalOrder.Aggregation(key, asc);
107+
default:
108+
throw new RuntimeException("unknown pathhierarchy order");
109+
}
110+
}
111+
112+
}
113+
114+
115+
}

0 commit comments

Comments
 (0)