Skip to content

Commit e415346

Browse files
committed
Merge commit '60d0b3a31da52cf510c83a37a106941adc1be1de' into elastic2.1
2 parents 039fe4c + 60d0b3a commit e415346

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1503
-126
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: java
22

33
jdk:
44
- oraclejdk8
5-
- openjdk7
5+
- oraclejdk7
66

77

88
script:

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Elasticsearch-SQL
22
=================
33
**1.X** [![1.X Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=master)](https://travis-ci.org/NLPchina/elasticsearch-sql) <br>
4-
**2.X** [![2.0.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)
4+
**2.0.0** [![2.0.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)<br>
5+
**2.1.0** [![2.1.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)<br>
56

67
Query elasticsearch using familiar SQL syntax.
78
You can also use ES functions in SQL.
@@ -20,18 +21,23 @@ Install as plugin:
2021
Versions
2122
------------
2223

23-
| elasticsearch version | latest version | remarks |
24-
| --------------------- | ------------- | ----------------------------- |
25-
| 1.X | 1.4.6 | tested against elastic 1.4-1.6 |
26-
| 2.0.0 | 2.0.1 | delete commands not supported |
24+
| elasticsearch version | latest version | remarks | branch |
25+
| --------------------- | ------------- | ----------------------------- | ---------- |
26+
| 1.X | 1.4.7 | tested against elastic 1.4-1.6 | master |
27+
| 2.0.0 | 2.0.2 | delete commands not supported | elastic2.0 |
28+
| 2.1.0 | 2.1.0 | delete commands not supported | elastic2.1 |
2729

2830
### Elasticsearch 1.X
2931
````
30-
./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.6/elasticsearch-sql-1.4.6.zip --install sql
32+
./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.7/elasticsearch-sql-1.4.7.zip --install sql
3133
````
3234
### Elasticsearch 2.0.0
3335
````
34-
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.0.1/elasticsearch-sql-2.0.1.zip
36+
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.0.2/elasticsearch-sql-2.0.2.zip
37+
````
38+
### Elasticsearch 2.1.0
39+
````
40+
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.1.0/elasticsearch-sql-2.1.0.zip
3541
````
3642
After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`.
3743

src/_site/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function addFieldsToRow (row,hit) {
161161
function removeNestedAndFilters (aggs) {
162162
for(field in aggs)
163163
{
164-
if (field.endsWith("@NESTED") || field.endsWith("@FILTER")){
164+
if (field.endsWith("@NESTED") || field.endsWith("@FILTER") || field.endsWith("@NESTED_REVERSED")){
165165
delete aggs[field]["doc_count"];
166166
delete aggs[field]["key"];
167167
leftField = Object.keys(aggs[field])[0];

src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.elasticsearch.plugin.nlpcn;
22

3-
import org.elasticsearch.action.ActionRequest;
43
import org.elasticsearch.client.Client;
54
import org.elasticsearch.common.inject.Inject;
65
import org.elasticsearch.common.settings.Settings;
6+
import org.elasticsearch.plugin.nlpcn.executors.ActionRequestRestExecuterFactory;
7+
import org.elasticsearch.plugin.nlpcn.executors.RestExecutor;
78
import org.elasticsearch.rest.*;
89
import org.nlpcn.es4sql.SearchDao;
10+
import org.nlpcn.es4sql.query.QueryAction;
911
import org.nlpcn.es4sql.query.SqlElasticRequestBuilder;
1012

13+
import java.util.Map;
14+
1115

1216
public class RestSqlAction extends BaseRestHandler {
1317

@@ -27,18 +31,18 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli
2731
if (sql == null) {
2832
sql = request.content().toUtf8();
2933
}
30-
3134
SearchDao searchDao = new SearchDao(client);
32-
SqlElasticRequestBuilder actionRequestBuilder = searchDao.explain(sql);
33-
ActionRequest actionRequest = actionRequestBuilder.request();
35+
QueryAction queryAction= searchDao.explain(sql);
3436

3537
// TODO add unittests to explain. (rest level?)
3638
if (request.path().endsWith("/_explain")) {
37-
String jsonExplanation = actionRequestBuilder.explain();
39+
String jsonExplanation = queryAction.explain().explain();
3840
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation);
3941
channel.sendResponse(bytesRestResponse);
4042
} else {
41-
new ActionRequestRestExecuter(actionRequestBuilder, channel, client).execute();
43+
Map<String, String> params = request.params();
44+
RestExecutor restExecutor = ActionRequestRestExecuterFactory.createExecutor(params.get("format"));
45+
restExecutor.execute(client,params,queryAction,channel);
4246
}
4347
}
4448
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.elasticsearch.plugin.nlpcn.executors;
2+
3+
/**
4+
* Created by Eliran on 26/12/2015.
5+
*/
6+
public class ActionRequestRestExecuterFactory {
7+
public static RestExecutor createExecutor(String format) {
8+
if(format == null || format.equals("")){
9+
return new ElasticDefaultRestExecutor();
10+
}
11+
if(format.equalsIgnoreCase("csv")){
12+
return new CSVResultRestExecutor();
13+
}
14+
return null;
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.elasticsearch.plugin.nlpcn.executors;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by Eliran on 27/12/2015.
7+
*/
8+
public class CSVResult {
9+
private final List<String> headers;
10+
private final List<String> lines;
11+
12+
public CSVResult(List<String> headers, List<String> lines) {
13+
this.headers = headers;
14+
this.lines = lines;
15+
}
16+
17+
public List<String> getHeaders() {
18+
return headers;
19+
}
20+
21+
public List<String> getLines() {
22+
return lines;
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.elasticsearch.plugin.nlpcn.executors;
2+
3+
import com.google.common.base.Joiner;
4+
import org.elasticsearch.client.Client;
5+
import org.elasticsearch.plugin.nlpcn.QueryActionElasticExecutor;
6+
import org.elasticsearch.rest.BytesRestResponse;
7+
import org.elasticsearch.rest.RestChannel;
8+
import org.elasticsearch.rest.RestStatus;
9+
import org.nlpcn.es4sql.query.QueryAction;
10+
11+
import java.util.*;
12+
13+
/**
14+
* Created by Eliran on 26/12/2015.
15+
*/
16+
public class CSVResultRestExecutor implements RestExecutor {
17+
18+
@Override
19+
public void execute(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel) throws Exception {
20+
Object queryResult = QueryActionElasticExecutor.executeAnyAction(client, queryAction);
21+
boolean flat = false;
22+
if(params.containsKey("flat")){
23+
flat = Boolean.parseBoolean(params.get("flat"));
24+
}
25+
String separator = ",";
26+
if(params.containsKey("separator")){
27+
separator = params.get("separator");
28+
}
29+
CSVResult result = new CSVResultsExtractor().extractResults(queryResult,flat,separator);
30+
String newLine = "\n";
31+
if(params.containsKey("newLine")){
32+
newLine = params.get("newLine");
33+
}
34+
String csvString = buildString(separator, result, newLine);
35+
BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, csvString);
36+
channel.sendResponse(bytesRestResponse);
37+
}
38+
39+
private String buildString(String separator, CSVResult result, String newLine) {
40+
StringBuilder csv = new StringBuilder();
41+
csv.append(Joiner.on(separator).join(result.getHeaders()));
42+
csv.append(newLine);
43+
csv.append(Joiner.on(newLine).join(result.getLines()));
44+
return csv.toString();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)