Skip to content

Commit

Permalink
Added configuration related to dynamic method injection
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetbehl committed Apr 18, 2016
1 parent 03fdb1c commit 56e4204
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 53 deletions.
43 changes: 19 additions & 24 deletions grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'"]
Expand All @@ -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
Expand All @@ -132,4 +109,22 @@ environments:
production:
elasticSearch:
client:
mode: node
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:
10 changes: 10 additions & 0 deletions grails-app/conf/plugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
62 changes: 44 additions & 18 deletions src/docs/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [:] ->
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 56e4204

Please sign in to comment.