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

Skip to content

Commit 63cb84a

Browse files
committed
fields support NLPchina#146
1 parent 4de3f62 commit 63cb84a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
55
import org.elasticsearch.common.base.Joiner;
66
import org.elasticsearch.search.SearchHit;
7+
import org.elasticsearch.search.SearchHitField;
78
import org.elasticsearch.search.SearchHits;
89
import org.elasticsearch.search.aggregations.Aggregation;
910
import org.elasticsearch.search.aggregations.AggregationBuilder;
@@ -234,7 +235,11 @@ private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits,
234235
Set<String> csvHeaders = new HashSet<>();
235236
for(SearchHit hit : hits){
236237
Map<String, Object> doc = hit.sourceAsMap();
237-
mergeHeaders(csvHeaders,doc,flat);
238+
Map<String, SearchHitField> fields = hit.getFields();
239+
for(SearchHitField searchHitField : fields.values()){
240+
doc.put(searchHitField.getName(),searchHitField.value());
241+
}
242+
mergeHeaders(csvHeaders, doc, flat);
238243
if(this.includeScore){
239244
doc.put("_score",hit.score());
240245
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,27 @@ public void includeScoreAndType() throws SqlParseException, SQLFeatureNotSupport
383383
Assert.assertTrue(lines.get(0).contains(",account,1.0"));
384384
Assert.assertTrue(lines.get(1).contains(",account,1.0"));
385385
}
386+
386387
/* todo: more tests:
387388
* filter/nested and than metric
388389
* histogram
389390
* geo
390391
*/
391392

393+
/* test for scripted fields
394+
@Test
395+
public void scriptedField() throws SqlParseException, SQLFeatureNotSupportedException, Exception {
396+
String query = String.format("select age+1 as agePlusOne ,age , firstname from %s/account where age = 31 limit 1", TEST_INDEX);
397+
CSVResult csvResult = getCsvResult(false, query,false,false);
398+
List<String> headers = csvResult.getHeaders();
399+
Assert.assertEquals(3,headers.size());
400+
Assert.assertTrue(headers.contains("agePlusOne"));
401+
Assert.assertTrue(headers.contains("age"));
402+
Assert.assertTrue(headers.contains("firstname"));
403+
List<String> lines = csvResult.getLines();
404+
Assert.assertTrue(lines.get(0).contains("32,31") || lines.get(0).contains("32.0,31.0"));
405+
}
406+
*/
392407

393408
private CSVResult getCsvResult(boolean flat, String query) throws SqlParseException, SQLFeatureNotSupportedException, Exception, CsvExtractorException {
394409
return getCsvResult(flat,query,false,false);

0 commit comments

Comments
 (0)