Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Versions

Dynamic Word Delimiter Plugin | ElasticSearch | Branch |
------------------------------|---------------|--------|
8.17.0.1 | 8.17.0 | 8.17.0 |
7.8.0.1 | 7.8.0 | 7.8.0 |
7.7.0.2 | 7.7.0 | 7.7.0 |
7.7.0.1 | 7.7.0 | 7.7.0 |
Expand Down
39 changes: 23 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>gr.skroutz</groupId>
<artifactId>elasticsearch-dynamic-word-delimiter</artifactId>
<version>7.8.0.2-SNAPSHOT</version>
<version>8.17.0.1-SNAPSHOT</version>
<description>Dynamic word delimiter token filter for ElasticSearch</description>
<inceptionYear>2015</inceptionYear>

Expand All @@ -19,10 +19,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<elasticsearch.version>7.8.0</elasticsearch.version>
<elasticsearch.version>8.17.0</elasticsearch.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lucene.version>8.5.1</lucene.version>
</properties>

<developers>
Expand Down Expand Up @@ -67,24 +66,23 @@
</parent>

<dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-test-framework</artifactId>
<version>${lucene.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elasticsearch.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.test</groupId>
<artifactId>framework</artifactId>
Expand All @@ -103,13 +101,22 @@
<version>2.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<filtering>false</filtering>
<includes>
<include>plugin-descriptor.properties</include>
<include>plugin-security.policy</include>
</includes>
</resource>
</resources>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.elasticsearch.action.support;

import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchResponse;
import org.apache.logging.log4j.Logger;

import java.util.HashSet;
import java.util.Set;

public class WordDelimiterActionListener implements ActionListener<SearchResponse> {
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.logging.Loggers;

import com.fasterxml.jackson.databind.JsonNode;

public class WordDelimiterActionListener implements ActionListener<JsonNode> {

private static WordDelimiterActionListener instance = null;
private static final Logger logger = Loggers.getLogger(
Expand All @@ -20,24 +20,34 @@ public class WordDelimiterActionListener implements ActionListener<SearchRespons
private Set<String> protectedWords;

protected WordDelimiterActionListener() {
protectedWords = new HashSet<String>();
protectedWords = new HashSet<>();
}

public void onResponse(SearchResponse response) {
SearchHit[] hits = response.getHits().getHits();
Set<String> localProtectedWords = new HashSet<String>();

String word;
for (SearchHit hit : hits) {
word = hit.getSourceAsMap().get("word").toString();
localProtectedWords.add(word);
private static HashSet<String> parseFromJsonNode(JsonNode response) {
HashSet<String> protectedWords = new HashSet<>();
if (response != null && response.has("hits") && response.path("hits").has("hits")) {
for (JsonNode hit : response.path("hits").path("hits")) {
JsonNode source = hit.path("_source");
if (source != null && source.has("word")) {
String word = source.path("word").asText();
logger.error("Found protected word: " + word);
protectedWords.add(word);
}
}
}
return protectedWords;
}

@Override
public void onResponse(JsonNode response) {
logger.error("Updating protected words in memory");

protectedWords = localProtectedWords;
protectedWords = parseFromJsonNode(response);
}

@Override
public void onFailure(Exception e) {
logger.error("`onFailure` called");
logger.error(e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.elasticsearch.module;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;

import com.fasterxml.jackson.databind.JsonNode;

import co.elastic.clients.json.jackson.JacksonJsonpMapper;

public class CustomElasticsearchAsyncClient {
private final RestClient restClient;
private final JacksonJsonpMapper jsonpMapper;

public CustomElasticsearchAsyncClient(RestClient restClient, JacksonJsonpMapper jsonpMapper) {
this.restClient = restClient;
this.jsonpMapper = jsonpMapper;
}

public CompletableFuture<Boolean> indicesExists(String indexName) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
Request request = new Request("HEAD", "/" + indexName);
restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
future.complete(response.getStatusLine().getStatusCode() == 200);
}

@Override
public void onFailure(Exception exception) {
if (exception instanceof ResponseException) {
ResponseException e = (ResponseException) exception;
if (e.getResponse().getStatusLine().getStatusCode() == 404) {
future.complete(false); // Index not found
return;
}
}
future.completeExceptionally(exception);
}
});
return future;
}

public CompletableFuture<JsonNode> searchMatchAll(String indexName) {
CompletableFuture<JsonNode> future = new CompletableFuture<>();
Request request = new Request("GET", "/" + indexName + "/_search");
request.setJsonEntity("{\"query\": {\"match_all\": {}}}");

restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
try {
String jsonResponse = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = jsonpMapper.objectMapper().readTree(jsonResponse);
future.complete(jsonNode);
} catch (IOException e) {
future.completeExceptionally(e);
}
}

@Override
public void onFailure(Exception exception) {
future.completeExceptionally(exception);
}
});
return future;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.elasticsearch.module;

import java.io.IOException;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;

import com.fasterxml.jackson.databind.JsonNode;

import co.elastic.clients.json.jackson.JacksonJsonpMapper;

public class CustomElasticsearchClient {
private final RestClient restClient;
private final JacksonJsonpMapper jsonpMapper;

public CustomElasticsearchClient(RestClient restClient, JacksonJsonpMapper jsonpMapper) {
this.restClient = restClient;
this.jsonpMapper = jsonpMapper;
}

public boolean indicesExists(String indexName) throws IOException {
Request request = new Request("HEAD", "/" + indexName);

try {
Response response = restClient.performRequest(request);
return response.getStatusLine().getStatusCode() == 200;
} catch (ResponseException e) {
if (e.getResponse().getStatusLine().getStatusCode() == 404) {
return false;
}
throw e;
}
}

public JsonNode searchMatchAll(String indexName) throws IOException {
Request request = new Request("GET", "/" + indexName + "/_search");
request.setJsonEntity("{\"query\": {\"match_all\": {}}}");

Response response = restClient.performRequest(request);

String jsonResponse = EntityUtils.toString(response.getEntity());

return jsonpMapper.objectMapper().readTree(jsonResponse);
}
}
Loading