diff --git a/.gitignore b/.gitignore
index d852dbc8..a80fdc7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,4 +18,5 @@ elasticsearch-sql.iml
.idea
src/_site/vendor/codemirror/mode/jinja2/.goutputstream-*
src/_site/node_modules
+src/site-server/node_modules
*~
diff --git a/.travis.yml b/.travis.yml
index 9ec76acc..272d917d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,18 +2,31 @@ language: java
jdk:
- oraclejdk8
- - oraclejdk7
-
-install:
- - mvn install -DskipTests=true -Dmaven.javadoc.skip=true -Dgpg.skip=true
+addons:
+ apt:
+ packages:
+ - oracle-java8-installer
-script:
- - wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.4.1/elasticsearch-2.4.1.deb && sudo dpkg -i --force-confnew elasticsearch-2.4.1.deb
+addons:
+ apt:
+ packages:
+ - oracle-java8-installer
+
+before_install:
+ # update to java 8
+ - sudo update-java-alternatives -s java-8-oracle
+ - export JAVA_HOME=/usr/lib/jvm/java-8-oracle
+ - wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.1.deb && sudo dpkg -i --force-confnew elasticsearch-5.0.1.deb
- sudo cp ./src/test/resources/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml
- sudo cat /etc/elasticsearch/elasticsearch.yml
- - sudo /usr/share/elasticsearch/bin/plugin install delete-by-query
+ - sudo java -version
- sudo service elasticsearch start
- - sleep 10
+ - sleep 30
+
+install:
+ - mvn install -DskipTests=true -Dmaven.javadoc.skip=true -Dgpg.skip=true
+
+script:
- ulimit -a
- mvn test
diff --git a/pom.xml b/pom.xml
index b6d5a39a..e150e042 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
org.nlpcn
elasticsearch-sql
- 2.4.1.0
+ 5.0.1.0
jar
Query elasticsearch using SQL
elasticsearch-sql
@@ -47,7 +47,7 @@
sql
true
true
- 2.4.1
+ 5.0.1
org.elasticsearch.plugin.nlpcn.SqlPlug
@@ -80,6 +80,12 @@
1.0.15
+
+ org.locationtech.spatial4j
+ spatial4j
+ 0.6
+
+
com.vividsolutions
jts
@@ -92,27 +98,48 @@
${elasticsearch.version}
provided
-
- org.elasticsearch.plugin
- delete-by-query
- ${elasticsearch.version}
- provided
-
+
+
+ org.elasticsearch.client
+ transport
+ ${elasticsearch.version}
+ provided
+
+
log4j
log4j
- 1.2.16
+ 1.2.17
provided
+
+ org.apache.logging.log4j
+ log4j-api
+ 2.7
+ provided
+
+
+ org.apache.logging.log4j
+ log4j-core
+ 2.7
+ provided
+
+
javax.servlet
servlet-api
2.5
provided
+
+ com.google.guava
+ guava
+ 15.0
+
+
-
+
@@ -132,8 +159,8 @@
maven-compiler-plugin
2.3.2
- 1.7
- 1.7
+ 1.8
+ 1.8
UTF-8
@@ -192,13 +219,13 @@
${project.build.directory}
druid.jar
-
- org.elasticsearch.plugin
- delete-by-query
- true
- ${project.build.directory}
- deleteByQuery.jar
-
+
+ com.google.guava
+ guava
+ false
+ ${project.build.directory}
+ guava.jar
+
${project.build.directory}
false
@@ -251,6 +278,7 @@
2.4
src/assembly/zip.xml
+ elasticsearch
diff --git a/src/main/java/com/alibaba/druid/pool/ElasticSearchConnection.java b/src/main/java/com/alibaba/druid/pool/ElasticSearchConnection.java
index 40454de1..c148ba37 100644
--- a/src/main/java/com/alibaba/druid/pool/ElasticSearchConnection.java
+++ b/src/main/java/com/alibaba/druid/pool/ElasticSearchConnection.java
@@ -4,6 +4,7 @@
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.InputStream;
import java.io.Reader;
@@ -28,7 +29,7 @@ public ElasticSearchConnection(String jdbcUrl) {
Settings settings = Settings.builder().put("client.transport.ignore_cluster_name", true).build();
try {
- TransportClient transportClient = TransportClient.builder().settings(settings).build();
+ TransportClient transportClient = new PreBuiltTransportClient(settings);
String hostAndPortArrayStr = jdbcUrl.split("/")[2];
String[] hostAndPortArray = hostAndPortArrayStr.split(",");
diff --git a/src/main/java/com/alibaba/druid/pool/ElasticSearchDruidPooledPreparedStatement.java b/src/main/java/com/alibaba/druid/pool/ElasticSearchDruidPooledPreparedStatement.java
index 066c0074..a7ec8fd6 100644
--- a/src/main/java/com/alibaba/druid/pool/ElasticSearchDruidPooledPreparedStatement.java
+++ b/src/main/java/com/alibaba/druid/pool/ElasticSearchDruidPooledPreparedStatement.java
@@ -2,7 +2,6 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
-import org.elasticsearch.plugin.deletebyquery.DeleteByQueryPlugin;
import org.elasticsearch.plugin.nlpcn.QueryActionElasticExecutor;
import org.elasticsearch.plugin.nlpcn.executors.CSVResult;
import org.elasticsearch.plugin.nlpcn.executors.CSVResultsExtractor;
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java
index fa4f17fb..ba80a10a 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java
@@ -4,15 +4,13 @@
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryAction;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.support.RestStatusToXContentListener;
-import org.elasticsearch.rest.action.support.RestToXContentListener;
+import org.elasticsearch.rest.action.RestStatusToXContentListener;
import org.nlpcn.es4sql.exception.SqlParseException;
import org.nlpcn.es4sql.query.SqlElasticDeleteByQueryRequestBuilder;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/DeleteByQueryRestListener.java b/src/main/java/org/elasticsearch/plugin/nlpcn/DeleteByQueryRestListener.java
deleted file mode 100644
index 18d1eca1..00000000
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/DeleteByQueryRestListener.java
+++ /dev/null
@@ -1,76 +0,0 @@
-package org.elasticsearch.plugin.nlpcn;
-
-import org.elasticsearch.action.ShardOperationFailedException;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
-import org.elasticsearch.action.deletebyquery.IndexDeleteByQueryResponse;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentBuilderString;
-import org.elasticsearch.rest.BytesRestResponse;
-import org.elasticsearch.rest.RestChannel;
-import org.elasticsearch.rest.RestResponse;
-import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.support.RestBuilderListener;
-
-
-public class DeleteByQueryRestListener extends RestBuilderListener {
-
- public DeleteByQueryRestListener(RestChannel channel) {
- super(channel);
- }
-
- @Override
- public RestResponse buildResponse(DeleteByQueryResponse result, XContentBuilder builder) throws Exception {
- RestStatus restStatus = RestStatus.OK;
- builder.startObject();
- builder.startObject(Fields._INDICES);
- for (IndexDeleteByQueryResponse indexDeleteByQueryResponse : result.getIndices()) {
- builder.startObject(indexDeleteByQueryResponse.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
-
- builder.startObject(Fields.DELETE);
-
- builder.field(Fields.DELETED, indexDeleteByQueryResponse.getDeleted());
- builder.field(Fields.FOUND, indexDeleteByQueryResponse.getFound());
- builder.field(Fields.FAILED, indexDeleteByQueryResponse.getFailed());
- builder.field(Fields.MISSING, indexDeleteByQueryResponse.getMissing());
-
-
- builder.endObject();
-
- builder.endObject();
- }
- builder.endObject();
- builder.startObject(Fields._SHARDS);
- ShardOperationFailedException[] failures = result.getShardFailures();
- if (failures != null && failures.length > 0) {
- builder.startArray(Fields.FAILURES);
- for (ShardOperationFailedException shardFailure : failures) {
- builder.startObject();
- builder.field(Fields.INDEX, shardFailure.index());
- builder.field(Fields.SHARD, shardFailure.shardId());
- builder.field(Fields.REASON, shardFailure.reason());
- builder.endObject();
- }
- builder.endArray();
- }
- builder.endObject();
-
- builder.endObject();
-
- return new BytesRestResponse(restStatus, builder);
- }
-
- static final class Fields {
- static final XContentBuilderString _INDICES = new XContentBuilderString("_indices");
- static final XContentBuilderString _SHARDS = new XContentBuilderString("_shards");
- static final XContentBuilderString TOTAL = new XContentBuilderString("total");
- static final XContentBuilderString DELETED = new XContentBuilderString("deleted");
- static final XContentBuilderString DELETE = new XContentBuilderString("delete");
- static final XContentBuilderString FOUND = new XContentBuilderString("found");
- static final XContentBuilderString MISSING = new XContentBuilderString("missing");
- static final XContentBuilderString FAILED = new XContentBuilderString("failed");
- static final XContentBuilderString FAILURES = new XContentBuilderString("failures");
- static final XContentBuilderString INDEX = new XContentBuilderString("index");
- static final XContentBuilderString SHARD = new XContentBuilderString("shard");
- static final XContentBuilderString REASON = new XContentBuilderString("reason");
- }
-}
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java
index fb208d67..b0fe2129 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java
@@ -18,6 +18,8 @@
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.InternalSearchHits;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.elasticsearch.search.sort.SortOrder;
import org.nlpcn.es4sql.domain.Field;
import org.nlpcn.es4sql.exception.SqlParseException;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
@@ -202,11 +204,12 @@ protected SearchResponse scrollOneTimeWithMax(Client client,TableInJoinRequestBu
.setScroll(new TimeValue(60000))
.setSize(MAX_RESULTS_ON_ONE_FETCH);
boolean ordered = tableRequest.getOriginalSelect().isOrderdSelect();
- if(!ordered) scrollRequest.setSearchType(SearchType.SCAN);
+ if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
responseWithHits = scrollRequest.get();
//on ordered select - not using SCAN , elastic returns hits on first scroll
- if(!ordered)
- responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
+ //es5.0 elastic always return docs on scan
+// if(!ordered)
+// responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
return responseWithHits;
}
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticUtils.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticUtils.java
index ebc53905..29c834f3 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticUtils.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticUtils.java
@@ -11,6 +11,8 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.elasticsearch.search.sort.SortOrder;
import org.nlpcn.es4sql.domain.Select;
import java.io.IOException;
@@ -26,12 +28,13 @@ public static SearchResponse scrollOneTimeWithHits(Client client, SearchRequestB
.setScroll(new TimeValue(60000))
.setSize(resultSize);
boolean ordered = originalSelect.isOrderdSelect();
- if(!ordered) scrollRequest.setSearchType(SearchType.SCAN);
+ if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
responseWithHits = scrollRequest.get();
//on ordered select - not using SCAN , elastic returns hits on first scroll
- if(!ordered) {
- responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
- }
+ //es5.0 elastic always return docs on scan
+// if(!ordered) {
+// responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
+// }
return responseWithHits;
}
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java b/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java
index aa855b08..4d3f5523 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java
@@ -3,7 +3,6 @@
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
@@ -11,13 +10,11 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.support.RestBuilderListener;
-import org.elasticsearch.search.warmer.IndexWarmersMetaData;
+import org.elasticsearch.rest.action.RestBuilderListener;
import java.io.IOException;
import java.util.List;
@@ -36,7 +33,7 @@ public GetIndexRequestRestListener(RestChannel channel,GetIndexRequest getIndexR
@Override
public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBuilder builder) throws Exception {
- GetIndexRequest.Feature[] features = getIndexRequest.featuresAsEnums();
+ GetIndexRequest.Feature[] features = getIndexRequest.features();
String[] indices = getIndexResponse.indices();
builder.startObject();
@@ -53,9 +50,6 @@ public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBui
case SETTINGS:
writeSettings(getIndexResponse.settings().get(index), builder, channel.request());
break;
- case WARMERS:
- writeWarmers(getIndexResponse.warmers().get(index), builder, channel.request());
- break;
default:
throw new IllegalStateException("feature [" + feature + "] is not valid");
}
@@ -94,20 +88,11 @@ private void writeSettings(Settings settings, XContentBuilder builder, ToXConten
builder.endObject();
}
- private void writeWarmers(List warmers, XContentBuilder builder, ToXContent.Params params) throws IOException {
- builder.startObject(Fields.WARMERS);
- if (warmers != null) {
- for (IndexWarmersMetaData.Entry warmer : warmers) {
- IndexWarmersMetaData.toXContent(warmer, builder, params);
- }
- }
- builder.endObject();
- }
static class Fields {
- static final XContentBuilderString ALIASES = new XContentBuilderString("aliases");
- static final XContentBuilderString MAPPINGS = new XContentBuilderString("mappings");
- static final XContentBuilderString SETTINGS = new XContentBuilderString("settings");
- static final XContentBuilderString WARMERS = new XContentBuilderString("warmers");
+ static final String ALIASES = "aliases";
+ static final String MAPPINGS = "mappings";
+ static final String SETTINGS = "settings";
+ static final String WARMERS = "warmers";
}
}
\ No newline at end of file
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java
index d5d94300..c549303e 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java
@@ -13,6 +13,8 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.internal.InternalSearchHit;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.elasticsearch.search.sort.SortOrder;
import org.nlpcn.es4sql.domain.Field;
import org.nlpcn.es4sql.domain.Select;
import org.nlpcn.es4sql.domain.Where;
@@ -117,10 +119,10 @@ private List createCombinedResults( TableInJoinRequestBuilder
finishedScrolling = true;
} else {
searchResponse = secondTableRequest.getRequestBuilder()
- .setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setSize(MAX_RESULTS_ON_ONE_FETCH).get();
- searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).get();
+ //es5.0 no need to scroll again!
+// searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).get();
finishedScrolling = false;
}
updateMetaSearchResults(searchResponse);
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java
index e5d5fe28..99f31cc6 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java
@@ -1,48 +1,76 @@
package org.elasticsearch.plugin.nlpcn;
import org.elasticsearch.client.Client;
+import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugin.nlpcn.executors.ActionRequestRestExecuterFactory;
import org.elasticsearch.plugin.nlpcn.executors.RestExecutor;
import org.elasticsearch.rest.*;
+import org.elasticsearch.rest.action.RestStatusToXContentListener;
import org.nlpcn.es4sql.SearchDao;
+import org.nlpcn.es4sql.exception.SqlParseException;
import org.nlpcn.es4sql.query.QueryAction;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
+import java.io.IOException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
public class RestSqlAction extends BaseRestHandler {
- @Inject
- public RestSqlAction(Settings settings, Client client, RestController restController) {
- super(settings, restController, client);
+// public static final RestSqlAction INSTANCE = new RestSqlAction();
+
+
+ @Inject
+ public RestSqlAction(Settings settings, RestController restController) {
+ super(settings);
restController.registerHandler(RestRequest.Method.POST, "/_sql/_explain", this);
restController.registerHandler(RestRequest.Method.GET, "/_sql/_explain", this);
restController.registerHandler(RestRequest.Method.POST, "/_sql", this);
restController.registerHandler(RestRequest.Method.GET, "/_sql", this);
}
- @Override
- protected void handleRequest(RestRequest request, RestChannel channel, final Client client) throws Exception {
- String sql = request.param("sql");
-
- if (sql == null) {
- sql = request.content().toUtf8();
- }
- SearchDao searchDao = new SearchDao(client);
- QueryAction queryAction= searchDao.explain(sql);
-
- // TODO add unittests to explain. (rest level?)
- if (request.path().endsWith("/_explain")) {
- String jsonExplanation = queryAction.explain().explain();
- BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation);
- channel.sendResponse(bytesRestResponse);
- } else {
+ @Override
+ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
+ String sql = request.param("sql");
+
+ if (sql == null) {
+ sql = request.content().utf8ToString();
+ }
+ try {
+ SearchDao searchDao = new SearchDao(client);
+ QueryAction queryAction= null;
+
+ queryAction = searchDao.explain(sql);
+
+ // TODO add unittests to explain. (rest level?)
+ if (request.path().endsWith("/_explain")) {
+ final String jsonExplanation = queryAction.explain().explain();
+ return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, jsonExplanation));
+ } else {
Map params = request.params();
RestExecutor restExecutor = ActionRequestRestExecuterFactory.createExecutor(params.get("format"));
- restExecutor.execute(client,params,queryAction,channel);
- }
- }
+ final QueryAction finalQueryAction = queryAction;
+ //doing this hack because elasticsearch throws exception for un-consumed props
+ Map additionalParams = new HashMap<>();
+ List additionalParamsNames = Arrays.asList("_type","_id","_score");
+ for(String paramName : additionalParamsNames) {
+ additionalParams.put(paramName, request.param(paramName));
+ }
+ return channel -> restExecutor.execute(client,additionalParams, finalQueryAction,channel);
+ }
+ } catch (SqlParseException e) {
+ e.printStackTrace();
+ } catch (SQLFeatureNotSupportedException e) {
+ e.printStackTrace();
+ }
+ return null;
+
+ }
+
}
\ No newline at end of file
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/SqlPlug.java b/src/main/java/org/elasticsearch/plugin/nlpcn/SqlPlug.java
index ad1fb096..8eaa0967 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/SqlPlug.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/SqlPlug.java
@@ -1,26 +1,36 @@
package org.elasticsearch.plugin.nlpcn;
+import org.elasticsearch.action.ActionRequest;
+import org.elasticsearch.action.ActionResponse;
+import org.elasticsearch.index.reindex.UpdateByQueryAction;
+import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
-import org.elasticsearch.rest.RestModule;
+import org.elasticsearch.rest.RestHandler;
-public class SqlPlug extends Plugin {
+import java.util.ArrayList;
+import java.util.List;
+
+public class SqlPlug extends Plugin implements ActionPlugin {
public SqlPlug() {
}
- @Override
+
public String name() {
return "sql";
}
- @Override
public String description() {
return "Use sql to query elasticsearch.";
}
- public void onModule(RestModule module)
- {
- module.addRestAction(RestSqlAction.class);
- }
+
+ @Override
+ public List> getRestHandlers() {
+ List> restHandlers = new ArrayList<>(1);
+ restHandlers.add(RestSqlAction.class);
+
+ return restHandlers;
+ }
}
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/executors/ElasticDefaultRestExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/executors/ElasticDefaultRestExecutor.java
index 254594d6..90738f3e 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/executors/ElasticDefaultRestExecutor.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/executors/ElasticDefaultRestExecutor.java
@@ -4,15 +4,15 @@
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
+import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.plugin.nlpcn.*;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.rest.action.support.RestStatusToXContentListener;
+import org.elasticsearch.rest.action.RestStatusToXContentListener;
import org.elasticsearch.search.SearchHits;
import org.nlpcn.es4sql.query.QueryAction;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/executors/RestExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/executors/RestExecutor.java
index 6f3acb13..e384207e 100644
--- a/src/main/java/org/elasticsearch/plugin/nlpcn/executors/RestExecutor.java
+++ b/src/main/java/org/elasticsearch/plugin/nlpcn/executors/RestExecutor.java
@@ -2,6 +2,7 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
+import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.nlpcn.es4sql.query.QueryAction;
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
diff --git a/src/main/java/org/nlpcn/es4sql/SQLFunctions.java b/src/main/java/org/nlpcn/es4sql/SQLFunctions.java
index d99d05a8..37f5f751 100644
--- a/src/main/java/org/nlpcn/es4sql/SQLFunctions.java
+++ b/src/main/java/org/nlpcn/es4sql/SQLFunctions.java
@@ -27,7 +27,7 @@ public class SQLFunctions {
);
- public static Tuple function(String methodName, List paramers, String name) {
+ public static Tuple function(String methodName, List paramers, String name,boolean returnValue) {
Tuple functionStr = null;
switch (methodName) {
case "split":
@@ -52,9 +52,6 @@ public static Tuple function(String methodName, List pa
break;
- case "floor":
- functionStr = floor(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
- break;
case "date_format":
functionStr = date_format(
@@ -63,21 +60,20 @@ public static Tuple function(String methodName, List pa
name);
break;
+ case "floor":
case "round":
- functionStr = round(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
- break;
case "log":
- functionStr = log(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
- break;
-
case "log10":
- functionStr = log10(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
- break;
-
+ case "ceil":
+ case "cbrt":
+ case "rint":
+ case "pow":
+ case "exp":
case "sqrt":
- functionStr = sqrt(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
+ functionStr = mathSingleValueTemplate("Math."+methodName,methodName,Util.expr2Object((SQLExpr) paramers.get(0).value).toString(), name);
break;
+
case "substring":
functionStr = substring(Util.expr2Object((SQLExpr) paramers.get(0).value).toString(),
Integer.parseInt(Util.expr2Object((SQLExpr) paramers.get(1).value).toString()),
@@ -113,6 +109,12 @@ public static Tuple function(String methodName, List pa
default:
}
+ if(returnValue){
+ String generatedFieldName = functionStr.v1();
+ String returnCommand = ";return " + generatedFieldName +";" ;
+ String newScript = functionStr.v2() + returnCommand;
+ functionStr = new Tuple(generatedFieldName, newScript);
+ }
return functionStr;
}
@@ -143,12 +145,14 @@ public static Tuple concat_ws(String split, List column
//split(Column str, java.lang.String pattern)
public static Tuple split(String strColumn, String pattern, int index, String valueName) {
String name = "split_" + random();
+ String script = "";
if (valueName == null) {
- return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')[" + index + "]");
+ script = "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')[" + index + "]";
+
} else {
- return new Tuple(name, strColumn + "; def " + name + " = " + valueName + ".split('" + pattern + "')[" + index + "]");
+ script = "; def " + name + " = " + valueName + ".split('" + pattern + "')[" + index + "]";
}
-
+ return new Tuple(name, script);
}
public static Tuple date_format(String strColumn, String pattern, String valueName) {
@@ -190,11 +194,10 @@ public static Tuple divide(SQLExpr a, SQLExpr b) {
public static Tuple binaryOpertator(String methodName, String operator, SQLExpr a, SQLExpr b) {
String name = methodName + "_" + random();
-
return new Tuple(name,
scriptDeclare(a) + scriptDeclare(b) +
convertType(a) + convertType(b) +
- " def " + name + " = " + extractName(a) + " " + operator + " " + extractName(b));
+ " def " + name + " = " + extractName(a) + " " + operator + " " + extractName(b) ) ;
}
private static boolean isProperty(SQLExpr expr) {
@@ -227,7 +230,8 @@ private static String convertType(SQLExpr script) {
if (newScript.trim().startsWith("def ")) {
//for now ,if variant is string,then change to double.
String temp = newScript.substring(4).split("=")[0].trim();
- return " if( " + temp + " instanceof String) " + temp + "=" + temp.trim() + ".toDouble(); ";
+
+ return " if( " + temp + " instanceof String) " + temp + "= Double.parseDouble(" + temp.trim() + "); ";
} else return "";
@@ -248,13 +252,13 @@ public static Tuple log10(String strColumn, String valueName) {
public static Tuple sqrt(String strColumn, String valueName) {
- return mathSingleValueTemplate("sqrt", strColumn, valueName);
+ return mathSingleValueTemplate("Math.sqrt", "sqrt", strColumn, valueName);
}
public static Tuple round(String strColumn, String valueName) {
- return mathSingleValueTemplate("round", strColumn, valueName);
+ return mathSingleValueTemplate("Math.round","round", strColumn, valueName);
}
@@ -265,7 +269,10 @@ public static Tuple trim(String strColumn, String valueName) {
}
public static Tuple mathSingleValueTemplate(String methodName, String strColumn, String valueName) {
- String name = methodName + "_" + random();
+ return mathSingleValueTemplate(methodName,methodName, strColumn,valueName);
+ }
+ public static Tuple mathSingleValueTemplate(String methodName,String fieldName, String strColumn, String valueName) {
+ String name = fieldName + "_" + random();
if (valueName == null) {
return new Tuple(name, "def " + name + " = " + methodName + "(doc['" + strColumn + "'].value)");
} else {
@@ -277,7 +284,7 @@ public static Tuple mathSingleValueTemplate(String methodName, S
public static Tuple strSingleValueTemplate(String methodName, String strColumn, String valueName) {
String name = methodName + "_" + random();
if (valueName == null) {
- return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value." + methodName + "()");
+ return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value." + methodName + "()" );
} else {
return new Tuple(name, strColumn + "; def " + name + " = " + valueName + "." + methodName + "()");
}
@@ -286,7 +293,7 @@ public static Tuple strSingleValueTemplate(String methodName, St
public static Tuple floor(String strColumn, String valueName) {
- return mathSingleValueTemplate("floor", strColumn, valueName);
+ return mathSingleValueTemplate("Math.floor", "floor",strColumn, valueName);
}
@@ -306,7 +313,7 @@ public static Tuple substring(String strColumn, int pos, int len
public static Tuple split(String strColumn, String pattern, String valueName) {
String name = "split_" + random();
if (valueName == null) {
- return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')");
+ return new Tuple(name, "def " + name + " = doc['" + strColumn + "'].value.split('" + pattern + "')" );
} else {
return new Tuple(name, strColumn + "; def " + name + " = " + valueName + ".split('" + pattern + "')");
}
diff --git a/src/main/java/org/nlpcn/es4sql/Test.java b/src/main/java/org/nlpcn/es4sql/Test.java
index 7cae2ace..eba0f4ea 100644
--- a/src/main/java/org/nlpcn/es4sql/Test.java
+++ b/src/main/java/org/nlpcn/es4sql/Test.java
@@ -3,6 +3,8 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.util.concurrent.ThreadContext;
+import org.elasticsearch.threadpool.ThreadPool;
import java.util.HashMap;
import java.util.Map;
@@ -14,7 +16,14 @@ public class Test {
public static String sqlToEsQuery(String sql) throws Exception {
Map actions = new HashMap();
Settings settings = Settings.builder().build();
- Client client = new NodeClient(settings, null, null, actions);
+// Client client = new NodeClient(settings, null, null, actions);
+// Settings.builder()
+// .put(ThreadContext.PREFIX + ".key1", "val1")
+// .put(ThreadContext.PREFIX + ".key2", "val 2")
+// .build();
+
+ ThreadPool threadPool = new ThreadPool(settings);
+ Client client = new NodeClient(settings, threadPool);
SearchDao searchDao = new org.nlpcn.es4sql.SearchDao(client);
try {
return searchDao.explain(sql).explain().explain();
diff --git a/src/main/java/org/nlpcn/es4sql/domain/Paramer.java b/src/main/java/org/nlpcn/es4sql/domain/Paramer.java
index 0fb1ddd4..8f326b32 100644
--- a/src/main/java/org/nlpcn/es4sql/domain/Paramer.java
+++ b/src/main/java/org/nlpcn/es4sql/domain/Paramer.java
@@ -7,6 +7,7 @@
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
import org.elasticsearch.common.xcontent.ToXContent;
+import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
@@ -36,7 +37,7 @@ public static Paramer parseParamer(SQLMethodInvokeExpr method) throws SqlParseEx
return instance;
}
- public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer paramer) {
+ public static ToXContent fullParamer(MatchPhraseQueryBuilder query, Paramer paramer) {
if (paramer.analysis != null) {
query.analyzer(paramer.analysis);
}
@@ -65,4 +66,14 @@ public static ToXContent fullParamer(WildcardQueryBuilder query, Paramer paramer
return query;
}
+ public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer paramer) {
+ if (paramer.analysis != null) {
+ query.analyzer(paramer.analysis);
+ }
+
+ if (paramer.boost != null) {
+ query.boost(paramer.boost);
+ }
+ return query;
+ }
}
diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java
index 7bbaaefd..c05f9996 100644
--- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java
+++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java
@@ -267,8 +267,9 @@ public static MethodField makeMethodField(String name, List arguments,
if (alias == null && first) {
alias = "field_" + SQLFunctions.random();//paramers.get(0).value.toString();
}
+ //should check if field and first .
Tuple newFunctions = SQLFunctions.function(finalMethodName, paramers,
- paramers.get(0).key);
+ paramers.get(0).key,first);
paramers.clear();
if (!first) {
//variance
diff --git a/src/main/java/org/nlpcn/es4sql/parse/ScriptFilter.java b/src/main/java/org/nlpcn/es4sql/parse/ScriptFilter.java
index 96e45b05..735740dd 100644
--- a/src/main/java/org/nlpcn/es4sql/parse/ScriptFilter.java
+++ b/src/main/java/org/nlpcn/es4sql/parse/ScriptFilter.java
@@ -80,7 +80,8 @@ private void parseAndUpdateScriptType(String scriptType) {
this.scriptType = ScriptService.ScriptType.INLINE;
break;
case "INDEXED":
- this.scriptType = ScriptService.ScriptType.INDEXED;
+ case "STORED":
+ this.scriptType = ScriptService.ScriptType.STORED;
break;
case "FILE":
this.scriptType = ScriptService.ScriptType.FILE;
diff --git a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java
index 5eb2c6f8..b8062bac 100644
--- a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java
+++ b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java
@@ -12,9 +12,10 @@
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
-import org.elasticsearch.search.aggregations.bucket.nested.NestedBuilder;
+import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.nlpcn.es4sql.domain.Field;
import org.nlpcn.es4sql.domain.KVValue;
@@ -49,7 +50,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
setIndicesAndTypes();
setWhere(select.getWhere());
- AggregationBuilder> lastAgg = null;
+ AggregationBuilder lastAgg = null;
for (List groupBy : select.getGroupBys()) {
if (!groupBy.isEmpty()) {
@@ -59,19 +60,21 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
//make groupby can reference to field alias
lastAgg = getGroupAgg(field, select);
- if (lastAgg != null && lastAgg instanceof TermsBuilder && !(field instanceof MethodField)) {
+ if (lastAgg != null && lastAgg instanceof TermsAggregationBuilder && !(field instanceof MethodField)) {
//if limit size is too small, increasing shard size is required
if (select.getRowCount() < 200) {
- ((TermsBuilder) lastAgg).shardSize(2000);
+ ((TermsAggregationBuilder) lastAgg).shardSize(2000);
for (Hint hint : select.getHints()) {
if (hint.getType() == HintType.SHARD_SIZE) {
if (hint.getParams() != null && hint.getParams().length != 0 && hint.getParams()[0] != null) {
- ((TermsBuilder) lastAgg).shardSize((Integer) hint.getParams()[0]);
+ ((TermsAggregationBuilder) lastAgg).shardSize((Integer) hint.getParams()[0]);
}
}
}
}
- ((TermsBuilder) lastAgg).size(select.getRowCount());
+ if(select.getRowCount()>0) {
+ ((TermsAggregationBuilder) lastAgg).size(select.getRowCount());
+ }
}
if (field.isNested()) {
@@ -100,10 +103,12 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
for (int i = 1; i < groupBy.size(); i++) {
field = groupBy.get(i);
- AggregationBuilder> subAgg = getGroupAgg(field, select);
- if (subAgg instanceof TermsBuilder && !(field instanceof MethodField)) {
- ((TermsBuilder) subAgg).size(0);
- }
+ AggregationBuilder subAgg = getGroupAgg(field, select);
+ //ES5.0 termsaggregation with size = 0 not supported anymore
+// if (subAgg instanceof TermsAggregationBuilder && !(field instanceof MethodField)) {
+
+// //((TermsAggregationBuilder) subAgg).size(0);
+// }
if (field.isNested()) {
AggregationBuilder nestedBuilder = createNestedAggregation(field);
@@ -148,7 +153,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
for (Order order : select.getOrderBys()) {
KVValue temp = groupMap.get(order.getName());
if (temp != null) {
- TermsBuilder termsBuilder = (TermsBuilder) temp.value;
+ TermsAggregationBuilder termsBuilder = (TermsAggregationBuilder) temp.value;
switch (temp.key) {
case "COUNT":
termsBuilder.order(Terms.Order.count(isASC(order)));
@@ -179,9 +184,9 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
return sqlElasticRequestBuilder;
}
- private AggregationBuilder> getGroupAgg(Field field, Select select2) throws SqlParseException {
+ private AggregationBuilder getGroupAgg(Field field, Select select2) throws SqlParseException {
boolean refrence = false;
- AggregationBuilder> lastAgg = null;
+ AggregationBuilder lastAgg = null;
for (Field temp : select.getFields()) {
if (temp instanceof MethodField && temp.getName().equals("script")) {
MethodField scriptField = (MethodField) temp;
@@ -200,9 +205,9 @@ private AggregationBuilder> getGroupAgg(Field field, Select select2) throws Sq
return lastAgg;
}
- private AbstractAggregationBuilder wrapNestedIfNeeded(AggregationBuilder nestedBuilder, boolean reverseNested) {
+ private AggregationBuilder wrapNestedIfNeeded(AggregationBuilder nestedBuilder, boolean reverseNested) {
if (!reverseNested) return nestedBuilder;
- if (reverseNested && !(nestedBuilder instanceof NestedBuilder)) return nestedBuilder;
+ if (reverseNested && !(nestedBuilder instanceof NestedAggregationBuilder)) return nestedBuilder;
//we need to jump back to root
return AggregationBuilders.reverseNested(nestedBuilder.getName() + "_REVERSED").subAggregation(nestedBuilder);
}
@@ -213,12 +218,17 @@ private AggregationBuilder createNestedAggregation(Field field) {
String nestedPath = field.getNestedPath();
if (field.isReverseNested()) {
- if (nestedPath == null || !nestedPath.startsWith("~"))
- return AggregationBuilders.reverseNested(getNestedAggName(field)).path(nestedPath);
+ if (nestedPath == null || !nestedPath.startsWith("~")) {
+ ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(getNestedAggName(field));
+ if(nestedPath!=null){
+ reverseNestedAggregationBuilder.path(nestedPath);
+ }
+ return reverseNestedAggregationBuilder;
+ }
nestedPath = nestedPath.substring(1);
}
- nestedBuilder = AggregationBuilders.nested(getNestedAggName(field)).path(nestedPath);
+ nestedBuilder = AggregationBuilders.nested(getNestedAggName(field),nestedPath);
return nestedBuilder;
}
@@ -228,7 +238,7 @@ private AggregationBuilder createChildrenAggregation(Field field) {
String childType = field.getChildType();
- childrenBuilder = AggregationBuilders.children(getChildrenAggName(field)).childType(childType);
+ childrenBuilder = AggregationBuilders.children(getChildrenAggName(field),childType);
return childrenBuilder;
}
@@ -267,7 +277,7 @@ private String getChildrenAggName(Field field) {
return prefix + "@CHILDREN";
}
- private boolean insertFilterIfExistsAfter(AggregationBuilder> agg, List groupBy, AggregationBuilder builder, int nextPosition) throws SqlParseException {
+ private boolean insertFilterIfExistsAfter(AggregationBuilder agg, List groupBy, AggregationBuilder builder, int nextPosition) throws SqlParseException {
if (groupBy.size() <= nextPosition) return false;
Field filterFieldCandidate = groupBy.get(nextPosition);
if (!(filterFieldCandidate instanceof MethodField)) return false;
@@ -277,10 +287,9 @@ private boolean insertFilterIfExistsAfter(AggregationBuilder> agg, List
return true;
}
- private AggregationBuilder> updateAggIfNested(AggregationBuilder> lastAgg, Field field) {
+ private AggregationBuilder updateAggIfNested(AggregationBuilder lastAgg, Field field) {
if (field.isNested()) {
- lastAgg = AggregationBuilders.nested(field.getName() + "Nested")
- .path(field.getNestedPath())
+ lastAgg = AggregationBuilders.nested(field.getName() + "Nested",field.getNestedPath())
.subAggregation(lastAgg);
}
return lastAgg;
@@ -304,12 +313,12 @@ private void setFields(List fields) {
}
}
- private void explanFields(SearchRequestBuilder request, List fields, AggregationBuilder> groupByAgg) throws SqlParseException {
+ private void explanFields(SearchRequestBuilder request, List fields, AggregationBuilder groupByAgg) throws SqlParseException {
for (Field field : fields) {
if (field instanceof MethodField) {
if (field.getName().equals("script")) {
- request.addField(field.getAlias());
+ request.addStoredField(field.getAlias());
DefaultQueryAction defaultQueryAction = new DefaultQueryAction(client, select);
defaultQueryAction.intialize(request);
List tempFields = Lists.newArrayList(field);
@@ -317,14 +326,14 @@ private void explanFields(SearchRequestBuilder request, List fields, Aggr
continue;
}
- AbstractAggregationBuilder makeAgg = aggMaker.makeFieldAgg((MethodField) field, groupByAgg);
+ AggregationBuilder makeAgg = aggMaker.makeFieldAgg((MethodField) field, groupByAgg);
if (groupByAgg != null) {
groupByAgg.subAggregation(makeAgg);
} else {
request.addAggregation(makeAgg);
}
} else if (field instanceof Field) {
- request.addField(field.getName());
+ request.addStoredField(field.getName());
} else {
throw new SqlParseException("it did not support this field method " + field);
}
diff --git a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java
index 0920bcd0..6b603621 100644
--- a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java
+++ b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java
@@ -10,6 +10,7 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
+import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.nlpcn.es4sql.domain.*;
import org.nlpcn.es4sql.domain.hints.Hint;
@@ -69,7 +70,7 @@ private boolean useScrollIfNeeded(boolean existsOrderBy) {
int scrollSize = (Integer) scrollHint.getParams()[0];
int timeoutInMilli = (Integer) scrollHint.getParams()[1];
if (!existsOrderBy)
- request.setSearchType(SearchType.SCAN);
+ request.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
request.setScroll(new TimeValue(timeoutInMilli)).setSize(scrollSize);
}
return scrollHint != null;
diff --git a/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java
index 97d52762..fdd63657 100644
--- a/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java
+++ b/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java
@@ -1,14 +1,16 @@
package org.nlpcn.es4sql.query;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryAction;
+
import org.elasticsearch.index.query.QueryBuilder;
-import org.elasticsearch.plugin.deletebyquery.*;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest;
+import org.elasticsearch.index.reindex.DeleteByQueryAction;
+import org.elasticsearch.index.reindex.DeleteByQueryRequest;
+import org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder;
import org.nlpcn.es4sql.domain.Delete;
import org.nlpcn.es4sql.domain.Select;
import org.nlpcn.es4sql.domain.Where;
@@ -41,12 +43,17 @@ public SqlElasticDeleteByQueryRequestBuilder explain() throws SqlParseException
* Set indices and types to the delete by query request.
*/
private void setIndicesAndTypes() {
- request.setIndices(query.getIndexArr());
- String[] typeArr = query.getTypeArr();
- if (typeArr != null) {
- request.setTypes(typeArr);
- }
+ DeleteByQueryRequest innerRequest = request.request();
+ innerRequest.indices(query.getIndexArr());
+ String[] typeArr = query.getTypeArr();
+ if (typeArr!=null){
+ innerRequest.getSearchRequest().types(typeArr);
+ }
+// String[] typeArr = query.getTypeArr();
+// if (typeArr != null) {
+// request.set(typeArr);
+// }
}
@@ -60,9 +67,9 @@ private void setIndicesAndTypes() {
private void setWhere(Where where) throws SqlParseException {
if (where != null) {
QueryBuilder whereQuery = QueryMaker.explan(where);
- request.setQuery(whereQuery);
+ request.filter(whereQuery);
} else {
- request.setQuery(QueryBuilders.matchAllQuery());
+ request.filter(QueryBuilders.matchAllQuery());
}
}
diff --git a/src/main/java/org/nlpcn/es4sql/query/QueryAction.java b/src/main/java/org/nlpcn/es4sql/query/QueryAction.java
index f8f90983..edd8202a 100644
--- a/src/main/java/org/nlpcn/es4sql/query/QueryAction.java
+++ b/src/main/java/org/nlpcn/es4sql/query/QueryAction.java
@@ -3,7 +3,7 @@
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Client;
-import org.elasticsearch.search.highlight.HighlightBuilder;
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.nlpcn.es4sql.domain.Query;
import org.nlpcn.es4sql.domain.Select;
import org.nlpcn.es4sql.domain.hints.Hint;
@@ -45,15 +45,20 @@ protected void updateRequestWithIndexAndRoutingOptions(Select select, SearchRequ
}
protected void updateRequestWithHighlight(Select select, SearchRequestBuilder request) {
-
+ boolean foundAnyHighlights = false;
+ HighlightBuilder highlightBuilder = new HighlightBuilder();
for(Hint hint : select.getHints()){
if(hint.getType() == HintType.HIGHLIGHT){
HighlightBuilder.Field highlightField = parseHighlightField(hint.getParams());
if(highlightField != null){
- request.addHighlightedField(highlightField);
+ foundAnyHighlights = true;
+ highlightBuilder.field(highlightField);
}
}
}
+ if(foundAnyHighlights){
+ request.highlighter(highlightBuilder);
+ }
}
protected HighlightBuilder.Field parseHighlightField(Object[] params)
diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java
index 5873b749..9cd6bfd8 100644
--- a/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java
+++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java
@@ -3,12 +3,12 @@
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
-import org.elasticsearch.action.support.QuerySourceBuilder;
+import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
@@ -32,12 +32,8 @@ public ActionRequest request() {
@Override
public String explain() {
try {
- XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
- Method method = deleteByQueryRequestBuilder.getClass().getDeclaredMethod("sourceBuilder");
- method.setAccessible(true);
- QuerySourceBuilder sourceBuilder = (QuerySourceBuilder) method.invoke(deleteByQueryRequestBuilder);
- sourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
- return builder.string();
+ SearchRequestBuilder source = deleteByQueryRequestBuilder.source();
+ return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java
index 01343232..b7a879af 100644
--- a/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java
+++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java
@@ -28,14 +28,7 @@ public ActionRequest request() {
@Override
public String explain() {
- try {
- XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
- requestBuilder.internalBuilder().toXContent(builder, ToXContent.EMPTY_PARAMS);
- return builder.string();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
+ return requestBuilder.toString();
}
@Override
diff --git a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java
index 1fea6d84..026088d8 100644
--- a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java
+++ b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java
@@ -51,10 +51,10 @@ private void buildMulti() {
public String explain() {
try {
XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
- firstTable.getRequestBuilder().internalBuilder().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS);
+ firstTable.getRequestBuilder().request().source().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS);
XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
- secondTable.getRequestBuilder().internalBuilder().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS);
+ secondTable.getRequestBuilder().request().source().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS);
String explained = String.format(" first query:\n%s\n second query:\n%s", firstBuilder.string(), secondBuilder.string());
return explained;
diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java
index dcc725ab..c5f2a710 100644
--- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java
+++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java
@@ -1,34 +1,31 @@
package org.nlpcn.es4sql.query.maker;
import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
-import org.elasticsearch.search.aggregations.ValuesSourceAggregationBuilder;
-import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridBuilder;
+import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder;
-import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder;
+import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
-import org.elasticsearch.search.aggregations.bucket.histogram.HistogramBuilder;
-import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder;
-import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeBuilder;
+import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
-import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
-import org.elasticsearch.search.aggregations.metrics.ValuesSourceMetricsAggregationBuilder;
-import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsBuilder;
-import org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesBuilder;
-import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricBuilder;
-import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
+import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregationBuilder;
+import org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder;
+import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricAggregationBuilder;
+import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder;
+import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
import org.elasticsearch.search.sort.SortOrder;
+import org.joda.time.DateTimeZone;
import org.nlpcn.es4sql.Util;
import org.nlpcn.es4sql.domain.Field;
import org.nlpcn.es4sql.domain.KVValue;
@@ -49,11 +46,11 @@ public class AggMaker {
* @return
* @throws SqlParseException
*/
- public AggregationBuilder> makeGroupAgg(Field field) throws SqlParseException {
+ public AggregationBuilder makeGroupAgg(Field field) throws SqlParseException {
if (field instanceof MethodField && field.getName().equals("script")) {
MethodField methodField = (MethodField) field;
- TermsBuilder termsBuilder = AggregationBuilders.terms(methodField.getAlias()).script(new Script(methodField.getParams().get(1).value.toString()));
+ TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(methodField.getAlias()).script(new Script(methodField.getParams().get(1).value.toString()));
groupMap.put(methodField.getAlias(), new KVValue("KEY", termsBuilder));
return termsBuilder;
}
@@ -65,12 +62,12 @@ public AggregationBuilder> makeGroupAgg(Field field) throws SqlParseException
if (methodField.getName().equals("filter")) {
Map paramsAsMap = methodField.getParamsAsMap();
Where where = (Where) paramsAsMap.get("where");
- return AggregationBuilders.filter(paramsAsMap.get("alias").toString()).
- filter(QueryMaker.explan(where));
+ return AggregationBuilders.filter(paramsAsMap.get("alias").toString(),
+ QueryMaker.explan(where));
}
return makeRangeGroup(methodField);
} else {
- TermsBuilder termsBuilder = AggregationBuilders.terms(field.getName()).field(field.getName());
+ TermsAggregationBuilder termsBuilder = AggregationBuilders.terms(field.getName()).field(field.getName());
groupMap.put(field.getName(), new KVValue("KEY", termsBuilder));
return termsBuilder;
}
@@ -85,9 +82,9 @@ public AggregationBuilder> makeGroupAgg(Field field) throws SqlParseException
* @return AggregationBuilder represents the SQL function
* @throws SqlParseException in case of unrecognized function
*/
- public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggregationBuilder parent) throws SqlParseException {
+ public AggregationBuilder makeFieldAgg(MethodField field, AggregationBuilder parent) throws SqlParseException {
groupMap.put(field.getAlias(), new KVValue("FIELD", parent));
- ValuesSourceMetricsAggregationBuilder builder;
+ ValuesSourceAggregationBuilder builder;
field.setAlias(fixAlias(field.getAlias()));
switch (field.getName().toUpperCase()) {
case "SUM":
@@ -110,7 +107,7 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg
return addFieldToAgg(field, builder);
case "PERCENTILES":
builder = AggregationBuilders.percentiles(field.getAlias());
- addSpecificPercentiles((PercentilesBuilder) builder, field.getParams());
+ addSpecificPercentiles((PercentilesAggregationBuilder) builder, field.getParams());
return addFieldToAgg(field, builder);
case "TOPHITS":
return makeTopHitsAgg(field);
@@ -124,7 +121,7 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg
}
}
- private void addSpecificPercentiles(PercentilesBuilder percentilesBuilder, List params) {
+ private void addSpecificPercentiles(PercentilesAggregationBuilder percentilesBuilder, List params) {
List percentiles = new ArrayList<>();
for (KVValue kValue : params) {
if (kValue.value.getClass().equals(BigDecimal.class)) {
@@ -149,7 +146,7 @@ private String fixAlias(String alias) {
return alias.replaceAll("\\[", "(").replaceAll("\\]", ")");
}
- private AbstractAggregationBuilder addFieldToAgg(MethodField field, ValuesSourceMetricsAggregationBuilder builder) {
+ private AggregationBuilder addFieldToAgg(MethodField field, ValuesSourceAggregationBuilder builder) {
KVValue kvValue = field.getParams().get(0);
if (kvValue.key != null && kvValue.key.equals("script")) {
if (kvValue.value instanceof MethodField) {
@@ -172,14 +169,18 @@ private AbstractAggregationBuilder addFieldToAgg(MethodField field, ValuesSource
if (nestedType.isReverse()) {
if (nestedType.path != null && nestedType.path.startsWith("~")) {
String realPath = nestedType.path.substring(1);
- nestedBuilder = AggregationBuilders.nested(nestedAggName).path(realPath);
+ nestedBuilder = AggregationBuilders.nested(nestedAggName,realPath);
nestedBuilder = nestedBuilder.subAggregation(builder);
return AggregationBuilders.reverseNested(nestedAggName + "_REVERSED").subAggregation(nestedBuilder);
} else {
- nestedBuilder = AggregationBuilders.reverseNested(nestedAggName).path(nestedType.path);
+ ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(nestedAggName);
+ if (nestedType.path!=null){
+ reverseNestedAggregationBuilder.path(nestedType.path);
+ }
+ nestedBuilder = reverseNestedAggregationBuilder;
}
} else {
- nestedBuilder = AggregationBuilders.nested(nestedAggName).path(nestedType.path);
+ nestedBuilder = AggregationBuilders.nested(nestedAggName,nestedType.path);
}
return nestedBuilder.subAggregation(builder);
@@ -192,7 +193,7 @@ private AbstractAggregationBuilder addFieldToAgg(MethodField field, ValuesSource
String childrenAggName = childrenType.field + "@CHILDREN";
- childrenBuilder = AggregationBuilders.children(childrenAggName).childType(childrenType.childType);
+ childrenBuilder = AggregationBuilders.children(childrenAggName,childrenType.childType);
return childrenBuilder;
}
@@ -200,7 +201,7 @@ private AbstractAggregationBuilder addFieldToAgg(MethodField field, ValuesSource
return builder.field(kvValue.toString());
}
- private AggregationBuilder> makeRangeGroup(MethodField field) throws SqlParseException {
+ private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException {
switch (field.getName().toLowerCase()) {
case "range":
return rangeBuilder(field);
@@ -224,9 +225,9 @@ private AggregationBuilder> makeRangeGroup(MethodField field) throws SqlParseE
}
- private AggregationBuilder> geoBounds(MethodField field) throws SqlParseException {
+ private AggregationBuilder geoBounds(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
- GeoBoundsBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
+ GeoBoundsAggregationBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
@@ -249,9 +250,9 @@ private AggregationBuilder> geoBounds(MethodField field) throws SqlParseExcept
return boundsBuilder;
}
- private AggregationBuilder> termsAgg(MethodField field) throws SqlParseException {
+ private AggregationBuilder termsAgg(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
- TermsBuilder terms = AggregationBuilders.terms(aggName);
+ TermsAggregationBuilder terms = AggregationBuilders.terms(aggName);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
@@ -294,7 +295,7 @@ private AggregationBuilder> termsAgg(MethodField field) throws SqlParseExcepti
private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
- ScriptedMetricBuilder scriptedMetricBuilder = AggregationBuilders.scriptedMetric(aggName);
+ ScriptedMetricAggregationBuilder scriptedMetricBuilder = AggregationBuilders.scriptedMetric(aggName);
Map scriptedMetricParams = field.getParamsAsMap();
if (!scriptedMetricParams.containsKey("map_script") && !scriptedMetricParams.containsKey("map_script_id") && !scriptedMetricParams.containsKey("map_script_file")) {
throw new SqlParseException("scripted metric parameters must contain map_script/map_script_id/map_script_file parameter");
@@ -318,7 +319,7 @@ private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlP
scriptedMetricBuilder.mapScript(new Script(paramValue));
break;
case "map_script_id":
- scriptedMetricBuilder.mapScript(new Script(paramValue, ScriptService.ScriptType.INDEXED, null, null));
+ scriptedMetricBuilder.mapScript(new Script(paramValue, ScriptService.ScriptType.STORED, null, null));
break;
case "map_script_file":
scriptedMetricBuilder.mapScript(new Script(paramValue, ScriptService.ScriptType.FILE, null, null));
@@ -327,7 +328,7 @@ private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlP
scriptedMetricBuilder.initScript(new Script(paramValue));
break;
case "init_script_id":
- scriptedMetricBuilder.initScript(new Script(paramValue, ScriptService.ScriptType.INDEXED, null, null));
+ scriptedMetricBuilder.initScript(new Script(paramValue, ScriptService.ScriptType.STORED, null, null));
break;
case "init_script_file":
scriptedMetricBuilder.initScript(new Script(paramValue, ScriptService.ScriptType.FILE, null, null));
@@ -336,7 +337,7 @@ private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlP
scriptedMetricBuilder.combineScript(new Script(paramValue));
break;
case "combine_script_id":
- scriptedMetricBuilder.combineScript(new Script(paramValue, ScriptService.ScriptType.INDEXED, null, null));
+ scriptedMetricBuilder.combineScript(new Script(paramValue, ScriptService.ScriptType.STORED, null, null));
break;
case "combine_script_file":
scriptedMetricBuilder.combineScript(new Script(paramValue, ScriptService.ScriptType.FILE, null, null));
@@ -345,7 +346,7 @@ private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlP
scriptedMetricBuilder.reduceScript(new Script(paramValue, ScriptService.ScriptType.INLINE, null, reduceScriptAdditionalParams));
break;
case "reduce_script_id":
- scriptedMetricBuilder.reduceScript(new Script(paramValue, ScriptService.ScriptType.INDEXED, null, reduceScriptAdditionalParams));
+ scriptedMetricBuilder.reduceScript(new Script(paramValue, ScriptService.ScriptType.STORED, null, reduceScriptAdditionalParams));
break;
case "reduce_script_file":
scriptedMetricBuilder.reduceScript(new Script(paramValue, ScriptService.ScriptType.FILE, null, reduceScriptAdditionalParams));
@@ -367,9 +368,9 @@ private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlP
return scriptedMetricBuilder;
}
- private AggregationBuilder> geohashGrid(MethodField field) throws SqlParseException {
+ private AggregationBuilder geohashGrid(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
- GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName);
+ GeoGridAggregationBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
@@ -400,9 +401,9 @@ private AggregationBuilder> geohashGrid(MethodField field) throws SqlParseExce
private static final String TIME_FARMAT = "yyyy-MM-dd HH:mm:ss";
- private ValuesSourceAggregationBuilder> dateRange(MethodField field) {
+ private ValuesSourceAggregationBuilder dateRange(MethodField field) {
String alias = gettAggNameFromParamsOrAlias(field);
- DateRangeBuilder dateRange = AggregationBuilders.dateRange(alias).format(TIME_FARMAT);
+ DateRangeAggregationBuilder dateRange = AggregationBuilders.dateRange(alias).format(TIME_FARMAT);
String value = null;
List ranges = new ArrayList<>();
@@ -415,10 +416,10 @@ private ValuesSourceAggregationBuilder> dateRange(MethodField field) {
dateRange.format(value);
continue;
} else if ("from".equals(kv.key)) {
- dateRange.addUnboundedFrom(kv.value);
+ dateRange.addUnboundedFrom(kv.value.toString());
continue;
} else if ("to".equals(kv.key)) {
- dateRange.addUnboundedTo(kv.value);
+ dateRange.addUnboundedTo(kv.value.toString());
continue;
} else if ("alias".equals(kv.key) || "nested".equals(kv.key) || "children".equals(kv.key)) {
continue;
@@ -441,15 +442,15 @@ private ValuesSourceAggregationBuilder> dateRange(MethodField field) {
* @return
* @throws SqlParseException
*/
- private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseException {
+ private DateHistogramAggregationBuilder dateHistogram(MethodField field) throws SqlParseException {
String alias = gettAggNameFromParamsOrAlias(field);
- DateHistogramBuilder dateHistogram = AggregationBuilders.dateHistogram(alias).format(TIME_FARMAT);
+ DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram(alias).format(TIME_FARMAT);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
switch (kv.key.toLowerCase()) {
case "interval":
- dateHistogram.interval(new DateHistogramInterval(kv.value.toString()));
+ dateHistogram.dateHistogramInterval(new DateHistogramInterval(kv.value.toString()));
break;
case "field":
dateHistogram.field(value);
@@ -458,7 +459,7 @@ private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseExc
dateHistogram.format(value);
break;
case "time_zone":
- dateHistogram.timeZone(value);
+ dateHistogram.timeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(value)));
break;
case "alias":
@@ -482,9 +483,9 @@ private String gettAggNameFromParamsOrAlias(MethodField field) {
return alias;
}
- private HistogramBuilder histogram(MethodField field) throws SqlParseException {
+ private HistogramAggregationBuilder histogram(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
- HistogramBuilder histogram = AggregationBuilders.histogram(aggName);
+ HistogramAggregationBuilder histogram = AggregationBuilders.histogram(aggName);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
@@ -540,7 +541,7 @@ private HistogramBuilder histogram(MethodField field) throws SqlParseException {
* @param field
* @return
*/
- private RangeBuilder rangeBuilder(MethodField field) {
+ private RangeAggregationBuilder rangeBuilder(MethodField field) {
LinkedList params = new LinkedList<>(field.getParams());
@@ -548,7 +549,7 @@ private RangeBuilder rangeBuilder(MethodField field) {
double[] ds = Util.KV2DoubleArr(params);
- RangeBuilder range = AggregationBuilders.range(field.getAlias()).field(fieldName);
+ RangeAggregationBuilder range = AggregationBuilders.range(field.getAlias()).field(fieldName);
for (int i = 1; i < ds.length; i++) {
range.addRange(ds[i - 1], ds[i]);
@@ -596,17 +597,17 @@ private AbstractAggregationBuilder makeCountAgg(MethodField field) {
*/
private AbstractAggregationBuilder makeTopHitsAgg(MethodField field) {
String alias = gettAggNameFromParamsOrAlias(field);
- TopHitsBuilder topHits = AggregationBuilders.topHits(alias);
+ TopHitsAggregationBuilder topHits = AggregationBuilders.topHits(alias);
List params = field.getParams();
String[] include = null;
String[] exclude = null;
for (KVValue kv : params) {
switch (kv.key) {
case "from":
- topHits.setFrom((int) kv.value);
+ topHits.from((int) kv.value);
break;
case "size":
- topHits.setSize((int) kv.value);
+ topHits.size((int) kv.value);
break;
case "include":
include = kv.value.toString().split(",");
@@ -620,12 +621,12 @@ private AbstractAggregationBuilder makeTopHitsAgg(MethodField field) {
case "children":
break;
default:
- topHits.addSort(kv.key, SortOrder.valueOf(kv.value.toString().toUpperCase()));
+ topHits.sort(kv.key, SortOrder.valueOf(kv.value.toString().toUpperCase()));
break;
}
}
if (include != null || exclude != null) {
- topHits.setFetchSource(include, exclude);
+ topHits.fetchSource(include, exclude);
}
return topHits;
}
diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java
index 17f57995..8061b1bc 100644
--- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java
+++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java
@@ -10,6 +10,8 @@
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.google.common.collect.ImmutableSet;
+import org.apache.lucene.search.join.ScoreMode;
+import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
@@ -104,7 +106,7 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value)
case "match_phrase":
case "matchphrase":
paramer = Paramer.parseParamer(value);
- MatchQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery(name, paramer.value);
+ MatchPhraseQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery(name, paramer.value);
bqb = Paramer.fullParamer(matchPhraseQuery, paramer);
break;
default:
@@ -123,8 +125,9 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
case N:
case EQ:
if (value == null || value instanceof SQLIdentifierExpr) {
+ //todo: change to exists
if(value == null || ((SQLIdentifierExpr) value).getName().equalsIgnoreCase("missing")) {
- x = QueryBuilders.missingQuery(name);
+ x = QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(name));
}
else {
throw new SqlParseException(String.format("Cannot recoginze Sql identifer %s", ((SQLIdentifierExpr) value).getName()));
@@ -161,13 +164,13 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
case IN:
//todo: value is subquery? here or before
Object[] values = (Object[]) value;
- MatchQueryBuilder[] matchQueries = new MatchQueryBuilder[values.length];
+ MatchPhraseQueryBuilder[] matchQueries = new MatchPhraseQueryBuilder[values.length];
for(int i = 0; i < values.length; i++) {
matchQueries[i] = QueryBuilders.matchPhraseQuery(name, values[i]);
}
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
- for(MatchQueryBuilder matchQuery : matchQueries) {
+ for(MatchPhraseQueryBuilder matchQuery : matchQueries) {
boolQuery.should(matchQuery);
}
x = boolQuery;
@@ -180,7 +183,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
String wkt = cond.getValue().toString();
try {
ShapeBuilder shapeBuilder = getShapeBuilderFromString(wkt);
- x = QueryBuilders.geoShapeQuery(cond.getName(), shapeBuilder,ShapeRelation.INTERSECTS);
+ x = QueryBuilders.geoShapeQuery(cond.getName(), shapeBuilder);
} catch (IOException e) {
e.printStackTrace();
throw new SqlParseException("couldn't create shapeBuilder from wkt: " + wkt);
@@ -190,44 +193,50 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
BoundingBoxFilterParams boxFilterParams = (BoundingBoxFilterParams) cond.getValue();
Point topLeft = boxFilterParams.getTopLeft();
Point bottomRight = boxFilterParams.getBottomRight();
- x = QueryBuilders.geoBoundingBoxQuery(cond.getName()).topLeft(topLeft.getLat(), topLeft.getLon()).bottomRight(bottomRight.getLat(), bottomRight.getLon());
+ x = QueryBuilders.geoBoundingBoxQuery(cond.getName()).setCorners(topLeft.getLat(), topLeft.getLon(),bottomRight.getLat(), bottomRight.getLon());
break;
case GEO_DISTANCE:
DistanceFilterParams distanceFilterParams = (DistanceFilterParams) cond.getValue();
Point fromPoint = distanceFilterParams.getFrom();
String distance = trimApostrophes(distanceFilterParams.getDistance());
- x = QueryBuilders.geoDistanceQuery(cond.getName()).distance(distance).lon(fromPoint.getLon()).lat(fromPoint.getLat());
+ x = QueryBuilders.geoDistanceQuery(cond.getName()).distance(distance).point(fromPoint.getLat(),fromPoint.getLon());
break;
case GEO_DISTANCE_RANGE:
RangeDistanceFilterParams rangeDistanceFilterParams = (RangeDistanceFilterParams) cond.getValue();
fromPoint = rangeDistanceFilterParams.getFrom();
String distanceFrom = trimApostrophes(rangeDistanceFilterParams.getDistanceFrom());
String distanceTo = trimApostrophes(rangeDistanceFilterParams.getDistanceTo());
- x = QueryBuilders.geoDistanceRangeQuery(cond.getName()).from(distanceFrom).to(distanceTo).lon(fromPoint.getLon()).lat(fromPoint.getLat());
+ x = QueryBuilders.geoDistanceRangeQuery(cond.getName(), fromPoint.getLat(), fromPoint.getLon()).from(distanceFrom).to(distanceTo);
break;
case GEO_POLYGON:
PolygonFilterParams polygonFilterParams = (PolygonFilterParams) cond.getValue();
- GeoPolygonQueryBuilder polygonFilterBuilder = QueryBuilders.geoPolygonQuery(cond.getName());
+ ArrayList geoPoints = new ArrayList();
for(Point p : polygonFilterParams.getPolygon())
- polygonFilterBuilder.addPoint(p.getLat(),p.getLon());
+ geoPoints.add(new GeoPoint(p.getLat(), p.getLon()));
+ GeoPolygonQueryBuilder polygonFilterBuilder = QueryBuilders.geoPolygonQuery(cond.getName(),geoPoints);
x = polygonFilterBuilder;
break;
case GEO_CELL:
CellFilterParams cellFilterParams = (CellFilterParams) cond.getValue();
Point geoHashPoint = cellFilterParams.getGeohashPoint();
- x = QueryBuilders.geoHashCellQuery(cond.getName()).point(geoHashPoint.getLat(),geoHashPoint.getLon()).precision(cellFilterParams.getPrecision()).neighbors(cellFilterParams.isNeighbors());
+ GeoPoint geoPoint = new GeoPoint(geoHashPoint.getLat(),geoHashPoint.getLon());
+ x = QueryBuilders.geoHashCellQuery(cond.getName(),geoPoint).precision(cellFilterParams.getPrecision()).neighbors(cellFilterParams.isNeighbors());
break;
case NIN_TERMS:
case IN_TERMS:
Object[] termValues = (Object[]) value;
if(termValues.length == 1 && termValues[0] instanceof SubQueryExpression)
termValues = ((SubQueryExpression) termValues[0]).getValues();
- x = QueryBuilders.termsQuery(name,termValues);
+ String[] termValuesStrings = new String[termValues.length];
+ for (int i=0;i> buckets = new HashMap<>();
- Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, age", TEST_INDEX));
+ Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('field'='age','size'=200,'alias'='age')", TEST_INDEX));
Terms gender = result.get("gender");
for(Terms.Bucket genderBucket : gender.getBuckets()) {
String genderKey = genderBucket.getKey().toString();
@@ -272,10 +272,10 @@ public void termsWithSize() throws Exception {
@Test
public void termsWithMissing() throws Exception {
- Aggregations result = query(String.format("SELECT count(*) FROM %s/gotCharacters GROUP BY terms('alias'='name','field'='name.ofHisName','missing'='-999')", TEST_INDEX));
- Terms name = result.get("name");
- Assert.assertNotNull(name.getBucketByKey("-999"));
- Assert.assertEquals(1, name.getBucketByKey("-999").getDocCount());
+ Aggregations result = query(String.format("SELECT count(*) FROM %s/gotCharacters GROUP BY terms('alias'='nick','field'='nickname','missing'='no_nickname')", TEST_INDEX));
+ Terms name = result.get("nick");
+ Assert.assertNotNull(name.getBucketByKey("no_nickname"));
+ Assert.assertEquals(3, name.getBucketByKey("no_nickname").getDocCount());
}
@Test
@@ -363,7 +363,7 @@ public void countGroupByDateTest() throws IOException, SqlParseException, SQLFea
@Test
public void countGroupByDateTestWithAlias() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
SqlElasticSearchRequestBuilder result = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select insert_time from online group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM','alias'='myAlias') ").explain();
- boolean containAlias = result.toString().replaceAll("\\s+","").contains("myAlias\":{\"date_histogram\":{\"field\":\"insert_time\",\"interval\":\"1.5h\",\"format\":\"yyyy-MM\"}}");
+ boolean containAlias = result.toString().replaceAll("\\s+","").contains("myAlias\":{\"date_histogram\":{\"field\":\"insert_time\",\"format\":\"yyyy-MM\",\"interval\":\"1.5h\"");
Assert.assertTrue(containAlias);
}
@@ -491,7 +491,7 @@ public void testFromSizeWithAggregations() throws Exception {
public void testSubAggregations() throws Exception {
Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */" +
- " * FROM %s/account GROUP BY (gender, age), (state) LIMIT 0,10", TEST_INDEX);
+ " * FROM %s/account GROUP BY (gender, terms('field'='age','size'=200,'alias'='age')), (state) LIMIT 200,200", TEST_INDEX);
Map> buckets = new HashMap<>();
@@ -557,7 +557,7 @@ public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseExcept
InternalGeoHashGrid grid = result.get("geohash_grid(field=center,precision=5)");
Collection buckets = grid.getBuckets();
for (GeoHashGrid.Bucket bucket : buckets) {
- Assert.assertTrue(bucket.getKey().toString().equals("4.9658203125,104.9853515625") || bucket.getKey().toString().equals("0.4833984375,100.458984375") );
+ Assert.assertTrue(bucket.getKeyAsString().equals("w2fsm") || bucket.getKeyAsString().equals("w0p6y") );
Assert.assertEquals(1,bucket.getDocCount());
}
}
@@ -660,7 +660,7 @@ public void histogramOnNestedField() throws Exception {
Histogram histogram = nested.getAggregations().get("someAlias");
for(Histogram.Bucket bucket : histogram.getBuckets()){
long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue();
- String key = bucket.getKey().toString();
+ String key = ((Double)bucket.getKey()).intValue()+"";
if(key.equals("0") || key.equals("4")){
Assert.assertEquals(2,count);
}
diff --git a/src/test/java/org/nlpcn/es4sql/DeleteTest.java b/src/test/java/org/nlpcn/es4sql/DeleteTest.java
index 5b92e88a..67bbb74f 100644
--- a/src/test/java/org/nlpcn/es4sql/DeleteTest.java
+++ b/src/test/java/org/nlpcn/es4sql/DeleteTest.java
@@ -28,7 +28,8 @@ public void loadTempData() throws Exception {
@After
public void deleteTempData() throws Exception {
- MainTestSuite.deleteQuery(TEST_INDEX, "account_temp");
+ //todo: find a way to delete only specific type
+ //MainTestSuite.deleteQuery(TEST_INDEX, "account_temp");
}
diff --git a/src/test/java/org/nlpcn/es4sql/ExplainTest.java b/src/test/java/org/nlpcn/es4sql/ExplainTest.java
index eaf86ad3..bd61ecec 100644
--- a/src/test/java/org/nlpcn/es4sql/ExplainTest.java
+++ b/src/test/java/org/nlpcn/es4sql/ExplainTest.java
@@ -26,7 +26,7 @@ public void searchSanity() throws IOException, SqlParseException, NoSuchMethodEx
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/search_explain.json"), StandardCharsets.UTF_8).replaceAll("\r","");
String result = explain(String.format("SELECT * FROM %s WHERE firstname LIKE 'A%%' AND age > 20 GROUP BY gender order by _score", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
@@ -34,7 +34,7 @@ public void aggregationQuery() throws IOException, SqlParseException, NoSuchMeth
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/aggregation_query_explain.json"), StandardCharsets.UTF_8).replaceAll("\r","");
String result = explain(String.format("SELECT a, CASE WHEN gender='0' then 'aaa' else 'bbb'end a2345,count(c) FROM %s GROUP BY terms('field'='a'),a2345", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
@@ -42,7 +42,7 @@ public void explainScriptValue() throws IOException, SqlParseException, NoSuchMe
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/script_value.json"), StandardCharsets.UTF_8).replaceAll("\r","");
String result = explain(String.format("SELECT case when gender is null then 'aaa' else gender end test , cust_code FROM %s", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
@@ -50,7 +50,7 @@ public void betweenScriptValue() throws IOException, SqlParseException, NoSuchMe
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/between_query.json"), StandardCharsets.UTF_8).replaceAll("\r","");
String result = explain(String.format("SELECT case when value between 100 and 200 then 'aaa' else value end test , cust_code FROM %s", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
@@ -58,7 +58,7 @@ public void searchSanityFilter() throws IOException, SqlParseException, NoSuchMe
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/search_explain_filter.json"), StandardCharsets.UTF_8).replaceAll("\r","");
String result = explain(String.format("SELECT * FROM %s WHERE firstname LIKE 'A%%' AND age > 20 GROUP BY gender", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
@@ -66,14 +66,14 @@ public void deleteSanity() throws IOException, SqlParseException, NoSuchMethodEx
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/delete_explain.json"), StandardCharsets.UTF_8).replaceAll("\r","");;
String result = explain(String.format("DELETE FROM %s WHERE firstname LIKE 'A%%' AND age > 20", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
@Test
public void spatialFilterExplainTest() throws IOException, SqlParseException, NoSuchMethodException, IllegalAccessException, SQLFeatureNotSupportedException, InvocationTargetException {
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/search_spatial_explain.json"), StandardCharsets.UTF_8).replaceAll("\r","");;
String result = explain(String.format("SELECT * FROM %s WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", TEST_INDEX));
- assertThat(result, equalTo(expectedOutput));
+ assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
}
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
diff --git a/src/test/java/org/nlpcn/es4sql/JDBCTests.java b/src/test/java/org/nlpcn/es4sql/JDBCTests.java
index d2112ad0..c134a86a 100644
--- a/src/test/java/org/nlpcn/es4sql/JDBCTests.java
+++ b/src/test/java/org/nlpcn/es4sql/JDBCTests.java
@@ -33,9 +33,8 @@ public void testJDBC() throws Exception {
connection.close();
dds.close();
- Assert.assertTrue(result.size()==2);
+ Assert.assertTrue(result.size()==1);
Assert.assertTrue(result.get(0).equals("Heath,39,F"));
- Assert.assertTrue(result.get(1).equals("Heath,39,F"));
}
}
diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java
index d3e32bbb..de0a6e23 100644
--- a/src/test/java/org/nlpcn/es4sql/JoinTests.java
+++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java
@@ -14,7 +14,9 @@
import java.io.IOException;
import java.sql.SQLFeatureNotSupportedException;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX;
@@ -61,7 +63,11 @@ public void joinParseWithHintsCheckSelectedFieldsSplitHASH() throws SqlParseExce
" (a.age > 10 OR a.balance > 2000)" +
" AND d.age > 1";
String explainedQuery = hashJoinRunAndExplain(query);
- boolean containTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"holdersName\":[\"virginia\",\"aurelia\"");
+ boolean containTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"holdersName\":[");
+ List holdersName = Arrays.asList("daenerys","nanette","virginia","aurelia","mcgee","hattie","elinor","burton");
+ for(String holderName : holdersName){
+ Assert.assertTrue("should contain:" + holderName , explainedQuery.contains(holderName));
+ }
Assert.assertTrue(containTerms);
}
@@ -439,9 +445,13 @@ public void joinWithOrWithTermsFilterOpt() throws SQLFeatureNotSupportedExceptio
, TEST_INDEX, TEST_INDEX);
String explainedQuery = hashJoinRunAndExplain(query);
- boolean containsHoldersNamesTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"holdersName\":[\"eddard\",\"jaime\",\"daenerys\",\"brandon\"]}");
+ boolean containsHoldersNamesTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"holdersName\":");
Assert.assertTrue(containsHoldersNamesTerms);
- boolean containsAgesTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"age\":[1,1,4]");
+ List holdersName = Arrays.asList("daenerys","brandon","eddard","jaime");
+ for(String holderName : holdersName){
+ Assert.assertTrue("should contain:" + holderName , explainedQuery.contains(holderName));
+ }
+ boolean containsAgesTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"age\":");
Assert.assertTrue(containsAgesTerms);
}
diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java
index c615c15a..0c447043 100644
--- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java
+++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java
@@ -11,13 +11,13 @@
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryAction;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.plugin.deletebyquery.DeleteByQueryPlugin;
+import org.elasticsearch.index.reindex.DeleteByQueryAction;
+import org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@@ -31,7 +31,6 @@
MethodQueryTest.class,
AggregationTest.class,
JoinTests.class,
- DeleteTest.class,
ExplainTest.class,
WktToGeoJsonConverterTests.class,
SqlParserTests.class,
@@ -42,6 +41,8 @@
JDBCTests.class,
UtilTests.class,
MultiQueryTests.class
+ ,
+ DeleteTest.class
})
public class MainTestSuite {
@@ -53,8 +54,9 @@ public class MainTestSuite {
public static void setUp() throws Exception {
Settings settings = Settings.builder().put("client.transport.ignore_cluster_name",true).build();
- client = TransportClient.builder().addPlugin(DeleteByQueryPlugin.class).settings(settings).
- build().addTransportAddress(getTransportAddress());
+
+ client = new PreBuiltTransportClient(settings).
+ addTransportAddress(getTransportAddress());
NodesInfoResponse nodeInfos = client.admin().cluster().prepareNodesInfo().get();
@@ -65,12 +67,15 @@ public static void setUp() throws Exception {
if(client.admin().indices().prepareExists(TEST_INDEX).execute().actionGet().isExists()){
client.admin().indices().prepareDelete(TEST_INDEX).get();
}
- loadBulk("src/test/resources/accounts.json");
loadBulk("src/test/resources/online.json");
+ prepareAccountsIndex();
+ loadBulk("src/test/resources/accounts.json");
preparePhrasesIndex();
loadBulk("src/test/resources/phrases.json");
+ prepareDogsIndex();
loadBulk("src/test/resources/dogs.json");
loadBulk("src/test/resources/peoples.json");
+ prepareGameOfThronesIndex();
loadBulk("src/test/resources/game_of_thrones_complex.json");
loadBulk("src/test/resources/systems.json");
@@ -99,6 +104,73 @@ public static void setUp() throws Exception {
System.out.println("Finished the setup process...");
}
+ private static void prepareGameOfThronesIndex() {
+ String dataMapping = "{ \"gotCharacters\": { " +
+ " \"properties\": {\n" +
+ " \"nickname\": {\n" +
+ "\"type\":\"string\", "+
+ "\"fielddata\":true"+
+ "},\n"+
+ " \"name\": {\n" +
+ "\"properties\": {\n" +
+ "\"firstname\": {\n" +
+ "\"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ "},\n" +
+ "\"lastname\": {\n" +
+ "\"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ "},\n" +
+ "\"ofHerName\": {\n" +
+ "\"type\": \"integer\"\n" +
+ "},\n" +
+ "\"ofHisName\": {\n" +
+ "\"type\": \"integer\"\n" +
+ "}\n" +
+ "}\n" +
+ "}"+
+ "} } }";
+ client.admin().indices().preparePutMapping(TEST_INDEX).setType("gotCharacters").setSource(dataMapping).execute().actionGet();
+ }
+
+ private static void prepareDogsIndex() {
+ String dataMapping = "{ \"dog\": {" +
+ " \"properties\": {\n" +
+ " \"dog_name\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ " }"+
+ " }"+
+ " }" +
+ "}";
+ client.admin().indices().preparePutMapping(TEST_INDEX).setType("dog").setSource(dataMapping).execute().actionGet();
+ }
+
+ private static void prepareAccountsIndex() {
+ String dataMapping = "{ \"account\": {" +
+ " \"properties\": {\n" +
+ " \"gender\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ " }," +
+ " \"address\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ " }," +
+ " \"state\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"fielddata\": true\n" +
+ " }" +
+ " }"+
+ " }" +
+ "}";
+ client.admin().indices().preparePutMapping(TEST_INDEX).setType("account").setSource(dataMapping).execute().actionGet();
+ }
+
+
+
+
+
private static void preparePhrasesIndex() {
String dataMapping = "{ \"phrase\": {" +
" \"properties\": {\n" +
@@ -225,18 +297,16 @@ public static void deleteQuery(String indexName) {
*/
public static void deleteQuery(String indexName, String typeName) {
- DeleteByQueryRequestBuilder deleteQuery = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE);
- deleteQuery.setIndices(indexName);
- if (typeName != null) {
- deleteQuery.setTypes(typeName);
- }
- deleteQuery.setQuery(QueryBuilders.matchAllQuery());
-
- deleteQuery.get();
- System.out.println(String.format("Deleted index %s and type %s", indexName, typeName));
-
+ DeleteByQueryRequestBuilder deleteQueryBuilder = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE);
+ deleteQueryBuilder.request().indices(indexName);
+ if (typeName!=null) {
+ deleteQueryBuilder.request().getSearchRequest().types(typeName);
+ }
+ deleteQueryBuilder.filter(QueryBuilders.matchAllQuery());
+ deleteQueryBuilder.get();
+ System.out.println(String.format("Deleted index %s and type %s", indexName, typeName));
- }
+ }
/**
@@ -267,10 +337,7 @@ public static void prepareSpatialIndex(String type){
"\t\t\t\t\"precision\": \"10km\"\n" +
"\t\t\t},\n" +
"\t\t\t\"center\":{\n" +
- "\t\t\t\t\"type\":\"geo_point\",\n" +
- "\t\t\t\t\"geohash\":true,\n" +
- "\t\t\t\t\"geohash_prefix\":true,\n" +
- "\t\t\t\t\"geohash_precision\":17\n" +
+ "\t\t\t\t\"type\":\"geo_point\"\n" +
"\t\t\t},\n" +
"\t\t\t\"description\":{\n" +
"\t\t\t\t\"type\":\"string\"\n" +
diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java
index fe17162a..a9acabe6 100644
--- a/src/test/java/org/nlpcn/es4sql/QueryTest.java
+++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java
@@ -9,11 +9,11 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
-import org.elasticsearch.search.highlight.HighlightField;
import org.junit.Assert;
import org.junit.Test;
@@ -603,21 +603,23 @@ public void geoDistance() throws SQLFeatureNotSupportedException, SqlParseExcept
Assert.assertEquals("square",result.getSource().get("description"));
}
- @Test
- public void geoDistanceRange() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
- SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_DISTANCE_RANGE(center,'1m','1km',100.5,0.50001)", TEST_INDEX));
- org.junit.Assert.assertEquals(1,results.getTotalHits());
- SearchHit result = results.getAt(0);
- Assert.assertEquals("square",result.getSource().get("description"));
- }
+ //ES5.0: geo_distance_range] queries are no longer supported for geo_point field types. Use geo_distance sort or aggregations
+// @Test
+// public void geoDistanceRange() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
+// SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_DISTANCE_RANGE(center,'1m','1km',100.5,0.50001)", TEST_INDEX));
+// org.junit.Assert.assertEquals(1,results.getTotalHits());
+// SearchHit result = results.getAt(0);
+// Assert.assertEquals("square",result.getSource().get("description"));
+// }
- @Test
- public void geoCell() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
- SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_CELL(center,100.5,0.50001,7)", TEST_INDEX));
- org.junit.Assert.assertEquals(1,results.getTotalHits());
- SearchHit result = results.getAt(0);
- Assert.assertEquals("square",result.getSource().get("description"));
- }
+ //ES5.0: geo_point field no longer supports geohash_cell queries
+// @Test
+// public void geoCell() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
+// SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_CELL(center,100.5,0.50001,7)", TEST_INDEX));
+// org.junit.Assert.assertEquals(1,results.getTotalHits());
+// SearchHit result = results.getAt(0);
+// Assert.assertEquals("square",result.getSource().get("description"));
+// }
@Test
public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
@@ -686,7 +688,8 @@ public void useScrollNoParams() throws IOException, SqlParseException, SQLFeatur
SearchResponse response = getSearchResponse(String.format("SELECT /*! USE_SCROLL*/ age,gender,firstname,balance FROM %s/account LIMIT 2000", TEST_INDEX, TEST_INDEX));
Assert.assertNotNull(response.getScrollId());
SearchHits hits = response.getHits();
- Assert.assertEquals(0,hits.getHits().length);
+ //default is 50 , es5.0 functionality now returns docs on first scroll
+ Assert.assertEquals(50,hits.getHits().length);
Assert.assertEquals(1000,hits.getTotalHits());
}
@@ -695,7 +698,7 @@ public void useScrollWithParams() throws IOException, SqlParseException, SQLFeat
SearchResponse response = getSearchResponse(String.format("SELECT /*! USE_SCROLL(10,5000)*/ age,gender,firstname,balance FROM %s/account ", TEST_INDEX, TEST_INDEX));
Assert.assertNotNull(response.getScrollId());
SearchHits hits = response.getHits();
- Assert.assertEquals(0,hits.getHits().length);
+ Assert.assertEquals(10,hits.getHits().length);
Assert.assertEquals(1000,hits.getTotalHits());
}
diff --git a/src/test/java/org/nlpcn/es4sql/SQLFunctionsTest.java b/src/test/java/org/nlpcn/es4sql/SQLFunctionsTest.java
index 584387d2..d28615be 100644
--- a/src/test/java/org/nlpcn/es4sql/SQLFunctionsTest.java
+++ b/src/test/java/org/nlpcn/es4sql/SQLFunctionsTest.java
@@ -6,12 +6,12 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.plugin.deletebyquery.DeleteByQueryPlugin;
import org.elasticsearch.plugin.nlpcn.QueryActionElasticExecutor;
import org.elasticsearch.plugin.nlpcn.executors.CSVResult;
import org.elasticsearch.plugin.nlpcn.executors.CSVResultsExtractor;
import org.elasticsearch.plugin.nlpcn.executors.CsvExtractorException;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.nlpcn.es4sql.domain.Condition;
@@ -48,6 +48,8 @@ public void functionFieldAliasAndGroupByAlias() throws Exception {
"floor(substring(address,0,3)*20) as key," +
"sum(age) cvalue FROM " + TestsConstants.TEST_INDEX + "/account where address is not null " +
"group by key order by cvalue desc limit 10 ";
+ SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
+ System.out.println(searchDao.explain(query).explain().explain());
CSVResult csvResult = getCsvResult(false, query);
List headers = csvResult.getHeaders();
@@ -133,34 +135,35 @@ public void test() throws Exception {
System.out.println(searchDao.explain(query).explain().explain());
}
- @Test
- public void whereConditionLeftFunctionRightVariableEqualTest() throws Exception {
-
- String query = "SELECT " +
- " * from " +
- TestsConstants.TEST_INDEX + "/account " +
- " where split(address,' ')[0]='806' limit 1000 ";
-
- CSVResult csvResult = getCsvResult(false, query);
- List contents = csvResult.getLines();
- Assert.assertTrue(contents.size() == 4);
- }
-
- @Test
- public void whereConditionLeftFunctionRightVariableGreatTest() throws Exception {
-
- String query = "SELECT " +
- " * from " +
- TestsConstants.TEST_INDEX + "/account " +
- " where floor(split(address,' ')[0]+0) > 805 limit 1000 ";
-
- SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
- System.out.println(searchDao.explain(query).explain().explain());
-
- CSVResult csvResult = getCsvResult(false, query);
- List contents = csvResult.getLines();
- Assert.assertTrue(contents.size() == 223);
- }
+// todo: change when split is back on language
+// @Test
+// public void whereConditionLeftFunctionRightVariableEqualTest() throws Exception {
+//
+// String query = "SELECT " +
+// " * from " +
+// TestsConstants.TEST_INDEX + "/account " +
+// " where split(address,' ')[0]='806' limit 1000 ";
+//
+// CSVResult csvResult = getCsvResult(false, query);
+// List contents = csvResult.getLines();
+// Assert.assertTrue(contents.size() == 4);
+// }
+//
+// @Test
+// public void whereConditionLeftFunctionRightVariableGreatTest() throws Exception {
+//
+// String query = "SELECT " +
+// " * from " +
+// TestsConstants.TEST_INDEX + "/account " +
+// " where floor(split(address,' ')[0]+0) > 805 limit 1000 ";
+//
+// SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
+// System.out.println(searchDao.explain(query).explain().explain());
+//
+// CSVResult csvResult = getCsvResult(false, query);
+// List contents = csvResult.getLines();
+// Assert.assertTrue(contents.size() == 223);
+// }
@Test
public void whereConditionLeftFunctionRightPropertyGreatTest() throws Exception {
@@ -241,22 +244,25 @@ public void concat_ws_fields() throws Exception {
Assert.assertTrue(contents.get(0).contains("-"));
}
- @Test
- public void split_field() throws Exception {
-
- //here is a bug,csv field with spa
- String query = "SELECT " +
- " split(address,' ')[0],age from " +
- TestsConstants.TEST_INDEX + "/account where address is not null " +
- " limit 10 ";
-
- CSVResult csvResult = getCsvResult(false, query);
- List headers = csvResult.getHeaders();
- List contents = csvResult.getLines();
- String[] splits = contents.get(0).split(",");
- Assert.assertTrue(headers.size() == 2);
- Assert.assertTrue(Integer.parseInt(splits[0]) > 0);
- }
+ // todo: change when split is back on language
+// @Test
+// public void split_field() throws Exception {
+//
+// //here is a bug,csv field with spa
+// String query = "SELECT " +
+// " split(address,' ')[0],age from " +
+// TestsConstants.TEST_INDEX + "/account where address is not null " +
+// " limit 10 ";
+// SearchDao searchDao = MainTestSuite.getSearchDao() != null ? MainTestSuite.getSearchDao() : getSearchDao();
+// System.out.println(searchDao.explain(query).explain().explain());
+//
+// CSVResult csvResult = getCsvResult(false, query);
+// List headers = csvResult.getHeaders();
+// List contents = csvResult.getLines();
+// String[] splits = contents.get(0).split(",");
+// Assert.assertTrue(headers.size() == 2);
+// Assert.assertTrue(Integer.parseInt(splits[0]) > 0);
+// }
private CSVResult getCsvResult(boolean flat, String query) throws SqlParseException, SQLFeatureNotSupportedException, Exception, CsvExtractorException {
@@ -273,8 +279,8 @@ private CSVResult getCsvResult(boolean flat, String query, boolean includeScore,
private SearchDao getSearchDao() throws UnknownHostException {
Settings settings = Settings.builder().put("client.transport.ignore_cluster_name", true).build();
- Client client = TransportClient.builder().addPlugin(DeleteByQueryPlugin.class).settings(settings).
- build().addTransportAddress(MainTestSuite.getTransportAddress());
+ Client client = new PreBuiltTransportClient(settings).
+ addTransportAddress(MainTestSuite.getTransportAddress());
return new SearchDao(client);
}
}
diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java
index 15b827fc..1351c599 100644
--- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java
+++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java
@@ -447,7 +447,7 @@ public void scriptFiledPlusLiteralTest() throws SqlParseException {
MethodField scriptMethod = (MethodField) field;
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
Assert.assertEquals(2, scriptMethod.getParams().size());
- Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("doc['field1'].value + 3"));
+ Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("doc['field1'].value + 3"));
}
@Test
@@ -462,7 +462,7 @@ public void scriptFieldPlusFieldTest() throws SqlParseException {
MethodField scriptMethod = (MethodField) field;
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
Assert.assertEquals(2, scriptMethod.getParams().size());
- Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("doc['field1'].value + doc['field2'].value"));
+ Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("doc['field1'].value + doc['field2'].value"));
}
@@ -478,7 +478,7 @@ public void scriptLiteralPlusLiteralTest() throws SqlParseException {
MethodField scriptMethod = (MethodField) field;
Assert.assertEquals("script", scriptMethod.getName().toLowerCase());
Assert.assertEquals(2, scriptMethod.getParams().size());
- Assert.assertTrue(scriptMethod.getParams().get(1).toString().endsWith("1 + 2"));
+ Assert.assertTrue(scriptMethod.getParams().get(1).toString().contains("1 + 2"));
}
diff --git a/src/test/resources/elasticsearch.yml b/src/test/resources/elasticsearch.yml
index 571102f0..8e301d86 100644
--- a/src/test/resources/elasticsearch.yml
+++ b/src/test/resources/elasticsearch.yml
@@ -1,2 +1 @@
-script.inline: true
-script.indexed: true
+script.max_compilations_per_minute: 100
diff --git a/src/test/resources/expectedOutput/aggregation_query_explain.json b/src/test/resources/expectedOutput/aggregation_query_explain.json
index bcdee275..6c23398a 100644
--- a/src/test/resources/expectedOutput/aggregation_query_explain.json
+++ b/src/test/resources/expectedOutput/aggregation_query_explain.json
@@ -1,40 +1,72 @@
{
- "from" : 0,
- "size" : 0,
- "_source" : {
- "includes" : [ ],
- "excludes" : [ ]
- },
- "fields" : [ "a", "a2345" ],
- "script_fields" : {
- "a2345" : {
- "script" : {
- "inline" : "if((doc['gender'].value=='0')){'aaa'} else {'bbb'}"
- }
- }
- },
- "aggregations" : {
- "terms(field=a)" : {
- "terms" : {
- "field" : "a"
- },
- "aggregations" : {
+ "from" : 0,
+ "size" : 0,
+ "_source" : {
+ "includes" : [ ],
+ "excludes" : [ ]
+ },
+ "stored_fields" : [
+ "a",
+ "a2345"
+ ],
+ "script_fields" : {
"a2345" : {
- "terms" : {
"script" : {
- "inline" : "if((doc['gender'].value=='0')){'aaa'} else {'bbb'}"
+ "inline" : "if((doc['gender'].value=='0')){'aaa'} else {'bbb'}",
+ "lang" : "painless"
},
- "size" : 0
- },
- "aggregations" : {
- "COUNT(c)" : {
- "value_count" : {
- "field" : "c"
- }
+ "ignore_failure" : false
+ }
+ },
+ "aggregations" : {
+ "terms(field=a)" : {
+ "terms" : {
+ "field" : "a",
+ "size" : 10,
+ "shard_size" : -1,
+ "min_doc_count" : 1,
+ "shard_min_doc_count" : 0,
+ "show_term_doc_count_error" : false,
+ "order" : [
+ {
+ "_count" : "desc"
+ },
+ {
+ "_term" : "asc"
+ }
+ ]
+ },
+ "aggregations" : {
+ "a2345" : {
+ "terms" : {
+ "script" : {
+ "inline" : "if((doc['gender'].value=='0')){'aaa'} else {'bbb'}",
+ "lang" : "painless"
+ },
+ "size" : 10,
+ "shard_size" : -1,
+ "min_doc_count" : 1,
+ "shard_min_doc_count" : 0,
+ "show_term_doc_count_error" : false,
+ "order" : [
+ {
+ "_count" : "desc"
+ },
+ {
+ "_term" : "asc"
+ }
+ ]
+ },
+ "aggregations" : {
+ "COUNT(c)" : {
+ "value_count" : {
+ "field" : "c"
+ }
+ }
+ }
+ }
}
- }
}
- }
- }
- }
+ },
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/between_query.json b/src/test/resources/expectedOutput/between_query.json
index 6e6dfa28..db5d64b5 100644
--- a/src/test/resources/expectedOutput/between_query.json
+++ b/src/test/resources/expectedOutput/between_query.json
@@ -1,15 +1,20 @@
{
- "from" : 0,
- "size" : 200,
- "_source" : {
- "includes" : [ "cust_code" ],
- "excludes" : [ ]
- },
- "script_fields" : {
- "test" : {
- "script" : {
- "inline" : "if((doc['value'].value >= 100 && doc['value'].value <=200)){'aaa'} else {doc['value'].value}"
- }
- }
- }
+ "from" : 0,
+ "size" : 200,
+ "_source" : {
+ "includes" : [
+ "cust_code"
+ ],
+ "excludes" : [ ]
+ },
+ "script_fields" : {
+ "test" : {
+ "script" : {
+ "inline" : "if((doc['value'].value >= 100 && doc['value'].value <=200)){'aaa'} else {doc['value'].value}",
+ "lang" : "painless"
+ },
+ "ignore_failure" : false
+ }
+ },
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/delete_explain.json b/src/test/resources/expectedOutput/delete_explain.json
index fd06724b..be206a3f 100644
--- a/src/test/resources/expectedOutput/delete_explain.json
+++ b/src/test/resources/expectedOutput/delete_explain.json
@@ -1,24 +1,42 @@
{
- "query" : {
- "bool" : {
- "must" : {
+ "size" : 1000,
+ "query" : {
"bool" : {
- "must" : [ {
- "wildcard" : {
- "firstname" : "A*"
- }
- }, {
- "range" : {
- "age" : {
- "from" : 20,
- "to" : null,
- "include_lower" : false,
- "include_upper" : true
- }
- }
- } ]
+ "must" : [
+ {
+ "bool" : {
+ "must" : [
+ {
+ "wildcard" : {
+ "firstname" : {
+ "wildcard" : "A*",
+ "boost" : 1.0
+ }
+ }
+ },
+ {
+ "range" : {
+ "age" : {
+ "from" : 20,
+ "to" : null,
+ "include_lower" : false,
+ "include_upper" : true,
+ "boost" : 1.0
+ }
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
}
- }
- }
- }
+ },
+ "_source" : false,
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/script_value.json b/src/test/resources/expectedOutput/script_value.json
index e09fa764..bb611a7b 100644
--- a/src/test/resources/expectedOutput/script_value.json
+++ b/src/test/resources/expectedOutput/script_value.json
@@ -1,15 +1,20 @@
{
- "from" : 0,
- "size" : 200,
- "_source" : {
- "includes" : [ "cust_code" ],
- "excludes" : [ ]
- },
- "script_fields" : {
- "test" : {
- "script" : {
- "inline" : "if((doc['gender'].empty)){'aaa'} else {doc['gender'].value}"
- }
- }
- }
+ "from" : 0,
+ "size" : 200,
+ "_source" : {
+ "includes" : [
+ "cust_code"
+ ],
+ "excludes" : [ ]
+ },
+ "script_fields" : {
+ "test" : {
+ "script" : {
+ "inline" : "if((doc['gender'].empty)){'aaa'} else {doc['gender'].value}",
+ "lang" : "painless"
+ },
+ "ignore_failure" : false
+ }
+ },
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/search_explain.json b/src/test/resources/expectedOutput/search_explain.json
index 4747e29b..6ddfa9d8 100644
--- a/src/test/resources/expectedOutput/search_explain.json
+++ b/src/test/resources/expectedOutput/search_explain.json
@@ -1,39 +1,69 @@
{
- "from" : 0,
- "size" : 0,
- "query" : {
- "bool" : {
- "must" : {
+ "from" : 0,
+ "size" : 0,
+ "query" : {
"bool" : {
- "must" : [ {
- "wildcard" : {
- "firstname" : "A*"
+ "must" : [
+ {
+ "bool" : {
+ "must" : [
+ {
+ "wildcard" : {
+ "firstname" : {
+ "wildcard" : "A*",
+ "boost" : 1.0
+ }
+ }
+ },
+ {
+ "range" : {
+ "age" : {
+ "from" : 20,
+ "to" : null,
+ "include_lower" : false,
+ "include_upper" : true,
+ "boost" : 1.0
+ }
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ },
+ "sort" : [
+ {
+ "_score" : {
+ "order" : "asc"
}
- }, {
- "range" : {
- "age" : {
- "from" : 20,
- "to" : null,
- "include_lower" : false,
- "include_upper" : true
- }
+ }
+ ],
+ "aggregations" : {
+ "gender" : {
+ "terms" : {
+ "field" : "gender",
+ "size" : 200,
+ "shard_size" : -1,
+ "min_doc_count" : 1,
+ "shard_min_doc_count" : 0,
+ "show_term_doc_count_error" : false,
+ "order" : [
+ {
+ "_count" : "desc"
+ },
+ {
+ "_term" : "asc"
+ }
+ ]
}
- } ]
}
- }
- }
- },
- "sort" : [ {
- "_score" : {
- "order" : "asc"
- }
- } ],
- "aggregations" : {
- "gender" : {
- "terms" : {
- "field" : "gender",
- "size" : 200
- }
- }
- }
+ },
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/search_explain_filter.json b/src/test/resources/expectedOutput/search_explain_filter.json
index eb70df2a..f6a247c1 100644
--- a/src/test/resources/expectedOutput/search_explain_filter.json
+++ b/src/test/resources/expectedOutput/search_explain_filter.json
@@ -1,38 +1,71 @@
{
- "from" : 0,
- "size" : 0,
- "query" : {
- "bool" : {
- "filter" : {
+ "from" : 0,
+ "size" : 0,
+ "query" : {
"bool" : {
- "must" : {
- "bool" : {
- "must" : [ {
- "wildcard" : {
- "firstname" : "A*"
+ "filter" : [
+ {
+ "bool" : {
+ "must" : [
+ {
+ "bool" : {
+ "must" : [
+ {
+ "wildcard" : {
+ "firstname" : {
+ "wildcard" : "A*",
+ "boost" : 1.0
+ }
+ }
+ },
+ {
+ "range" : {
+ "age" : {
+ "from" : 20,
+ "to" : null,
+ "include_lower" : false,
+ "include_upper" : true,
+ "boost" : 1.0
+ }
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
}
- }, {
- "range" : {
- "age" : {
- "from" : 20,
- "to" : null,
- "include_lower" : false,
- "include_upper" : true
- }
- }
- } ]
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ },
+ "aggregations" : {
+ "gender" : {
+ "terms" : {
+ "field" : "gender",
+ "size" : 200,
+ "shard_size" : -1,
+ "min_doc_count" : 1,
+ "shard_min_doc_count" : 0,
+ "show_term_doc_count_error" : false,
+ "order" : [
+ {
+ "_count" : "desc"
+ },
+ {
+ "_term" : "asc"
+ }
+ ]
}
- }
}
- }
- }
- },
- "aggregations" : {
- "gender" : {
- "terms" : {
- "field" : "gender",
- "size" : 200
- }
- }
- }
+ },
+ "ext" : { }
}
\ No newline at end of file
diff --git a/src/test/resources/expectedOutput/search_spatial_explain.json b/src/test/resources/expectedOutput/search_spatial_explain.json
index 8b175702..316e2b81 100644
--- a/src/test/resources/expectedOutput/search_spatial_explain.json
+++ b/src/test/resources/expectedOutput/search_spatial_explain.json
@@ -2,23 +2,59 @@
"from" : 0,
"size" : 200,
"query" : {
- "bool" : {
- "filter" : {
- "bool" : {
- "must" : {
- "geo_shape" : {
- "place" : {
- "shape" : {
- "type" : "polygon",
- "coordinates" : [ [ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ], [ 102.0, 2.0 ] ] ]
- },
- "relation" : "intersects"
- },
- "_name" : null
- }
- }
+ "bool" : {
+ "filter" : [
+ {
+ "bool" : {
+ "must" : [
+ {
+ "geo_shape" : {
+ "place" : {
+ "shape" : {
+ "type" : "polygon",
+ "orientation" : "right",
+ "coordinates" : [
+ [
+ [
+ 102.0,
+ 2.0
+ ],
+ [
+ 103.0,
+ 2.0
+ ],
+ [
+ 103.0,
+ 3.0
+ ],
+ [
+ 102.0,
+ 3.0
+ ],
+ [
+ 102.0,
+ 2.0
+ ]
+ ]
+ ]
+ },
+ "relation" : "intersects"
+ },
+ "ignore_unmapped" : false,
+ "boost" : 1.0
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
+ }
+ }
+ ],
+ "disable_coord" : false,
+ "adjust_pure_negative" : true,
+ "boost" : 1.0
}
- }
- }
- }
+ },
+ "ext" : { }
}
\ No newline at end of file