From 56e4204f50ea684b51cc4f5c8af23db6561f6d7d Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Tue, 19 Apr 2016 02:38:15 +0530 Subject: [PATCH] Added configuration related to dynamic method injection --- grails-app/conf/application.yml | 43 ++++++------- grails-app/conf/plugin.groovy | 10 +++ src/docs/configuration.adoc | 62 +++++++++++++------ .../ElasticsearchGrailsPlugin.groovy | 4 +- .../util/DomainDynamicMethodsUtils.groovy | 21 ++++--- 5 files changed, 87 insertions(+), 53 deletions(-) diff --git a/grails-app/conf/application.yml b/grails-app/conf/application.yml index e427c64b..129b40be 100644 --- a/grails-app/conf/application.yml +++ b/grails-app/conf/application.yml @@ -65,26 +65,6 @@ grails: scriptlets: html taglib: none staticparts: none - ---- -hibernate: - cache: - queries: false - use_second_level_cache: true - use_query_cache: false - region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory' - -endpoints: - jmx: - unique-names: true - -dataSource: - pooled: true - jmxExport: true - driverClassName: org.h2.Driver - username: sa - password: - elasticSearch: date: formats: ["yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"] @@ -106,9 +86,6 @@ environments: transport.sniff: true bulkIndexOnStartup: true test: - dataSource: - dbCreate: update - url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE elasticSearch: client: mode: local @@ -132,4 +109,22 @@ environments: production: elasticSearch: client: - mode: node \ No newline at end of file + mode: node +--- +hibernate: + cache: + queries: false + use_second_level_cache: true + use_query_cache: false + region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory' + +endpoints: + jmx: + unique-names: true + +dataSource: + pooled: true + jmxExport: true + driverClassName: org.h2.Driver + username: sa + password: \ No newline at end of file diff --git a/grails-app/conf/plugin.groovy b/grails-app/conf/plugin.groovy index 4b3b4185..c8ae23cd 100644 --- a/grails-app/conf/plugin.groovy +++ b/grails-app/conf/plugin.groovy @@ -75,10 +75,20 @@ elasticSearch { */ includeTransients = false + /** + * Disable dynamic method injection in domain class + */ + disableDynamicMethodsInjection = false + /** * Search method name in domain class, defaults to search */ searchMethodName = "search" + + /** + * countHits method name in domain class, defaults to search + */ + countHitsMethodName = "countHits" } environments { diff --git a/src/docs/configuration.adoc b/src/docs/configuration.adoc index 0f70f2ae..08830789 100644 --- a/src/docs/configuration.adoc +++ b/src/docs/configuration.adoc @@ -137,6 +137,50 @@ In some cases the developer may prefer not to upgrade the alias to the new versi The default is `false`. ==== +=== Dynamic Method Injection + +==== `elasticSearch.searchMethodName` + +Change the name of search method in domain class. By default it's `search`. + +For example + +[source, groovy] +---- +MyDomain.search("${params.query}") + +---- + +[TIP] +==== +In order to change the method name to `esSearch` just update the `elasticSearch.searchMethodName='esSearch'` in application.groovy +==== + + +==== `elasticSearch.countHitsMethodName` + +Change the name of countHits method in domain class. By default it's `countHits`. + +For example + +[source, groovy] +---- +MyDomain.countHits("${params.query}") + +---- + +[TIP] +==== +In order to change the method name to `esCountHits` just update the `elasticSearch.countHitsMethodName='esCountHits'` in application.groovy +==== + + + +==== `elasticSearch.disableDynamicMethodsInjection` + +To complete disabled injection of dynamic methods set `elasticSearch.disableDynamicMethodsInjection = true` in `applicaiton.groovy` + + === Others properties ==== `elasticSearch.datastoreImpl` @@ -288,24 +332,6 @@ Whether to index and search all non excluded transient properties. All explicitl Default is `false`. ==== -==== `elasticSearch.searchMethodName` - -Change the name of search method in domain class. By default it's `search`. - -For example - -[source, groovy] ----- -MyDomain.search("${params.query}") - ----- - -[TIP] -==== -In order to change the method name to `esSearch` just update the `elasticSearch.searchMethodName='esSearch'` in application.groovy -==== - - === Default configuration script ==== Grails 2.x diff --git a/src/main/groovy/grails/plugins/elasticsearch/ElasticsearchGrailsPlugin.groovy b/src/main/groovy/grails/plugins/elasticsearch/ElasticsearchGrailsPlugin.groovy index 72ac20af..611ea12e 100755 --- a/src/main/groovy/grails/plugins/elasticsearch/ElasticsearchGrailsPlugin.groovy +++ b/src/main/groovy/grails/plugins/elasticsearch/ElasticsearchGrailsPlugin.groovy @@ -142,6 +142,8 @@ class ElasticsearchGrailsPlugin extends Plugin { void doWithDynamicMethods() { // Define the custom ElasticSearch mapping for searchable domain classes - DomainDynamicMethodsUtils.injectDynamicMethods(grailsApplication, applicationContext) + if(grailsApplication.config.elasticSearch.disableDynamicMethodsInjection == false) { + DomainDynamicMethodsUtils.injectDynamicMethods(grailsApplication, applicationContext) + } } } diff --git a/src/main/groovy/grails/plugins/elasticsearch/util/DomainDynamicMethodsUtils.groovy b/src/main/groovy/grails/plugins/elasticsearch/util/DomainDynamicMethodsUtils.groovy index e5fc905a..c7d93882 100644 --- a/src/main/groovy/grails/plugins/elasticsearch/util/DomainDynamicMethodsUtils.groovy +++ b/src/main/groovy/grails/plugins/elasticsearch/util/DomainDynamicMethodsUtils.groovy @@ -54,7 +54,8 @@ class DomainDynamicMethodsUtils { SearchableClassMapping scm = elasticSearchContextHolder.getMappingContext(domainCopy) def indexAndType = [indices: scm.queryingIndex, types: domainCopy.clazz] - String searchMethodName = grailsApplication.config.searchMethodName ?: 'search' + String searchMethodName = grailsApplication.config.elasticSearch.searchMethodName ?: 'search' + String countHitsMethodName = grailsApplication.config.elasticSearch.countHitsMethodName ?: 'countHits' // Inject the search method domain.metaClass.'static'."$searchMethodName" << { String q, Map params = [:] -> @@ -98,35 +99,35 @@ class DomainDynamicMethodsUtils { } // Inject the countHits method - domain.metaClass.'static'.countHits << { String q, Map params = [:] -> + domain.metaClass.'static'."$countHitsMethodName" << { String q, Map params = [:] -> elasticSearchService.countHits(q, params + indexAndType) } - domain.metaClass.'static'.countHits << { Map params = [:], Closure q -> + domain.metaClass.'static'."$countHitsMethodName" << { Map params = [:], Closure q -> elasticSearchService.countHits(params + indexAndType, q) } - domain.metaClass.'static'.countHits << { Closure q, Map params = [:] -> + domain.metaClass.'static'."$countHitsMethodName" << { Closure q, Map params = [:] -> elasticSearchService.countHits(params + indexAndType, q) } // Inject the search method - domain.metaClass.static.search << { String q, Map params = [:] -> + domain.metaClass.static."$searchMethodName" << { String q, Map params = [:] -> elasticSearchService.search(q, params + indexAndType) } - domain.metaClass.static.search << { Map params = [:], Closure q -> + domain.metaClass.static."$searchMethodName" << { Map params = [:], Closure q -> elasticSearchService.search(params + indexAndType, q) } - domain.metaClass.static.search << { Closure q, Map params = [:] -> + domain.metaClass.static."$searchMethodName" << { Closure q, Map params = [:] -> elasticSearchService.search(params + indexAndType, q) } // Inject the countHits method - domain.metaClass.static.countHits << { String q, Map params = [:] -> + domain.metaClass.static."$countHitsMethodName" << { String q, Map params = [:] -> elasticSearchService.countHits(q, params + indexAndType) } - domain.metaClass.static.countHits << { Map params = [:], Closure q -> + domain.metaClass.static."$countHitsMethodName" << { Map params = [:], Closure q -> elasticSearchService.countHits(params + indexAndType, q) } - domain.metaClass.static.countHits << { Closure q, Map params = [:] -> + domain.metaClass.static."$countHitsMethodName" << { Closure q, Map params = [:] -> elasticSearchService.countHits(params + indexAndType, q) }