如何在grails和elasticsearch中定义单个bean

时间:2018-02-07 13:55:07

标签: mongodb elasticsearch grails

我想使用mongodb,grails和elasticsearch进行简单的搜索。但我发现这个问题并无法理解如何实施解决方案。

这是我的Domain类

package hellokitty
class Person {
    String name
    static mapWith = "mongo"

        static searchable = {
        only = 'name'
    }

    static constraints = {
    }
}

这是我的build.gradle文件

buildscript {
    ext {
        grailsVersion = '3.3.2'
        gormVersion = '6.1.8.RELEASE'
        elasticsearchVersion = '5.4.1'
        elasticsearchAttachmentsVersion = '5.4.1'
    }
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "gradle.plugin.com.energizedwork.webdriver-binaries:webdriver-binaries-gradle-plugin:1.1"
        classpath "gradle.plugin.com.energizedwork:idea-gradle-plugins:1.4"
        //classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.6"
    }
}

version "0.1"
group "hellokitty"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.energizedwork.webdriver-binaries"
apply plugin:"com.energizedwork.idea-project-components"
apply plugin:"asset-pipeline"
apply plugin:"org.grails.grails-gsp"

ext {
    gradleWrapperVersion = '3.5'
    elasticsearchVersion = '5.4.1'
    elasticsearchAttachmentsVersion = '5.4.1'
}

ext['elasticsearch.version'] = '5.4.1'

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    //compile "org.springframework.boot:spring-boot-starter-tomcat"
    provided "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-web-boot"
    compile "org.grails:grails-logging"
    compile "org.grails:grails-plugin-rest"
    compile "org.grails:grails-plugin-databinding"
    compile "org.grails:grails-plugin-i18n"
    compile "org.grails:grails-plugin-services"
    compile "org.grails:grails-plugin-url-mappings"
    compile "org.grails:grails-plugin-interceptors"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:async"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:events"
    //compile "org.grails.plugins:hibernate5"
//    compile "org.hibernate:hibernate-core:5.1.5.Final"
    compile 'org.grails.plugins:spring-security-core:3.2.0'
    compile "org.grails.plugins:gsp"

    //compile "org.grails.plugins:elasticsearch:2.4.0"
    compile "org.grails.plugins:mongodb"
//    compile 'org.grails.plugins:mongodb:6.1.4'
    compile("org.grails:gorm-mongodb-spring-boot:6.1.2.RELEASE")

    compile 'org.grails.plugins:elasticsearch:2.4.0.RC1'
    //compile 'org.elasticsearch:elasticsearch:5.4.1'

    console "org.grails:grails-console"
    profile "org.grails.profiles:web"
//    compile 'org.grails.plugins:elasticsearch:2.4.0.RC1'
    runtime 'org.elasticsearch.plugin:mapper-attachments:2.4.6'
    runtime "org.glassfish.web:el-impl:2.1.2-b03"
    //runtime "com.h2database:h2"
    runtime "org.apache.tomcat:tomcat-jdbc"
    runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.6"

    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-web-testing-support"
    testCompile "org.grails.plugins:geb"
    testRuntime "org.seleniumhq.selenium:selenium-chrome-driver:3.6.0"
    testRuntime "org.seleniumhq.selenium:selenium-firefox-driver:3.6.0"
    testRuntime "org.seleniumhq.selenium:selenium-remote-driver:3.6.0"
    testRuntime "org.seleniumhq.selenium:selenium-api:3.6.0"
}

bootRun {
    jvmArgs('-Dspring.output.ansi.enabled=always')
    addResources = true
}

webdriverBinaries {
    chromedriver '2.32'
    geckodriver '0.18.0'
}

tasks.withType(Test) {
    systemProperty "geb.env", System.getProperty('geb.env')
    systemProperty "webdriver.chrome.driver", System.getProperty('webdriver.chrome.driver')
    systemProperty "webdriver.gecko.driver", System.getProperty('webdriver.gecko.driver')
}

assets {
    minifyJs = true
    minifyCss = true
}

这是我的application.yml文件

---
grails:
    profile: web
    codegen:
        defaultPackage: hellokitty
    spring:
        transactionManagement:
            proxies: false
    gorm:
        reactor:
            # Whether to translate GORM events into Reactor events
            # Disabled by default for performance reasons
            events: false
info:
    app:
        name: '@info.app.name@'
        version: '@info.app.version@'
        grailsVersion: '@info.app.grailsVersion@'
spring:
    main:
        banner-mode: "off"
    groovy:
        template:
            check-template-location: false

# Spring Actuator Endpoints are Disabled by Default
endpoints:
    enabled: false
    jmx:
        enabled: true

---
grails:
    mime:
        disable:
            accept:
                header:
                    userAgents:
                        - Gecko
                        - WebKit
                        - Presto
                        - Trident
        types:
            all: '*/*'
            atom: application/atom+xml
            css: text/css
            csv: text/csv
            form: application/x-www-form-urlencoded
            html:
              - text/html
              - application/xhtml+xml
            js: text/javascript
            json:
              - application/json
              - text/json
            multipartForm: multipart/form-data
            pdf: application/pdf
            rss: application/rss+xml
            text: text/plain
            hal:
              - application/hal+json
              - application/hal+xml
            xml:
              - text/xml
              - application/xml
    urlmapping:
        cache:
            maxsize: 1000
    controllers:
        defaultScope: singleton
    converters:
        encoding: UTF-8
    views:
        default:
            codec: html
        gsp:
            encoding: UTF-8
            htmlcodec: xml
            codecs:
                expression: html
                scriptlets: html
                taglib: none
                staticparts: none
endpoints:
    jmx:
        unique-names: true

---
#hibernate:
#    cache:
#        queries: false
#        use_second_level_cache: false
#        use_query_cache: false
#dataSource:
#    pooled: true
#    jmxExport: true
#    driverClassName: org.h2.Driver
#    username: sa
#    password: ''

elasticSearch:
    date:
        formats: ["yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]
    client.hosts:
        - {host: localhost, port: 9300}
    datastoreImpl: mongoDatastore
    defaultExcludedProperties: ['password']
    disableAutoIndex: false
    index:
        compound_format: true
    unmarshallComponents: true
    searchableProperty:
        name: searchable
    includeTransients: false

environments:
    development:
        grails:
            serverURL: http://localhost:8080
            mongodb:
                host: "localhost"
                port: 27017
                username: ""
                password: ""
                databaseName: "people"
            elasticSearch:
                client:
                    mode: local
                    transport.sniff: true
                bulkIndexOnStartup: true
                datastoreImpl: mongoDatastore
    production:
        grails:
            mongodb:
                host: "localhost"
                port: 27017
                username: ""
                password: ""
                databaseName: "people"
            elasticSearch:
                client:
                  mode: node

但是我得到了这个错误。我无法理解如何实施 ACTION 它建议

/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java ...
| Running application... 

Configuring Spring Security Core ...
... finished configuring Spring Security Core

2018-02-07 19:30:54.622 ERROR --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field mappingContext in grails.plugins.elasticsearch.mapping.DomainReflectionService required a single bean, but 2 were found:
        - grailsDomainClassMappingContext: defined in null
        - mongoMappingContext: defined by method 'getMappingContext' in null


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
| Error Failed to start server (Use --stacktrace to see the full trace)

Process finished with exit code 1

任何人都可以给我任何建议吗?谢谢

1 个答案:

答案 0 :(得分:0)

实际上,即使我也遇到了同样的问题,因此我创建了一个由Grails团队解决的问题。

https://github.com/puneetbehl/elasticsearch-grails-plugin/issues/26

因此,只需将您的Elasticsearch版本更新为2.4.2或仅替换为以下依赖项即可。

编译“ org.grails.plugins:elasticsearch:2.4.2”。

感谢Puneet Behl对其进行了修复并将其修复到2.4.2版本。