Browse files

cleaner registration of module, added field nconfinement

  • Loading branch information...
1 parent 7e97fae commit b86639ffcc7e7c73d18d4c36d98c516f4bdde448 @jprante committed Mar 26, 2012
View
18 README.md
@@ -6,7 +6,7 @@ This plugin extends Elasticsearch with a term list capability. Term lists can be
Installation
------------
- bin/plugin -install jprante/elasticsearch-index-termlist/1.0.0
+ bin/plugin -install jprante/elasticsearch-index-termlist/1.1.0
Introduction
------------
@@ -19,16 +19,28 @@ Getting the list of all terms indexed is useful for variuos purposes, for exampl
- input to linguistic analysis tools
- for other post-processing of the indexed terms outside of Elasticsearch
-Example of getting the term list of index `test`
+Example of getting term lists
+
+Consider the following example index
curl -XPUT 'http://localhost:9200/test/'
curl -XPUT 'http://localhost:9200/test/test/1' -d '{ "test": "Hello World" }'
curl -XPUT 'http://localhost:9200/test/test/2' -d '{ "test": "Hello Jörg Prante" }'
curl -XPUT 'http://localhost:9200/test/test/3' -d '{ "message": "elastic search" }'
+
+Get term list of index `test`
+
curl -XGET 'http://localhost:9200/test/_termlist'
{"ok":true,"_shards":{"total":5,"successful":5,"failed":0},"terms":["hello","prant","world","elastic","search","jorg"]}
-Only terms of field names not starting with underscore are listed. Terms of internal fields like `_uid`, `_all`, or `_type` are skipped.
+Get term list of index `test` of field `message`
+
+ curl -XGET 'http://localhost:9200/test/_termlist/message'
+ {"ok":true,"_shards":{"total":5,"successful":5,"failed":0},"terms":["elastic","search"]}
+
+Optionally, the term list can be narrowed down to a field name. The field name is the Lucene field name as found in the Lucene index.
+
+Only terms of field names not starting with underscore are listed. Terms of internal fields like `_uid`, `_all`, or `_type` are always skipped.
If you want a sorted term list, you have to sort the obtained list at client side.
View
2 pom.xml
@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-index-termlist</artifactId>
- <version>1.0.0</version>
+ <version>1.1.0</version>
<name>Elastic Search Index Termlist Plugin</name>
<packaging>jar</packaging>
<dependencies>
View
7 src/main/java/org/elasticsearch/action/termlist/CompactHashSet.java
@@ -24,6 +24,13 @@
import java.util.Iterator;
import java.util.NoSuchElementException;
+/**
+ * CompactHashSet - implementation taken from
+ * http://code.google.com/p/ontopia/source/browse/trunk/ontopia/src/java/net/ontopia/utils/CompactHashSet.java?r=1704
+ * (License Apache 2.0)
+ * load factor increased to 0.9
+ *
+ */
public class CompactHashSet extends AbstractSet {
private final static int INITIAL_SIZE = 3;
View
13 src/main/java/org/elasticsearch/action/termlist/ShardTermlistRequest.java
@@ -25,20 +25,33 @@
class ShardTermlistRequest extends BroadcastShardOperationRequest {
+ private String field;
+
ShardTermlistRequest() {
}
public ShardTermlistRequest(String index, int shardId, TermlistRequest request) {
super(index, shardId);
+ this.field = request.getField();
}
+
+ public void setField(String field) {
+ this.field = field;
+ }
+
+ public String getField() {
+ return field;
+ }
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
+ field = in.readUTF();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
+ out.writeUTF(field);
}
}
View
2 src/main/java/org/elasticsearch/action/termlist/ShardTermlistResponse.java
@@ -44,7 +44,7 @@ public ShardTermlistResponse(String index, int shardId, Set<String> termlist) {
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
int n = in.readInt();
- termlist = new CompactHashSet();
+ termlist = new CompactHashSet(n);
for (int i = 0; i <n; i++) {
termlist.add(in.readUTF());
}
View
74 src/main/java/org/elasticsearch/action/termlist/TermToken.java
@@ -1,74 +0,0 @@
-/*
- * 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.action.termlist;
-
-import java.io.IOException;
-import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.common.io.stream.Streamable;
-
-public class TermToken implements Streamable, Comparable<TermToken> {
-
- private String term;
- private int docfreq;
- private int shard;
-
- TermToken() {
- }
-
- public TermToken(String term, int docfreq, int shard) {
- this.term = term;
- this.docfreq = docfreq;
- this.shard = shard;
- }
-
- public String getTerm() {
- return term;
- }
-
- public int getDocFreq() {
- return docfreq;
- }
-
- public int getShard() {
- return shard;
- }
-
- public static TermToken readTermToken(StreamInput in) throws IOException {
- TermToken termToken = new TermToken();
- termToken.readFrom(in);
- return termToken;
- }
-
- public void readFrom(StreamInput in) throws IOException {
- term = in.readUTF();
- docfreq = in.readInt();
- shard = in.readInt();
- }
-
- public void writeTo(StreamOutput out) throws IOException {
- out.writeUTF(term);
- out.writeInt(docfreq);
- out.writeInt(shard);
- }
-
- public int compareTo(TermToken t) {
- return getTerm().compareTo(t.getTerm());
- }
-}
View
12 src/main/java/org/elasticsearch/action/termlist/TermlistRequest.java
@@ -26,6 +26,8 @@
public class TermlistRequest extends BroadcastOperationRequest {
+ private String field;
+
TermlistRequest() {
}
@@ -34,13 +36,23 @@ public TermlistRequest(String... indices) {
operationThreading(BroadcastOperationThreading.THREAD_PER_SHARD);
}
+ public void setField(String field) {
+ this.field = field;
+ }
+
+ public String getField() {
+ return field;
+ }
+
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
+ field = in.readUTF();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
+ out.writeUTF(field);
}
}
View
4 src/main/java/org/elasticsearch/action/termlist/TransportTermlistAction.java
@@ -133,7 +133,9 @@ protected ShardTermlistResponse shardOperation(ShardTermlistRequest request) thr
do {
Term t = te.term();
if (t != null && t.field().charAt(0) != '_') {
- set.add(t.text());
+ if (request.getField() == null || t.field().equals(request.getField())) {
+ set.add(t.text());
+ }
}
} while (te.next());
te.close();
View
16 src/main/java/org/elasticsearch/module/termlist/TermlistModule.java
@@ -1,19 +1,17 @@
package org.elasticsearch.module.termlist;
-import org.elasticsearch.action.GenericAction;
+import org.elasticsearch.action.ActionModule;
import org.elasticsearch.action.termlist.TermlistAction;
import org.elasticsearch.action.termlist.TransportTermlistAction;
-import org.elasticsearch.action.support.TransportAction;
-import org.elasticsearch.common.inject.AbstractModule;
-import org.elasticsearch.common.inject.multibindings.MapBinder;
-public class TermlistModule extends AbstractModule {
+public class TermlistModule extends ActionModule {
+ public TermlistModule() {
+ super(true);
+ }
+
@Override
protected void configure() {
- bind(TransportTermlistAction.class).asEagerSingleton();
- MapBinder<GenericAction, TransportAction> transportActionsBinder =
- MapBinder.newMapBinder(binder(), GenericAction.class, TransportAction.class);
- transportActionsBinder.addBinding(TermlistAction.INSTANCE).to(TransportTermlistAction.class).asEagerSingleton();
+ registerAction(TermlistAction.INSTANCE, TransportTermlistAction.class);
}
}
View
17 src/main/java/org/elasticsearch/plugin/termlist/IndexTermlistPlugin.java
@@ -1,9 +1,8 @@
package org.elasticsearch.plugin.termlist;
-import java.util.Collection;
-import org.elasticsearch.common.collect.Lists;
-import org.elasticsearch.common.inject.Module;
-import org.elasticsearch.module.termlist.TermlistModule;
+import org.elasticsearch.action.ActionModule;
+import org.elasticsearch.action.termlist.TermlistAction;
+import org.elasticsearch.action.termlist.TransportTermlistAction;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.rest.RestModule;
import org.elasticsearch.rest.action.termlist.RestTermlistAction;
@@ -24,10 +23,8 @@ public void onModule(RestModule module) {
module.addRestAction(RestTermlistAction.class);
}
- @Override
- public Collection<Class<? extends Module>> modules() {
- Collection<Class<? extends Module>> modules = Lists.newArrayList();
- modules.add(TermlistModule.class);
- return modules;
- }
+ public void onModule(ActionModule module) {
+ module.registerAction(TermlistAction.INSTANCE, TransportTermlistAction.class);
+ }
+
}
View
55 src/main/java/org/elasticsearch/rest/action/termlist/RestTermlistAction.java
@@ -42,46 +42,43 @@ public RestTermlistAction(Settings settings, Client client, RestController contr
super(settings, client);
controller.registerHandler(POST, "/_termlist", this);
controller.registerHandler(POST, "/{index}/_termlist", this);
+ controller.registerHandler(POST, "/_termlist/{field}", this);
+ controller.registerHandler(POST, "/{index}/_termlist/{field}", this);
controller.registerHandler(GET, "/_termlist", this);
controller.registerHandler(GET, "/{index}/_termlist", this);
+ controller.registerHandler(GET, "/_termlist/{field}", this);
+ controller.registerHandler(GET, "/{index}/_termlist/{field}", this);
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel) {
TermlistRequest termlistRequest = new TermlistRequest(RestActions.splitIndices(request.param("index")));
- try {
- client.execute(TermlistAction.INSTANCE, termlistRequest, new ActionListener<TermlistResponse>() {
+ termlistRequest.setField(request.param("field"));
+ client.execute(TermlistAction.INSTANCE, termlistRequest, new ActionListener<TermlistResponse>() {
- @Override
- public void onResponse(TermlistResponse response) {
- try {
- XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
- builder.startObject();
- builder.field("ok", true);
- buildBroadcastShardsHeader(builder, response);
- builder.array("terms", response.getTermlist().toArray());
- builder.endObject();
- channel.sendResponse(new XContentRestResponse(request, OK, builder));
- } catch (Exception e) {
- onFailure(e);
- }
+ @Override
+ public void onResponse(TermlistResponse response) {
+ try {
+ XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
+ builder.startObject();
+ builder.field("ok", true);
+ buildBroadcastShardsHeader(builder, response);
+ builder.array("terms", response.getTermlist().toArray());
+ builder.endObject();
+ channel.sendResponse(new XContentRestResponse(request, OK, builder));
+ } catch (Exception e) {
+ onFailure(e);
}
+ }
- @Override
- public void onFailure(Throwable e) {
- try {
- channel.sendResponse(new XContentThrowableRestResponse(request, e));
- } catch (IOException e1) {
- logger.error("Failed to send failure response", e1);
- }
+ @Override
+ public void onFailure(Throwable e) {
+ try {
+ channel.sendResponse(new XContentThrowableRestResponse(request, e));
+ } catch (IOException e1) {
+ logger.error("Failed to send failure response", e1);
}
- });
- } catch (Exception e) {
- try {
- channel.sendResponse(new XContentThrowableRestResponse(request, e));
- } catch (IOException e1) {
- logger.error("Failed to send failure response", e1);
}
- }
+ });
}
}

0 comments on commit b86639f

Please sign in to comment.