Skip to content

Commit 150ec57

Browse files
committed
add optional field argument to getMappings method
1 parent 199debc commit 150ec57

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
* Add optional `field` argument to `getMappings` client method, which allows for wildcard search and filtering of mappings ( e.g. `(GET) /logs-coldobx-*/_mapping/*application` )
15+
1216
## [3.4.0] - 2024-09-26
1317

1418
### Added

models/io/HyperClient.cfc

+22-3
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,37 @@ component accessors="true" threadSafe singleton {
266266
}
267267
}
268268

269+
270+
269271
/**
270272
* Returns the mappings for an index
271273
*
272274
* @indexName string the name of the index
275+
* @field an optional field name or field pattern
273276
*/
274-
struct function getMappings( required string indexName ){
275-
var response = variables.nodePool.newRequest( arguments.indexName & "/_mapping", "GET" ).send();
277+
struct function getMappings( required string indexName, string field ){
278+
var path = arguments.indexName & "/_mapping";
279+
if( !isNull( arguments.field ) ){
280+
path &= "/field/" & arguments.field;
281+
}
282+
var response = variables.nodePool.newRequest( path, "GET" ).send();
276283

277284
if ( response.getStatusCode() != 200 ) {
278285
onResponseFailure( response );
279286
} else {
280-
return response.json()[ indexName ].mappings;
287+
return isNull( arguments.field )
288+
? response.json()[ indexName ].mappings
289+
: response.json().reduce( ( acc, indexKey, value ) => {
290+
value.mappings.keyArray().each( ( mappingKey ) => {
291+
if( !acc.keyExists( mappingKey ) ){
292+
acc[ mappingKey ] = value.mappings[ mappingKey ];
293+
acc[ mappingKey ]["indices"] = [ indexKey ];
294+
} else {
295+
acc[ mappingKey ]["indices"].append( indexKey );
296+
}
297+
} );
298+
return acc;
299+
}, {} );
281300
}
282301
}
283302

test-harness/tests/specs/unit/ClientTest.cfc

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ component extends="HyperClientTest" {
33
function beforeAll(){
44
super.beforeAll();
55

6-
variables.model = getWirebox().getInstance( "HyperClient@cbelasticsearch" );
6+
variables.model = getWirebox().getInstance( "Client@cbelasticsearch" );
77

88
super.beforeAll();
99
}
1010

1111
function run(){
12-
// all of our native client methods are interface and pass through to the native client. Those tests after the core tests are completed
13-
super.run();
12+
describe( "Ensures the mapping for the client is present", function(){
13+
it( "Checks the instance", function(){
14+
expect( variables.model ).toBeInstanceOf( "cbelasticsearch.models.io.HyperClient" );
15+
});
16+
} );
1417
}
1518

1619
}

test-harness/tests/specs/unit/HyperClientTest.cfc

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ component extends="coldbox.system.testing.BaseTestCase" {
7575

7676
expect( mappings.properties ).toHaveKey( "modifiedTime" );
7777
expect( mappings.properties.modifiedTime ).toHaveKey( "format" );
78+
79+
var fieldMappings = variables.model.getMappings( variables.testIndexName, "*modifiedTime*" );
80+
81+
debug( fieldMappings );
82+
83+
expect( fieldMappings )
84+
.toBeStruct()
85+
.toHaveKey( "modifiedTime" );
86+
87+
expect( fieldMappings.modifiedTime ).toBeStruct()
88+
.toHaveKey( "full_name" )
89+
.toHaveKey( "mapping" )
90+
.toHaveKey( "indices" );
7891

7992
} );
8093

0 commit comments

Comments
 (0)