From 32761b9165f4f70138644601c9ed972093f77813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Wed, 26 Nov 2014 00:14:30 +0100 Subject: [PATCH] 1.4.0.2 --- README.md | 13 ++- pom.xml | 16 +++- .../action/langdetect/LangdetectResponse.java | 12 +++ .../langdetect/TransportLangdetectAction.java | 4 +- .../profile/LangdetectProfileAction.java | 26 +++++ .../profile/LangdetectProfileRequest.java | 36 +++++++ .../LangdetectProfileRequestBuilder.java | 23 +++++ .../profile/LangdetectProfileResponse.java | 27 ++++++ .../TransportLangdetectProfileAction.java | 32 +++++++ .../langdetect/LangdetectIndexModule.java | 13 +++ .../module/langdetect/LangdetectModule.java | 3 +- .../module/langdetect/LangdetectService.java | 63 ++++++------- .../plugin/langdetect/LangdetectPlugin.java | 13 +++ .../langdetect/RestLangdetectAction.java | 6 +- .../RestLangdetectProfileAction.java | 32 +++++++ .../plugin/LangDetectActionTest.java | 35 +++++++ .../plugin/helper/AbstractNodeTestHelper.java | 94 +++++++++++++++++++ 17 files changed, 401 insertions(+), 47 deletions(-) create mode 100644 src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileAction.java create mode 100644 src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequest.java create mode 100644 src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequestBuilder.java create mode 100644 src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileResponse.java create mode 100644 src/main/java/org/xbib/elasticsearch/action/langdetect/profile/TransportLangdetectProfileAction.java create mode 100644 src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectIndexModule.java create mode 100644 src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectProfileAction.java create mode 100644 src/test/java/org/oxbib/elasticsearch/plugin/LangDetectActionTest.java create mode 100644 src/test/java/org/oxbib/elasticsearch/plugin/helper/AbstractNodeTestHelper.java diff --git a/README.md b/README.md index f4407f2..52a7718 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ zh-tw | Elasticsearch | Plugin | Release date | | -------------- | -------------- | ------------ | +| 1.4.0 | 1.4.0.2 | Nov 26, 2014 | | 1.4.0 | 1.4.0.1 | Nov 20, 2014 | | 1.4.0 | 1.4.0.0 | Nov 14, 2014 | | 1.3.1 | 1.3.0.0 | Jul 30, 2014 | @@ -91,7 +92,7 @@ zh-tw ## Installation - ./bin/plugin -install langdetect -url http://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-langdetect/1.4.0.1/elasticsearch-langdetect-1.4.0.1-plugin.zip + ./bin/plugin -install langdetect -url http://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-langdetect/1.4.0.2/elasticsearch-langdetect-1.4.0.2-plugin.zip Do not forget to restart the node after installing. @@ -99,6 +100,7 @@ Do not forget to restart the node after installing. | File | SHA1 | | --------------------------------------------- | -----------------------------------------| +| elasticsearch-langdetect-1.4.0.2-plugin.zip | 60919142f12d8b54e519f6cf97862e842c6b3bb8 | | elasticsearch-langdetect-1.4.0.1-plugin.zip | a7ab2401ae68789bf1e5841427ce40440c903da4 | | elasticsearch-langdetect-1.4.0.0-plugin.zip | f95361fa1a81b2681e2e9002b03ca6aad57f3012 | | elasticsearch-langdetect-1.3.0.0-plugin.zip | e2dd56c72f19cec861141becd8beb18d7bb26ee6 | @@ -246,7 +248,7 @@ All feedback is welcome! If you find issues, please post them at [Github](https: curl -XPOST 'localhost:9200/_langdetect?pretty' -d 'This is a test' { - "ok" : true, + "profile" : "/langdetect/", "languages" : [ { "language" : "en", "probability" : 0.9999971603535163 @@ -255,7 +257,7 @@ All feedback is welcome! If you find issues, please post them at [Github](https: curl -XPOST 'localhost:9200/_langdetect?pretty' -d 'Das ist ein Test' { - "ok" : true, + "profile" : "/langdetect/", "languages" : [ { "language" : "de", "probability" : 0.9999993070517024 @@ -264,7 +266,7 @@ All feedback is welcome! If you find issues, please post them at [Github](https: curl -XPOST 'localhost:9200/_langdetect?pretty' -d 'Datt isse ne test' { - "ok" : true, + "profile" : "/langdetect/", "languages" : [ { "language" : "no", "probability" : 0.5714251911820175 @@ -277,6 +279,9 @@ All feedback is welcome! If you find issues, please post them at [Github](https: } ] } +## Change profile of language detection to "shart text" profile + + curl -XPOST 'localhost:9200/_langdetect/profile?profile=/langdetect/short-text/' # Credits diff --git a/pom.xml b/pom.xml index e3089a5..11622ea 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.xbib.elasticsearch.plugin elasticsearch-langdetect - 1.4.0.1 + 1.4.0.2 jar @@ -99,6 +99,20 @@ test + + org.apache.logging.log4j + log4j-slf4j-impl + 2.0 + test + + + + org.apache.logging.log4j + log4j-core + 2.0 + test + + diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/LangdetectResponse.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/LangdetectResponse.java index bffe5a0..0328859 100644 --- a/src/main/java/org/xbib/elasticsearch/action/langdetect/LangdetectResponse.java +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/LangdetectResponse.java @@ -13,11 +13,22 @@ public class LangdetectResponse extends ActionResponse implements StatusToXContent { + private String profile; + private List languages; public LangdetectResponse() { } + public LangdetectResponse setProfile(String profile) { + this.profile = profile; + return this; + } + + public String getProfile() { + return profile; + } + public LangdetectResponse setLanguages(List languages) { this.languages = languages; return this; @@ -29,6 +40,7 @@ public List getLanguages() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field("profile", profile); builder.startArray("languages"); for (Language lang : languages) { builder.startObject().field("language", lang.getLanguage()) diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/TransportLangdetectAction.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/TransportLangdetectAction.java index 4ae64cc..ac6c004 100644 --- a/src/main/java/org/xbib/elasticsearch/action/langdetect/TransportLangdetectAction.java +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/TransportLangdetectAction.java @@ -21,15 +21,13 @@ public TransportLangdetectAction(Settings settings, ThreadPool threadPool, ActionFilters actionFilters, LangdetectService service) { super(settings, LangdetectAction.NAME, threadPool, actionFilters); this.service = service; - // start the service here - this.service.start(); } @Override protected void doExecute(LangdetectRequest request, ActionListener listener) { try { List langs = service.detectAll(request.getText()); - listener.onResponse(new LangdetectResponse().setLanguages(langs)); + listener.onResponse(new LangdetectResponse().setLanguages(langs).setProfile(service.getProfile())); } catch (LanguageDetectionException e) { listener.onFailure(e); } diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileAction.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileAction.java new file mode 100644 index 0000000..433b2db --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileAction.java @@ -0,0 +1,26 @@ +package org.xbib.elasticsearch.action.langdetect.profile; + +import org.elasticsearch.action.admin.indices.IndicesAction; +import org.elasticsearch.client.IndicesAdminClient; + +public class LangdetectProfileAction extends IndicesAction { + + public static final String NAME = "langdetect.profile"; + + public static final LangdetectProfileAction INSTANCE = new LangdetectProfileAction(); + + private LangdetectProfileAction() { + super(NAME); + } + + @Override + public LangdetectProfileRequestBuilder newRequestBuilder(IndicesAdminClient client) { + return new LangdetectProfileRequestBuilder(client); + } + + @Override + public LangdetectProfileResponse newResponse() { + return new LangdetectProfileResponse(); + } + +} diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequest.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequest.java new file mode 100644 index 0000000..50ca8ae --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequest.java @@ -0,0 +1,36 @@ +package org.xbib.elasticsearch.action.langdetect.profile; + +import org.elasticsearch.action.support.single.custom.SingleCustomOperationRequest; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; + +import java.io.IOException; + +public class LangdetectProfileRequest extends SingleCustomOperationRequest { + + private String profile; + + public LangdetectProfileRequest() { + } + + public LangdetectProfileRequest setProfile(String profile) { + this.profile = profile; + return this; + } + + public String getProfile() { + return profile; + } + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + profile = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(profile); + } +} diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequestBuilder.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequestBuilder.java new file mode 100644 index 0000000..cef0d4b --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileRequestBuilder.java @@ -0,0 +1,23 @@ +package org.xbib.elasticsearch.action.langdetect.profile; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.single.custom.SingleCustomOperationRequestBuilder; +import org.elasticsearch.client.IndicesAdminClient; + +public class LangdetectProfileRequestBuilder extends SingleCustomOperationRequestBuilder { + + public LangdetectProfileRequestBuilder(IndicesAdminClient client) { + super(client, new LangdetectProfileRequest()); + } + + public LangdetectProfileRequestBuilder setProfile(String string) { + request.setProfile(string); + return this; + } + + @Override + protected void doExecute(ActionListener listener) { + client.execute(LangdetectProfileAction.INSTANCE, request, listener); + } + +} diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileResponse.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileResponse.java new file mode 100644 index 0000000..a79cb21 --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/LangdetectProfileResponse.java @@ -0,0 +1,27 @@ +package org.xbib.elasticsearch.action.langdetect.profile; + +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.xcontent.StatusToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.rest.RestStatus; + +import java.io.IOException; + +import static org.elasticsearch.rest.RestStatus.OK; + +public class LangdetectProfileResponse extends ActionResponse implements StatusToXContent { + + public LangdetectProfileResponse() { + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field("ok", true); + return builder; + } + + @Override + public RestStatus status() { + return OK; + } +} diff --git a/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/TransportLangdetectProfileAction.java b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/TransportLangdetectProfileAction.java new file mode 100644 index 0000000..bab7d03 --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/action/langdetect/profile/TransportLangdetectProfileAction.java @@ -0,0 +1,32 @@ +package org.xbib.elasticsearch.action.langdetect.profile; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.TransportAction; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.threadpool.ThreadPool; +import org.xbib.elasticsearch.index.analysis.langdetect.LanguageDetectionException; +import org.xbib.elasticsearch.module.langdetect.LangdetectService; + +public class TransportLangdetectProfileAction extends TransportAction { + + private final LangdetectService service; + + @Inject + public TransportLangdetectProfileAction(Settings settings, ThreadPool threadPool, + ActionFilters actionFilters, LangdetectService service) { + super(settings, LangdetectProfileAction.NAME, threadPool, actionFilters); + this.service = service; + } + + @Override + protected void doExecute(LangdetectProfileRequest request, ActionListener listener) { + try { + service.setProfile(request.getProfile()); + listener.onResponse(new LangdetectProfileResponse()); + } catch (LanguageDetectionException e) { + listener.onFailure(e); + } + } +} diff --git a/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectIndexModule.java b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectIndexModule.java new file mode 100644 index 0000000..ccf23ee --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectIndexModule.java @@ -0,0 +1,13 @@ +package org.xbib.elasticsearch.module.langdetect; + +import org.elasticsearch.common.inject.Binder; +import org.elasticsearch.common.inject.Module; + +public class LangdetectIndexModule implements Module { + + @Override + public void configure(Binder binder) { + binder.bind(RegisterLangdetectType.class).asEagerSingleton(); + } + +} diff --git a/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectModule.java b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectModule.java index b122067..688cada 100644 --- a/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectModule.java +++ b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectModule.java @@ -7,6 +7,7 @@ public class LangdetectModule implements Module { @Override public void configure(Binder binder) { - binder.bind(RegisterLangdetectType.class).asEagerSingleton(); + binder.bind(LangdetectService.class).asEagerSingleton(); } + } diff --git a/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectService.java b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectService.java index 18ff5ac..6d4763c 100644 --- a/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectService.java +++ b/src/main/java/org/xbib/elasticsearch/module/langdetect/LangdetectService.java @@ -4,6 +4,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.xbib.elasticsearch.index.analysis.langdetect.LangProfile; @@ -23,6 +25,8 @@ public class LangdetectService extends AbstractLifecycleComponent { + private final static ESLogger logger = ESLoggerFactory.getLogger(LangdetectService.class.getName()); + private final static Pattern word = Pattern.compile("[\\P{IsWord}]", Pattern.UNICODE_CHARACTER_CLASS); private final static String[] DEFAULT_LANGUAGES = new String[] { @@ -116,16 +120,7 @@ public LangdetectService(Settings settings) { protected void doStart() throws ElasticsearchException { this.profile = settings.get("profile", "/langdetect/"); load(settings); - this.priorMap = null; - this.n_trial = settings.getAsInt("number_of_trials", 7); - this.alpha = settings.getAsDouble("alpha", 0.5); - this.alpha_width = settings.getAsDouble("alpha_width", 0.05); - this.iteration_limit = settings.getAsInt("iteration_limit", 10000); - this.prob_threshold = settings.getAsDouble("prob_threshold", 0.1); - this.conv_threshold = settings.getAsDouble("conv_threshold", 0.99999); - this.base_freq = settings.getAsInt("base_freq", 10000); - this.filterPattern = settings.get("pattern") != null ? - Pattern.compile(settings.get("pattern"),Pattern.UNICODE_CHARACTER_CLASS) : null; + init(); } @Override @@ -179,6 +174,19 @@ private void load(Settings settings) { } } + private void init() { + this.priorMap = null; + this.n_trial = settings.getAsInt("number_of_trials", 7); + this.alpha = settings.getAsDouble("alpha", 0.5); + this.alpha_width = settings.getAsDouble("alpha_width", 0.05); + this.iteration_limit = settings.getAsInt("iteration_limit", 10000); + this.prob_threshold = settings.getAsDouble("prob_threshold", 0.1); + this.conv_threshold = settings.getAsDouble("conv_threshold", 0.99999); + this.base_freq = settings.getAsInt("base_freq", 10000); + this.filterPattern = settings.get("pattern") != null ? + Pattern.compile(settings.get("pattern"),Pattern.UNICODE_CHARACTER_CLASS) : null; + } + public void loadProfileFromResource(String resource, String profile, int index, int langsize) throws IOException { InputStream in = getClass().getResourceAsStream(profile + resource); if (in == null) { @@ -207,32 +215,15 @@ public void addProfile(LangProfile profile, int index, int langsize) throws IOEx } } - /** - * Set prior information about language probabilities. - * - * @param priorMap the priorMap to set - * @throws org.xbib.elasticsearch.index.analysis.langdetect.LanguageDetectionException - */ - public void setPriorMap(HashMap priorMap) throws LanguageDetectionException { - this.priorMap = new double[langlist.size()]; - double sump = 0; - for (int i = 0; i < this.priorMap.length; ++i) { - String lang = langlist.get(i); - if (priorMap.containsKey(lang)) { - double p = priorMap.get(lang); - if (p < 0) { - throw new LanguageDetectionException("Prior probability must be non-negative"); - } - this.priorMap[i] = p; - sump += p; - } - } - if (sump <= 0) { - throw new LanguageDetectionException("More one of prior probability must be non-zero"); - } - for (int i = 0; i < this.priorMap.length; ++i) { - this.priorMap[i] /= sump; - } + public void setProfile(String profile) throws LanguageDetectionException { + this.profile = profile; + langlist.clear(); + load(settings); + init(); + } + + public String getProfile() { + return profile; } public List detectAll(String text) throws LanguageDetectionException { diff --git a/src/main/java/org/xbib/elasticsearch/plugin/langdetect/LangdetectPlugin.java b/src/main/java/org/xbib/elasticsearch/plugin/langdetect/LangdetectPlugin.java index a663655..75b8a05 100644 --- a/src/main/java/org/xbib/elasticsearch/plugin/langdetect/LangdetectPlugin.java +++ b/src/main/java/org/xbib/elasticsearch/plugin/langdetect/LangdetectPlugin.java @@ -7,9 +7,13 @@ import org.elasticsearch.rest.RestModule; import org.xbib.elasticsearch.action.langdetect.LangdetectAction; import org.xbib.elasticsearch.action.langdetect.TransportLangdetectAction; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileAction; +import org.xbib.elasticsearch.action.langdetect.profile.TransportLangdetectProfileAction; +import org.xbib.elasticsearch.module.langdetect.LangdetectIndexModule; import org.xbib.elasticsearch.module.langdetect.LangdetectModule; import org.xbib.elasticsearch.module.langdetect.LangdetectService; import org.xbib.elasticsearch.rest.action.langdetect.RestLangdetectAction; +import org.xbib.elasticsearch.rest.action.langdetect.RestLangdetectProfileAction; import java.util.Collection; @@ -31,10 +35,12 @@ public String description() { public void onModule(RestModule module) { module.addRestAction(RestLangdetectAction.class); + module.addRestAction(RestLangdetectProfileAction.class); } public void onModule(ActionModule module) { module.registerAction(LangdetectAction.INSTANCE, TransportLangdetectAction.class); + module.registerAction(LangdetectProfileAction.INSTANCE, TransportLangdetectProfileAction.class); } @SuppressWarnings("rawtypes") @@ -47,6 +53,13 @@ public Collection> services() { @Override public Collection> indexModules() { + Collection> modules = newArrayList(); + modules.add(LangdetectIndexModule.class); + return modules; + } + + @Override + public Collection> modules() { Collection> modules = newArrayList(); modules.add(LangdetectModule.class); return modules; diff --git a/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectAction.java b/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectAction.java index f0dcd38..ea21153 100644 --- a/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectAction.java +++ b/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectAction.java @@ -24,7 +24,9 @@ public RestLangdetectAction(Settings settings, Client client, RestController con @Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { - LangdetectRequest langdetectRequest = new LangdetectRequest().setText(request.content().toUtf8()); - client.admin().indices().execute(LangdetectAction.INSTANCE, langdetectRequest, new RestStatusToXContentListener(channel)); + LangdetectRequest langdetectRequest = new LangdetectRequest() + .setText(request.content().toUtf8()); + client.admin().indices().execute(LangdetectAction.INSTANCE, langdetectRequest, + new RestStatusToXContentListener(channel)); } } \ No newline at end of file diff --git a/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectProfileAction.java b/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectProfileAction.java new file mode 100644 index 0000000..b4abaa0 --- /dev/null +++ b/src/main/java/org/xbib/elasticsearch/rest/action/langdetect/RestLangdetectProfileAction.java @@ -0,0 +1,32 @@ +package org.xbib.elasticsearch.rest.action.langdetect; + +import org.elasticsearch.client.Client; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.support.RestStatusToXContentListener; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileAction; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileRequest; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileResponse; + +import static org.elasticsearch.rest.RestRequest.Method.POST; + +public class RestLangdetectProfileAction extends BaseRestHandler { + + @Inject + public RestLangdetectProfileAction(Settings settings, Client client, RestController controller) { + super(settings, controller, client); + controller.registerHandler(POST, "/_langdetect/profile", this); + } + + @Override + public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { + LangdetectProfileRequest langdetectRequest = new LangdetectProfileRequest() + .setProfile(request.param("profile", "/langdetect/")); + client.admin().indices().execute(LangdetectProfileAction.INSTANCE, langdetectRequest, + new RestStatusToXContentListener(channel)); + } +} \ No newline at end of file diff --git a/src/test/java/org/oxbib/elasticsearch/plugin/LangDetectActionTest.java b/src/test/java/org/oxbib/elasticsearch/plugin/LangDetectActionTest.java new file mode 100644 index 0000000..bf331ae --- /dev/null +++ b/src/test/java/org/oxbib/elasticsearch/plugin/LangDetectActionTest.java @@ -0,0 +1,35 @@ +package org.oxbib.elasticsearch.plugin; + +import org.junit.Test; +import org.oxbib.elasticsearch.plugin.helper.AbstractNodeTestHelper; +import org.xbib.elasticsearch.action.langdetect.LangdetectRequestBuilder; +import org.xbib.elasticsearch.action.langdetect.LangdetectResponse; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileRequestBuilder; +import org.xbib.elasticsearch.action.langdetect.profile.LangdetectProfileResponse; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class LangDetectActionTest extends AbstractNodeTestHelper { + + @Test + public void testLangDetectProfile() throws Exception { + LangdetectRequestBuilder langdetectRequestBuilder = + new LangdetectRequestBuilder(client("1").admin().indices()).setText("hello this is a test"); + LangdetectResponse response = langdetectRequestBuilder.execute().actionGet(); + assertEquals("en", response.getLanguages().get(0).getLanguage()); + assertEquals("/langdetect/", response.getProfile()); + + LangdetectProfileRequestBuilder langdetectProfileRequestBuilder = + new LangdetectProfileRequestBuilder(client("1").admin().indices()) + .setProfile("/langdetect/short-text/"); + LangdetectProfileResponse langdetectProfileResponse = langdetectProfileRequestBuilder.execute().actionGet(); + assertNotNull(langdetectProfileResponse); + + langdetectRequestBuilder = + new LangdetectRequestBuilder(client("1").admin().indices()).setText("hello this is a test"); + response = langdetectRequestBuilder.execute().actionGet(); + assertEquals("en", response.getLanguages().get(0).getLanguage()); + assertEquals("/langdetect/short-text/", response.getProfile()); + } +} diff --git a/src/test/java/org/oxbib/elasticsearch/plugin/helper/AbstractNodeTestHelper.java b/src/test/java/org/oxbib/elasticsearch/plugin/helper/AbstractNodeTestHelper.java new file mode 100644 index 0000000..0c8eae2 --- /dev/null +++ b/src/test/java/org/oxbib/elasticsearch/plugin/helper/AbstractNodeTestHelper.java @@ -0,0 +1,94 @@ +package org.oxbib.elasticsearch.plugin.helper; + +import org.elasticsearch.client.Client; +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.logging.ESLoggerFactory; +import org.elasticsearch.common.network.NetworkUtils; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.elasticsearch.node.Node; +import org.junit.After; +import org.junit.Before; + +import java.util.Map; + +import static org.elasticsearch.common.collect.Maps.newHashMap; +import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS; +import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; +import static org.elasticsearch.node.NodeBuilder.nodeBuilder; + +public abstract class AbstractNodeTestHelper { + + protected final static ESLogger logger = ESLoggerFactory.getLogger("test"); + + protected final String CLUSTER = "test-cluster-" + NetworkUtils.getLocalAddress().getHostName(); + + protected Settings defaultSettings = ImmutableSettings + .settingsBuilder() + .put("cluster.name", CLUSTER) + .build(); + + private Map nodes = newHashMap(); + + private Map clients = newHashMap(); + + private Map addresses = newHashMap(); + + @Before + public void setUp() throws Exception { + startNode("1"); + startNode("2"); // two nodes, for testing state + } + + @After + public void closeIndices() { + closeAllNodes(); + } + + public Node startNode(String id) { + return buildNode(id).start(); + } + + public Node buildNode(String id) { + return buildNode(id, EMPTY_SETTINGS); + } + + public Node buildNode(String id, Settings settings) { + String settingsSource = getClass().getName().replace('.', '/') + ".yml"; + Settings finalSettings = settingsBuilder() + .loadFromClasspath(settingsSource) + .put(defaultSettings) + .put(settings) + .put("name", id) + .put("gateway.type", "none") + .put("cluster.routing.schedule", "50ms") + .put("index.store.type", "memory") + .put("http.enabled", false) + .put("discovery.zen.multicast.enabled", false) + .build(); + Node node = nodeBuilder().settings(finalSettings).local(true).build(); + Client client = node.client(); + nodes.put(id, node); + clients.put(id, client); + return node; + } + + public Client client(String id) { + return clients.get(id); + } + + public void closeAllNodes() { + for (Client client : clients.values()) { + client.close(); + } + clients.clear(); + for (Node node : nodes.values()) { + if (node != null) { + node.close(); + } + } + nodes.clear(); + } + +}