@@ -36,7 +36,7 @@ public class EsDataQuerier implements DataQuerier {
3636
3737 public static final String SCHEME = "http" ;
3838
39- public static final String INDEX_INFO = "indexInfo " ;
39+ public static final String INDEX_NAME_MAPPINGS = "indexNameMappings " ;
4040
4141 protected int maxResultWindow = 10000 ;
4242
@@ -106,7 +106,8 @@ public RecordSet query(String sql, Properties queryProps,
106106 SelectorDefinition selectorDefinition = MoqlParser .parseMoql (sql );
107107 List <String > indexAndTables = getIndexAndTables (selectorDefinition );
108108 // 转换特殊符号 *
109- transformSelectorDefinition (selectorDefinition , indexAndTables , queryProps );
109+ transformSelectorDefinition (selectorDefinition , indexAndTables ,
110+ queryProps );
110111 String query = MoqlTranslator .translateMetadata2Sql (selectorDefinition ,
111112 SqlDialectType .ELASTICSEARCH );
112113 // String queryUrl = makeQueryUrl(indexAndTables, queryProps);
@@ -121,42 +122,45 @@ public RecordSet query(String sql, Properties queryProps,
121122 }
122123 }
123124
124- protected void transformSelectorDefinition (SelectorDefinition selectorDefinition ,
125- List <String > indexAndTables ,
126- Properties properties ) throws IOException {
125+ protected void transformSelectorDefinition (
126+ SelectorDefinition selectorDefinition , List <String > indexAndTables ,
127+ Properties properties ) throws IOException {
127128 SelectorMetadata metadata = (SelectorMetadata ) selectorDefinition ;
128129 ColumnsMetadata columnsMetadata = metadata .getColumns ();
129130 List <ColumnMetadata > columns = columnsMetadata .getColumns ();
130131 for (int i = 0 ; i < columns .size (); i ++) {
131132 if (columns .get (i ).getName ().endsWith ("*" )) {
132133 columns .remove (i );
133- String index ;
134- if (properties .get (INDEX_INFO ) == null ) {
135- index = indexAndTables .get (0 );
134+ Properties indexNameMappings = (Properties ) properties .get (
135+ INDEX_NAME_MAPPINGS );
136+ String indexName ;
137+ if (indexNameMappings == null ) {
138+ indexName = indexAndTables .get (0 );
136139 } else {
137- Properties indexInfo = (Properties ) properties .get (INDEX_INFO );
138- index = indexInfo .getProperty (indexAndTables .get (0 ));
140+ indexName = indexNameMappings .getProperty (indexAndTables .get (0 ));
139141 }
140-
141- List < ColumnMetadata > indexColumnsMetadata = getIndexColumnsMetadata ( index );
142- for (int j = indexColumnsMetadata .size () - 1 ; j >= 0 ; j --) {
142+ List < ColumnMetadata > indexColumnsMetadata = getIndexColumnsMetadata (
143+ indexName );
144+ for (int j = indexColumnsMetadata .size () - 1 ; j >= 0 ; j --) {
143145 ColumnMetadata columnMetadata = indexColumnsMetadata .get (j );
144146 columns .add (i , columnMetadata );
145147 }
146148 }
147149 }
148150 }
149151
150- private List <ColumnMetadata > getIndexColumnsMetadata (String index ) throws IOException {
151- Request request = new Request ("GET" , index );
152+ private List <ColumnMetadata > getIndexColumnsMetadata (String indexName )
153+ throws IOException {
154+ Request request = new Request ("GET" , indexName );
152155 Response response = httpClient .performRequest (request );
153156 String data = EntityUtils .toString (response .getEntity ());
154157 JsonParser jsonParser = new JsonParser ();
155158 JsonObject root = (JsonObject ) jsonParser .parse (data );
156- JsonObject properties = root .get (index ).getAsJsonObject ().get ("mappings" ).getAsJsonObject ().get ("properties" ).getAsJsonObject ();
159+ JsonObject properties = root .get (indexName ).getAsJsonObject ()
160+ .get ("mappings" ).getAsJsonObject ().get ("properties" ).getAsJsonObject ();
157161 List <ColumnMetadata > columns = new ArrayList <>(properties .size ());
158162 properties .entrySet ();
159- for (Map .Entry <String , JsonElement > map : properties .entrySet ()) {
163+ for (Map .Entry <String , JsonElement > map : properties .entrySet ()) {
160164 String name = map .getKey ();
161165 columns .add (new ColumnMetadata (name , name ));
162166 }
@@ -196,32 +200,36 @@ protected List<String> getIndexAndTables(
196200
197201 protected String makeQueryUrl (List <String > indexAndTables ,
198202 Properties queryProps ) {
199-
200-
201203 StringBuffer sbuf = new StringBuffer ();
202- // sbuf.append(esServiceUrl);
203204 sbuf .append ("/" );
204- if (queryProps .get (INDEX_INFO ) != null ) {
205- Properties indexInfo = (Properties ) queryProps .get (INDEX_INFO );
206- String index = indexInfo .getProperty (indexAndTables .get (0 ));
207- sbuf .append (index );
205+ Properties indexNameMappings = (Properties ) queryProps .get (
206+ INDEX_NAME_MAPPINGS );
207+ if (indexNameMappings != null ) {
208+ String indexName = indexNameMappings .getProperty (indexAndTables .get (0 ));
209+ if (indexName == null )
210+ indexName = indexAndTables .get (0 );
211+ sbuf .append (indexName );
208212 } else {
209213 sbuf .append (indexAndTables .get (0 ));
210214 }
211-
212215 sbuf .append ("/" );
213216 if (indexAndTables .size () > 1 ) {
214217 for (int i = 1 ; i < indexAndTables .size (); i ++) {
215218 if (i != 1 ) {
216219 sbuf .append ("," );
217220 }
218- sbuf .append (indexAndTables .get (i ));
221+ String indexName = indexNameMappings .getProperty (indexAndTables .get (i ));
222+ if (indexName == null )
223+ indexName = indexAndTables .get (i );
224+ sbuf .append (indexName );
219225 }
220226 sbuf .append ("/" );
221227 }
222228 sbuf .append ("_search?pretty" );
223- queryProps .remove (INDEX_INFO );
229+ queryProps .remove (INDEX_NAME_MAPPINGS );
224230 assembleUrlProperties (sbuf , queryProps );
231+ if (indexNameMappings != null )
232+ queryProps .put (INDEX_NAME_MAPPINGS , indexNameMappings );
225233 return sbuf .toString ();
226234 }
227235
@@ -277,8 +285,8 @@ protected RecordSet toRecordSet(String data,
277285
278286 protected RecordSet toQueryRecordSet (JsonObject jsonObject ,
279287 SelectorDefinition selectorDefinition ) {
280- RecordSetImpl recordSet = SelectorDefinitionUtils
281- . createRecordSet ( selectorDefinition );
288+ RecordSetImpl recordSet = SelectorDefinitionUtils . createRecordSet (
289+ selectorDefinition );
282290 Operand [] operands = buildColumnOperands (selectorDefinition );
283291 JsonArray hitArray = jsonObject .getAsJsonArray ("hits" );
284292 List <Object []> records = recordSet .getRecords ();
@@ -293,8 +301,8 @@ protected RecordSet toQueryRecordSet(JsonObject jsonObject,
293301
294302 protected RecordSet toAggregationRecordSet (JsonObject jsonObject ,
295303 SelectorDefinition selectorDefinition ) {
296- RecordSetImpl recordSet = SelectorDefinitionUtils
297- . createRecordSet ( selectorDefinition );
304+ RecordSetImpl recordSet = SelectorDefinitionUtils . createRecordSet (
305+ selectorDefinition );
298306 Operand [] operands = buildColumnOperands (selectorDefinition );
299307
300308 List <Object []> records = recordSet .getRecords ();
@@ -326,8 +334,9 @@ protected Operand[] buildColumnOperands(
326334 try {
327335 operands [i ++] = operandFactory .createOperand (value );
328336 } catch (MoqlException e ) {
329- throw new IllegalArgumentException (StringFormater
330- .format ("Invalid column value '{}'!" , columnMetadata .getValue ()));
337+ throw new IllegalArgumentException (
338+ StringFormater .format ("Invalid column value '{}'!" ,
339+ columnMetadata .getValue ()));
331340 }
332341 }
333342 return operands ;
@@ -394,8 +403,8 @@ protected void toAggregationEntityMaps(JsonObject jsonObject,
394403 toAggretaionEntityMaps (jsonObject , entityMaps , head ,
395404 groupNodeNames [offset ]);
396405 } else {
397- JsonObject groupNode = (JsonObject ) jsonObject
398- . get ( groupNodeNames [offset ]);
406+ JsonObject groupNode = (JsonObject ) jsonObject . get (
407+ groupNodeNames [offset ]);
399408 JsonArray jsonArray = (JsonArray ) groupNode .get ("buckets" );
400409 for (int i = 0 ; i < jsonArray .size (); i ++) {
401410 JsonObject jo = (JsonObject ) jsonArray .get (i );
0 commit comments