Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d86c4ce

Browse files
committed
Merge commit '4de3f626cee1d17281abdbfcc0900cc53858d737' into elastic2.0
2 parents f7f1a02 + 4de3f62 commit d86c4ce

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/main/java/org/elasticsearch/plugin/nlpcn/executors/CSVResultRestExecutor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public void execute(Client client, Map<String, String> params, QueryAction query
2626
if(params.containsKey("separator")){
2727
separator = params.get("separator");
2828
}
29-
CSVResult result = new CSVResultsExtractor().extractResults(queryResult,flat,separator);
29+
boolean includeScore = Boolean.getBoolean(params.getOrDefault("_score", "false"));
30+
boolean includeType = Boolean.getBoolean(params.getOrDefault("_type", "false"));
31+
CSVResult result = new CSVResultsExtractor(includeScore,includeType).extractResults(queryResult,flat,separator);
3032
String newLine = "\n";
3133
if(params.containsKey("newLine")){
3234
newLine = params.get("newLine");

src/main/java/org/elasticsearch/plugin/nlpcn/executors/CSVResultsExtractor.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
* Created by Eliran on 27/12/2015.
2525
*/
2626
public class CSVResultsExtractor {
27+
private final boolean includeType;
28+
private final boolean includeScore;
2729
private int currentLineIndex;
2830

29-
public CSVResultsExtractor() {
31+
public CSVResultsExtractor(boolean includeScore, boolean includeType) {
32+
this.includeScore = includeScore;
33+
this.includeType = includeType;
3034
this.currentLineIndex = 0;
3135
}
3236

@@ -229,8 +233,20 @@ private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits,
229233
for(SearchHit hit : hits){
230234
Map<String, Object> doc = hit.sourceAsMap();
231235
mergeHeaders(csvHeaders,doc,flat);
236+
if(this.includeScore){
237+
doc.put("_score",hit.score());
238+
}
239+
if(this.includeType){
240+
doc.put("_type",hit.type());
241+
}
232242
docsAsMap.add(doc);
233243
}
244+
if (this.includeScore){
245+
csvHeaders.add("_score");
246+
}
247+
if (this.includeType){
248+
csvHeaders.add("_type");
249+
}
234250
return new ArrayList<>(csvHeaders);
235251
}
236252

src/test/java/org/nlpcn/es4sql/CSVResultsExtractorTests.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,48 @@ public void percentileAggregationTest() throws SqlParseException, SQLFeatureNotS
341341
Assert.assertEquals("32.0,32.0,34.0,36.0,38.0,40.0,40.0",lines.get(0));
342342
}
343343

344+
@Test
345+
public void includeTypeAndNotScore() throws SqlParseException, SQLFeatureNotSupportedException, Exception {
346+
String query = String.format("select age , firstname from %s/account where age > 31 limit 2", TEST_INDEX);
347+
CSVResult csvResult = getCsvResult(false, query,false,true);
348+
List<String> headers = csvResult.getHeaders();
349+
Assert.assertEquals(3,headers.size());
350+
Assert.assertTrue(headers.contains("age"));
351+
Assert.assertTrue(headers.contains("firstname"));
352+
Assert.assertTrue(headers.contains("_type"));
353+
List<String> lines = csvResult.getLines();
354+
Assert.assertTrue(lines.get(0).contains(",account"));
355+
Assert.assertTrue(lines.get(1).contains(",account"));
356+
}
344357

358+
@Test
359+
public void includeScoreAndNotType() throws SqlParseException, SQLFeatureNotSupportedException, Exception {
360+
String query = String.format("select age , firstname from %s/account where age > 31 limit 2", TEST_INDEX);
361+
CSVResult csvResult = getCsvResult(false, query,true,false);
362+
List<String> headers = csvResult.getHeaders();
363+
Assert.assertEquals(3,headers.size());
364+
Assert.assertTrue(headers.contains("age"));
365+
Assert.assertTrue(headers.contains("firstname"));
366+
Assert.assertTrue(headers.contains("_score"));
367+
List<String> lines = csvResult.getLines();
368+
Assert.assertTrue(lines.get(0).contains(",1.0"));
369+
Assert.assertTrue(lines.get(1).contains(",1.0"));
370+
}
345371

372+
@Test
373+
public void includeScoreAndType() throws SqlParseException, SQLFeatureNotSupportedException, Exception {
374+
String query = String.format("select age , firstname from %s/account where age > 31 limit 2", TEST_INDEX);
375+
CSVResult csvResult = getCsvResult(false, query,true,true);
376+
List<String> headers = csvResult.getHeaders();
377+
Assert.assertEquals(4,headers.size());
378+
Assert.assertTrue(headers.contains("age"));
379+
Assert.assertTrue(headers.contains("firstname"));
380+
Assert.assertTrue(headers.contains("_score"));
381+
Assert.assertTrue(headers.contains("_type"));
382+
List<String> lines = csvResult.getLines();
383+
Assert.assertTrue(lines.get(0).contains(",account,1.0"));
384+
Assert.assertTrue(lines.get(1).contains(",account,1.0"));
385+
}
346386
/* todo: more tests:
347387
* filter/nested and than metric
348388
* histogram
@@ -351,12 +391,14 @@ public void percentileAggregationTest() throws SqlParseException, SQLFeatureNotS
351391

352392

353393
private CSVResult getCsvResult(boolean flat, String query) throws SqlParseException, SQLFeatureNotSupportedException, Exception, CsvExtractorException {
394+
return getCsvResult(flat,query,false,false);
395+
}
396+
397+
private CSVResult getCsvResult(boolean flat, String query,boolean includeScore , boolean includeType) throws SqlParseException, SQLFeatureNotSupportedException, Exception, CsvExtractorException {
354398
SearchDao searchDao = MainTestSuite.getSearchDao();
355399
QueryAction queryAction = searchDao.explain(query);
356400
Object execution = QueryActionElasticExecutor.executeAnyAction(searchDao.getClient(), queryAction);
357-
return new CSVResultsExtractor().extractResults(execution, flat, ",");
401+
return new CSVResultsExtractor(includeScore,includeType).extractResults(execution, flat, ",");
358402
}
359403

360-
361-
362404
}

0 commit comments

Comments
 (0)