Skip to content

Commit

Permalink
Merge pull request #101 from kaerfredoc/master
Browse files Browse the repository at this point in the history
Support for UUID DB identifiers
  • Loading branch information
noamt committed Jun 16, 2015
2 parents 276dfe1 + 25de97d commit eae2053
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
4 changes: 2 additions & 2 deletions grails-app/domain/test/Building.groovy
Expand Up @@ -3,7 +3,7 @@ package test
class Building {

String name
Date date = new Date()
Date date = new Date()
GeoPoint location

static constraints = {
Expand All @@ -12,6 +12,6 @@ class Building {

static searchable = {
location geoPoint: true, component: true
date alias: "@timestamp"
date alias: "@timestamp"
}
}
17 changes: 17 additions & 0 deletions grails-app/domain/test/custom/id/Toy.groovy
@@ -0,0 +1,17 @@
package test.custom.id

class Toy {
UUID id
String name
String color

static searchable = true

static mapping = {
id(generator: "uuid2", type: "uuid-char", length: 36)
}

static constraints = {
name(nullable: true)
}
}
Expand Up @@ -96,6 +96,8 @@ class ElasticSearchMappingFactory {

if (idTypeIsMongoObjectId(idType)) {
idType = treatValueAsAString(idType)
} else if (idTypeIsUUID(idType)) {
idType = 'string'
}

props.id = defaultDescriptor(idType, 'not_analyzed', true)
Expand Down Expand Up @@ -210,6 +212,10 @@ class ElasticSearchMappingFactory {
idType.equals('objectId')
}

private static boolean idTypeIsUUID(String idType) {
idType.equalsIgnoreCase('uuid')
}

private static String treatValueAsAString(String idType) {
if (Holders.grailsApplication.config.elasticSearch.datastoreImpl =~ /mongo/) {
idType = 'string'
Expand Down
Expand Up @@ -27,6 +27,7 @@ import org.elasticsearch.search.sort.FieldSortBuilder
import org.elasticsearch.search.sort.SortBuilders
import org.elasticsearch.search.sort.SortOrder
import test.*
import test.custom.id.Toy

class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {

Expand Down Expand Up @@ -135,30 +136,30 @@ class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {
List<Product> searchResults = result.searchResults
searchResults[0].name == product.name
}
void 'should marshal the alias field and unmarshal correctly (ignore alias)'() {
given:
def location = new GeoPoint(
lat: 53.00,
lon: 10.00
).save(failOnError: true)
def building = new Building(

void 'should marshal the alias field and unmarshal correctly (ignore alias)'() {
given:
def location = new GeoPoint(
lat: 53.00,
lon: 10.00
).save(failOnError: true)
def building = new Building(
name: 'WatchTower',
location: location
).save(failOnError: true)
building.save(failOnError: true)
building.save(failOnError: true)
elasticSearchService.index(building)
elasticSearchAdminService.refresh()
elasticSearchService.index(building)
elasticSearchAdminService.refresh()
when:
def result = elasticSearchService.search(building.name, [indices: Building, types: Building])
when:
def result = elasticSearchService.search(building.name, [indices: Building, types: Building])
then:
result.total == 1
List<Building> searchResults = result.searchResults
searchResults[0].name == building.name
}
then:
result.total == 1
List<Building> searchResults = result.searchResults
searchResults[0].name == building.name
}
void 'a date value should be marshalled and de-marshalled correctly'() {
Date date = new Date()
Expand Down Expand Up @@ -303,28 +304,28 @@ class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {
List<Product> searchResults = result.searchResults
searchResults[0].name == wurmProduct.name
}
void 'searching with a FilterBuilder filter and a Closure query'(){
void 'searching with a FilterBuilder filter and a Closure query'() {
when: 'searching for a price'
FilterBuilder filter = FilterBuilders.rangeFilter("price").gte(1.99).lte(2.3)
FilterBuilder filter = FilterBuilders.rangeFilter("price").gte(1.99).lte(2.3)
def result = elasticSearchService.search(null as Closure, filter)
then: "the result should be product 'wurm'"
result.total == 1
List<Product> searchResults = result.searchResults
searchResults[0].name == "wurm"
}
void 'searching with a FilterBuilder filter and a QueryBuilder query'(){
when: 'searching for a price'
FilterBuilder filter = FilterBuilders.rangeFilter("price").gte(1.99).lte(2.3)
}
void 'searching with a FilterBuilder filter and a QueryBuilder query'() {
when: 'searching for a price'
FilterBuilder filter = FilterBuilders.rangeFilter("price").gte(1.99).lte(2.3)
def result = elasticSearchService.search(null as QueryBuilder, filter)
then: "the result should be product 'wurm'"
result.total == 1
List<Product> searchResults = result.searchResults
searchResults[0].name == "wurm"
}
}
void 'searching with wildcards in query at first position'() {
when: 'search with asterisk at first position'
Expand Down Expand Up @@ -603,7 +604,7 @@ class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {
given: 'A Spaceship with some cool canons'
def spaceship = new Spaceship(name: 'Spaceball One', captain: new Person(firstName: 'Dark', lastName: 'Helmet').save())
def data = [
engines: [
engines : [
[name: "Primary", maxSpeed: 'Ludicrous Speed'],
[name: "Secondary", maxSpeed: 'Ridiculous Speed'],
[name: "Tertiary", maxSpeed: 'Light Speed'],
Expand Down Expand Up @@ -633,10 +634,28 @@ class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {
shipData.facilities.size() == 3
}
void 'Index a domain object with UUID-based id'() {
given:
def car = new Toy(name: 'Car', color: "Red")
car.save(failOnError: true)
def plane = new Toy(name: 'Plane', color: "Yellow")
plane.save(failOnError: true)
elasticSearchService.index([car, plane])
elasticSearchAdminService.refresh()
when:
def search = elasticSearchService.search('Yellow', [indices: Toy, types: Toy])
then:
search.total == 1
search.searchResults[0].id == plane.id
}
void 'bulk test'() {
given:
(1..1858).each {
def person = new Person(firstName: 'Person', lastName: 'McNumbery'+it).save(flush: true)
def person = new Person(firstName: 'Person', lastName: 'McNumbery' + it).save(flush: true)
def spaceShip = new Spaceship(name: 'Ship-' + it, captain: person).save(flush: true)
println "Created ${it} domains"
}
Expand Down Expand Up @@ -674,7 +693,7 @@ class ElasticSearchServiceIntegrationSpec extends IntegrationSpec {
private def findFailures() {
def domainClass = new DefaultGrailsDomainClass(Spaceship)
def failures=[]
def failures = []
def allObjects = Spaceship.list()
allObjects.each {
elasticSearchHelper.withElasticSearch { client ->
Expand Down

0 comments on commit eae2053

Please sign in to comment.