@@ -413,47 +413,23 @@ public Map<String, Object> getDocument(String index, String id) throws Exception
413
413
/**
414
414
* Query the index for a specific record and return values for a set of fields.
415
415
*/
416
- public Map <String , String > getFieldsValues (String index , String id , Set <String > fields ) throws IOException {
416
+ public Map <String , String > getFieldsValues (String index , String id , Set <String > fields , String language ) throws Exception {
417
417
if (!activated ) {
418
418
return Collections .emptyMap ();
419
419
}
420
420
421
- Map <String , String > fieldValues = new HashMap <>(fields .size ());
422
- try {
423
- String query = String .format ("_id:\" %s\" " , id );
424
- // TODO: Check maxRecords
425
- // TODO: Use _doc API?
426
-
427
-
428
- final SearchResponse searchResponse = this .query (index , query , null , fields , new HashMap <>(), 0 , 1 , null );
429
-
430
- List <Hit > totalHits = searchResponse .hits ().hits ();
431
- long matches = totalHits .size ();
432
- if (matches == 0 ) {
433
- return fieldValues ;
434
- } else if (matches == 1 ) {
435
- final Hit hit = totalHits .get (0 );
436
-
437
- fields .forEach (f -> {
438
- final Object o = hit .fields ().get (f );
439
- if (o instanceof String ) {
440
- fieldValues .put (f , (String ) o );
441
- } else if (o instanceof HashMap && f .endsWith ("Object" )) {
442
- fieldValues .put (f , (String ) ((HashMap ) o ).get ("default" ));
443
- }
444
- });
445
- } else {
446
- throw new IOException (String .format (
447
- "Your query '%s' returned more than one record, %d in fact. Can't retrieve field values for more than one record." ,
448
- query ,
449
- matches
450
- ));
421
+ Map <String , String > fieldValues = new HashMap <>();
422
+ Map <String , Object > sources = getDocument (index , id );
423
+
424
+ for (String field : fields ) {
425
+ Object value = sources .get (field );
426
+ if (value instanceof String ) {
427
+ fieldValues .put (field , (String ) value );
428
+ } else if (value instanceof Map && field .endsWith ("Object" )) {
429
+ Map valueMap = (Map ) value ;
430
+ String languageValue = (String ) valueMap .get ("lang" + language );
431
+ fieldValues .put (field , languageValue != null ? languageValue : (String ) valueMap .get ("default" ));
451
432
}
452
-
453
- } catch (Exception e ) {
454
- throw new IOException (String .format (
455
- "Error during fields value retrieval. Errors is '%s'." , e .getMessage ()
456
- ));
457
433
}
458
434
return fieldValues ;
459
435
}
0 commit comments