Skip to content

Commit

Permalink
es 8.3.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yuan committed Aug 14, 2022
1 parent 1648c7f commit 57f60cc
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 38 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.ansj</groupId>
<artifactId>elasticsearch-analysis-ansj</artifactId>
<version>7.17.5.0</version>
<version>8.3.3.0</version>
<description>elasticsearch analysis by ansj</description>
<name>elasticsearch-analysis-ansj</name>
<url>http://maven.nlpcn.org</url>
Expand All @@ -13,8 +13,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<runSuite>**/MainTestSuite.class</runSuite>
<elasticsearch.plugin.name>elasticsearch-analysis-ansj</elasticsearch.plugin.name>
<elasticsearch.plugin.java.version>1.8</elasticsearch.plugin.java.version>
<elasticsearch.version>7.17.5</elasticsearch.version>
<elasticsearch.plugin.java.version>17</elasticsearch.plugin.java.version>
<elasticsearch.version>8.3.3</elasticsearch.version>
<elasticsearch.plugin.classname>org.ansj.elasticsearch.plugin.AnalysisAnsjPlugin</elasticsearch.plugin.classname>
</properties>

Expand Down Expand Up @@ -66,8 +66,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${elasticsearch.plugin.java.version}</source>
<target>${elasticsearch.plugin.java.version}</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/ansj/elasticsearch/action/AnsjRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.single.shard.SingleShardRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.internal.Requests;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -61,7 +61,7 @@ public ActionRequestValidationException validate() {
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(path);
out.writeMap(args);
out.writeGenericMap(args);
out.writeBytesReference(source);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) {

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeMap(map);
out.writeGenericMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.ansj.library.StopLibrary;
import org.ansj.library.SynonymsLibrary;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.Table;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.ansj.library.DicLibrary;
import org.ansj.library.StopLibrary;
import org.ansj.library.SynonymsLibrary;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.Table;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/org/ansj/elasticsearch/cat/ChineseRestTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@
* under the License.
*/

import org.elasticsearch.core.Booleans;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.io.UTF8StreamWriter;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.BytesStream;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.rest.*;

import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
*/
Expand All @@ -42,15 +50,15 @@ public class ChineseRestTable {
public static RestResponse buildResponse(Table table, RestChannel channel) throws Exception {
RestRequest request = channel.request();
XContentType xContentType = XContentType
.fromMediaTypeOrFormat(request.param("format", request.header("Content-Type")));
.fromMediaType(request.param("format", request.header("Content-Type")));
if (xContentType != null) {
return buildXContentBuilder(table, channel);
}
return buildTextPlainResponse(table, channel);
}

public static RestResponse response(RestChannel channel, String text) throws IOException {
try (UTF8StreamWriter out = new UTF8StreamWriter(); BytesStreamOutput bytesOut = channel.bytesOutput()) {
try (UTF8StreamWriter out = new UTF8StreamWriter(); BytesStream bytesOut = channel.bytesOutput()) {
out.setOutput(bytesOut);
out.append(text);
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
Expand Down Expand Up @@ -89,7 +97,7 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
List<DisplayHeader> headers = buildDisplayHeaders(table, request);
int[] width = buildWidths(table, request, verbose, headers);

try (BytesStreamOutput bytesOut = channel.bytesOutput(); UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut)) {
try (BytesStream bytesOut = channel.bytesOutput(); UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut)) {
if (verbose) {
for (int col = 0; col < headers.size(); col++) {
DisplayHeader header = headers.get(col);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider;
Expand All @@ -20,8 +19,8 @@ public class AnsjAnalyzerProvider extends AbstractIndexAnalyzerProvider<AnsjAnal
private final AnsjAnalyzer analyzer;

@Inject
public AnsjAnalyzerProvider(IndexSettings indexSettings, @Assisted String name, @Assisted Settings settings) {
super(indexSettings, name, settings);
public AnsjAnalyzerProvider(IndexSettings indexSettings, String name, Settings settings) {
super(name, settings);

Settings settings2 = indexSettings.getSettings().getAsSettings("index.analysis.tokenizer." + name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenizerFactory;
Expand All @@ -37,9 +36,13 @@ public class AnsjTokenizerTokenizerFactory extends AbstractTokenizerFactory {

private static final Logger LOG = LogManager.getLogger();

private final IndexSettings indexSettings;

@Inject
public AnsjTokenizerTokenizerFactory(IndexSettings indexSettings, @Assisted String name, @Assisted Settings settings) {
public AnsjTokenizerTokenizerFactory(IndexSettings indexSettings, String name, Settings settings) {
super(indexSettings, settings, name);

this.indexSettings = indexSettings;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.logging.log4j.Logger;
import org.elasticsearch.SpecialPermission;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.nlpcn.commons.lang.tire.domain.SmartForest;
Expand Down Expand Up @@ -46,7 +45,6 @@ public class AnsjElasticConfigurator {

private final Environment env;

@Inject
public AnsjElasticConfigurator(Environment env) {
this.env = env;

Expand Down
23 changes: 12 additions & 11 deletions src/main/java/org/ansj/elasticsearch/plugin/AnalysisAnsjPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@
import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.index.analysis.AnalyzerProvider;
import org.elasticsearch.index.analysis.TokenizerFactory;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.AnalysisPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xcontent.NamedXContentRegistry;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -44,8 +52,8 @@ public class AnalysisAnsjPlugin extends Plugin implements AnalysisPlugin, Action
private static final Logger LOG = LogManager.getLogger();

@Override
public Collection<Module> createGuiceModules() {
return Collections.singletonList(new AnsjModule());
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<RepositoriesService> repositoriesServiceSupplier) {
return Collections.singletonList(new AnsjElasticConfigurator(environment));
}

@Override
Expand Down Expand Up @@ -87,11 +95,4 @@ public Map<String, AnalysisModule.AnalysisProvider<AnalyzerProvider<? extends An
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
return Arrays.asList(new RestAnsjAction(), new AnalyzerCatAction(), new AnsjCatAction());
}

private class AnsjModule extends AbstractModule {
@Override
protected void configure() {
bind(AnsjElasticConfigurator.class).asEagerSingleton();
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/ansj/elasticsearch/rest/RestAnsjAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import org.ansj.library.DicLibrary;
import org.ansj.library.StopLibrary;
import org.ansj.library.SynonymsLibrary;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -40,7 +39,7 @@ public List<Route> routes() {
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
AnsjRequest ansjRequest = new AnsjRequest();

ansjRequest.asMap().putAll(request.params());
Expand Down

0 comments on commit 57f60cc

Please sign in to comment.