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

Skip to content

Commit 4de3f62

Browse files
committed
csv results type and score support NLPchina#150
1 parent 5b2406a commit 4de3f62

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
@@ -26,9 +26,13 @@
2626
* Created by Eliran on 27/12/2015.
2727
*/
2828
public class CSVResultsExtractor {
29+
private final boolean includeType;
30+
private final boolean includeScore;
2931
private int currentLineIndex;
3032

31-
public CSVResultsExtractor() {
33+
public CSVResultsExtractor(boolean includeScore, boolean includeType) {
34+
this.includeScore = includeScore;
35+
this.includeType = includeType;
3236
this.currentLineIndex = 0;
3337
}
3438

@@ -231,8 +235,20 @@ private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits,
231235
for(SearchHit hit : hits){
232236
Map<String, Object> doc = hit.sourceAsMap();
233237
mergeHeaders(csvHeaders,doc,flat);
238+
if(this.includeScore){
239+
doc.put("_score",hit.score());
240+
}
241+
if(this.includeType){
242+
doc.put("_type",hit.type());
243+
}
234244
docsAsMap.add(doc);
235245
}
246+
if (this.includeScore){
247+
csvHeaders.add("_score");
248+
}
249+
if (this.includeType){
250+
csvHeaders.add("_type");
251+
}
236252
return new ArrayList<>(csvHeaders);
237253
}
238254

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)