Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Mar 19, 2024
1 parent a963742 commit 734875c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,31 @@ for example the `apoc.es.query`.
By default, the `<index param>` and `<id param>` will be populated as `_all`, while the `<id param>`, if not present, will be removed from the endpoint
| `<host>/<index param>/<type param>/<id param>_stats?<query param>`. Note that you only need to enter one of three values between `<index param>`,`<id param>` and `<type param>`, the others will eventually be excluded from the endpoint.

The type param is usually an underscore string indicating the type of the API, e.g. `_doc` or `_update` (while previously indicated https://www.elastic.co/guide/en/elasticsearch/reference/6.1/removal-of-types.html[the mapping types]).
This is to allow you to call, for example, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html[this API]
|===


For example, by using the `apoc.es.query`, we can execute:
For example, by using the `apoc.es.query`, we can execute a Search API:
[source, cypher]
----
CALL apoc.es.query(<$host>, <$index>, <$type>, 'q=name:Neo4j', null, { version: 'EIGHT' })
----

Updates a document in Elastic 8 via https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#docs-update[Update API]:

[source, cypher]
----
CALL apoc.es.put($host,'<indexName>','_doc','<idName>','refresh=true',{name: 'foo'}, {version: 'EIGHT'})
----

Call a https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#indices-create-index[Create Index API] in elastic 8:

[source, cypher]
----
CALL apoc.es.put($host,'<indexName>', null, null, null, null, { version: 'EIGHT' })
----


=== Results

Expand Down
22 changes: 2 additions & 20 deletions extended/src/main/java/apoc/es/ElasticSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public class ElasticSearch {

@Context
public URLAccessChecker urlAccessChecker;
// todo - in elastic 8 mettere List.of(...) --> object .filter( Objects::nonNull ) . join('/')






/**
* @param payload
* @return
Expand All @@ -43,11 +38,6 @@ protected String toPayload(Object payload) {
return payload.toString();
}

/**
* TODO: if config version is 8, then type become _doc in PUT and POST, and nothing in GET
*
*/

@Procedure
@Description("apoc.es.stats(host-url-Key,$config) - elastic search statistics")
public Stream<MapResult> stats(@Name("host") String hostOrKey, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
Expand All @@ -61,17 +51,15 @@ public Stream<MapResult> stats(@Name("host") String hostOrKey, @Name(value = "co
public Stream<MapResult> get(@Name("host") String hostOrKey, @Name("index") String index, @Name("type") String type, @Name("id") String id, @Name("query") Object query, @Name("payload") Object payload,
@Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
ElasticSearchConfig conf = new ElasticSearchConfig(config);
// String queryUrl2 = ElasticSearchHandler.Version.DEFAULT.get().getQueryUrl(hostOrKey, index, "mytype", id, query).replace("mytype/", "");
String queryUrl = conf.getVersion().getQueryUrl(hostOrKey, index, type, id, query);//.replace("mytype/", "");
return loadJsonStream(queryUrl, conf, toPayload(payload));
}
// http://localhost:53229/test-index/41e43611-77c7-4fa2-93d9-948ec376e3b4

@Procedure
@Description("apoc.es.query(host-or-port,index-or-null,type-or-null,query-or-null,payload-or-null,$config) yield value - perform a SEARCH operation on elastic search")
public Stream<MapResult> query(@Name("host") String hostOrKey, @Name("index") String index, @Name("type") String type, @Name("query") Object query, @Name("payload") Object payload,
@Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
ElasticSearchConfig conf = new ElasticSearchConfig(config);
// String searchQueryUrl = conf.getVersion().getSearchQueryUrl(hostOrKey, index, "mytype", query).replace("mytype/", "");
String searchQueryUrl = conf.getVersion().getSearchQueryUrl(hostOrKey, index, type, query);//.replace("mytype/", "");

return loadJsonStream(searchQueryUrl, conf, toPayload(payload));
Expand Down Expand Up @@ -105,11 +93,9 @@ public Stream<MapResult> post(@Name("host") String hostOrKey, @Name("index") Str
}
ElasticSearchConfig conf = new ElasticSearchConfig(config, "POST");
String queryUrl = conf.getVersion().getQueryUrl(hostOrKey, index, type, null, query);
// String queryUrl = conf.getVersion().getQueryUrl(hostOrKey, index, "_doc", null, query);
return loadJsonStream(queryUrl, conf, toPayload(payload));
}

// todo - potrei chiamarlo type-or-resource-or-null
@Procedure
@Description("apoc.es.put(host-or-port,index-or-null,type-or-null,id-or-null,query-or-null,payload-or-null,$config) yield value - perform a PUT operation on elastic search")
public Stream<MapResult> put(@Name("host") String hostOrKey, @Name("index") String index, @Name("type") String type, @Name("id") String id, @Name("query") Object query,
Expand All @@ -119,13 +105,9 @@ public Stream<MapResult> put(@Name("host") String hostOrKey, @Name("index") Stri
{
payload = Collections.emptyMap();
}
// String s = getElasticSearchUrl(hostOrKey) + "/" + index + "/" + id + "/_all?" + query;

ElasticSearchConfig conf = new ElasticSearchConfig(config, "PUT");
String s = conf.getVersion().getQueryUrl(hostOrKey, index, type, id, query);
// String s = conf.getVersion().getQueryUrl(hostOrKey, index, "_doc", id, query);

// String s = ElasticSearchHandler.Version.DEFAULT.get().getQueryUrl(hostOrKey, index, "_doc", id, query);
return loadJsonStream(s, conf, toPayload(payload));
}

Expand Down
34 changes: 7 additions & 27 deletions extended/src/main/java/apoc/es/ElasticSearchHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class ElasticSearchHandler {
* With this pattern we can match both key:value params and key=value params
*/
private final static Pattern KEY_VALUE = Pattern.compile("(.*)(:|=)(.*)");
// todo - remove from here..

protected String getElasticSearchUrl(String hostOrKey) {
return new UrlResolver("http", "localhost", 9200).getUrl("es", hostOrKey);
}
Expand Down Expand Up @@ -58,11 +58,9 @@ protected String getSearchQueryUrl(String hostOrKey, String index, String type,
return getElasticSearchUrl(hostOrKey) + formatSearchQueryUrl(index, type, query);
}

// protected abstract String getQueryUrl(String hostOrKey, String index, String type, String id, Object query);
//
//
// protected abstract String getSearchQueryUrl(String hostOrKey, String index, String type, Object query);

/**
* Format the Search API url template according to the parameters.
*/
protected abstract String formatSearchQueryUrl(String index, String type, Object query);

/**
Expand All @@ -87,35 +85,19 @@ public ElasticSearchHandler get() {

static class Eight extends ElasticSearchHandler {

// @Override
// protected String getQueryUrl(String hostOrKey, String index, String type, String id, Object query) {
// return null;
// }
//
// @Override
// protected String getSearchQueryUrl(String hostOrKey, String index, String type, Object query) {
// return null;
// }

@Override
protected String formatSearchQueryUrl(String index, String type, Object query) {
// todo - _all è necessario in elastic 8??

String queryUrl = String.format( "/%s/_search?%s",
index == null ? "_all" : index,
// type == null ? "_all" : type,
toQueryParams(query));

return removeTerminalQuote(queryUrl);
}



@Override
protected String formatQueryUrl(String index, String type, String id, Object query) {
// type = Objects.requireNonNullElse(type, "_doc");

// todo - stream.of() accetta null???

String queryUrl = Arrays.asList(index, type, id)
.stream()
.filter(StringUtils::isNotBlank)
Expand All @@ -132,10 +114,8 @@ protected String formatQueryUrl(String index, String type, String id, Object que

static class Default extends ElasticSearchHandler {

private final static String fullQueryTemplate = "/%s/%s/%s?%s";

// /{index}/{type}/_search?{query}
private final static String fullQuerySearchTemplate = "/%s/%s/_search?%s";
private final String fullQueryTemplate = "/%s/%s/%s?%s";
private final String fullQuerySearchTemplate = "/%s/%s/_search?%s";

@Override
protected String formatSearchQueryUrl(String index, String type, Object query) {
Expand Down
177 changes: 0 additions & 177 deletions extended/src/test/java/apoc/es/ElasticSearchTestToDelete.java

This file was deleted.

Loading

0 comments on commit 734875c

Please sign in to comment.