Grails 可搜索:搜索成员对象中的特定字段?

发布于 2024-09-25 06:28:54 字数 377 浏览 5 评论 0原文

使用 Grails Searchable 插件,我获得了以下类:

class Person {
 static searchable = {
  address component: true
    }
}

并且:

class Address {
 static searchable = {
  root false
 }
 String country
}

我想对来自特定国家/地区的人员进行特定搜索。 “国家:NL”不起作用。 “地址:国家:NL”也不起作用。我找不到任何有关此语法的信息。有什么想法吗?

我想我必须在可搜索闭包中做一些聪明的索引或其他一些技巧,但我就是找不到它。

Using the Grails Searchable plugin, I've got these classes:

class Person {
 static searchable = {
  address component: true
    }
}

and:

class Address {
 static searchable = {
  root false
 }
 String country
}

I want to do a specific search for persons from a specific country. "country:NL" doesn't work. "address:country:NL" doesn't work either. I can't find anything about the syntax for this. Any ideas?

I think I'll have to do some clever indexing or some other trick in the searchable closure, but I just can't find it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2024-10-02 06:28:54

我使用您的两个类创建了一个基本应用程序(Grails 1.3.5,Searchable 0.5.5.1),并且搜索“country:NL”对我有用。您是否记得在尝试搜索之前调用index()?

grails create-app search
grains install-plugin searchable

人员:

class Person {
  static searchable = {
    address component: true
  }

  Address address
}

地址:

class Address {
  static belongsTo = Person
  static searchable = {
    root false
  }
  String country
}

Bootstrap:

class BootStrap {

    def init = { servletContext ->

      def p1 = new Person(address:new Address(country:'NL')).save()
      def p2 = new Person(address:new Address(country:'DE')).save()
      def p3 = new Person(address:new Address(country:'NZ')).save()

      Person.index()

    }

    def destroy = {
    }
}

然后我浏览到 /searchable 并搜索国家:NL 并返回了人员 1。

如果您想了解 Searchable 在字段/索引等方面的幕后功能 - Luke 是一个非常方便的工具(只需下载可执行 JAR):http://code.google.com/p/luke/

索引文件

<user.home>/.grails/projects/<project name>/searchable-index/development/index

欢呼

I created a basic app (Grails 1.3.5, Searchable 0.5.5.1) with your two classes and searching for 'country:NL' works for me. Did you remember to call index() before trying to search?

grails create-app search
grains install-plugin searchable

Person:

class Person {
  static searchable = {
    address component: true
  }

  Address address
}

Address:

class Address {
  static belongsTo = Person
  static searchable = {
    root false
  }
  String country
}

Bootstrap:

class BootStrap {

    def init = { servletContext ->

      def p1 = new Person(address:new Address(country:'NL')).save()
      def p2 = new Person(address:new Address(country:'DE')).save()
      def p3 = new Person(address:new Address(country:'NZ')).save()

      Person.index()

    }

    def destroy = {
    }
}

Then I browsed to to /searchable and searched for country:NL and got person 1 returned.

If you want to see what Searchable is doing under the covers with regards to fields/indexes etc - Luke is a very handy tool (just download the executable JAR): http://code.google.com/p/luke/

The index files are in

<user.home>/.grails/projects/<project name>/searchable-index/development/index

cheers

Lee

小帐篷 2024-10-02 06:28:54

有效的丑陋解决方案:不要依赖 Searchable。目前,我首先执行 Person.search(params.query, [max:99999]).results,然后执行简单的 .findAll 按国家/地区查找,然后执行 .sublist() 以使分页正常工作再次。

遗憾的是,很难让如此明显的东西与 Searchable 一起使用。

我还没有开始工作的另一个解决方案是使country成为返回address.country的Person上的临时属性。开箱即用,我不知道如何修复它。

如果有人对我有任何更好的解决方案,我很想听听。

The ugly solution that works: don't rely on Searchable. At the moment I first do a Person.search(params.query, [max:99999]).results, then do simple .findAll to find by country and .sublist() to get paging to work again.

It's a shame it's so hard to get something so obvious to work with Searchable.

Another solution I haven't gotten to work is making country a transient property on Person that returns address.country. Didn't work out of the box, and I have no idea on how to fix it.

If anyone has any prettier solutions for me, I'd love to hear them.

巾帼英雄 2024-10-02 06:28:54

我是 grails 新手,但为什么必须使用可搜索插件?

简单的 1:1 或 1:n 关系有什么问题?

    package com.test
class Person {

        static constraints = {
          name (nullable:false)
          address (nullable:true)
        }

        String name
        Address address

        String toString() {
          "name[" + name + "]. address[" + address + "]"
        }

        static mapping = {
          address lazy:false
        }
    }


    class Address {

        static constraints = {
          country (nullable:false)
          town   (nullable:false)
        }
        String country
        String town
        //Person person

        static  belongsTo = [person:Person]
    //    static  belongsTo = Person

        String toString(){
          "town[" + town + "], country[" + country + "]"
        }
    }

        package com.test

        import grails.test.*

        class PersonIntegrationTestTests extends GrailsUnitTestCase {
            protected void setUp() {
                super.setUp()
            }

            protected void tearDown() {
                super.tearDown()
            }

            void testSomething() {
              def bill = new Person(name:'bill', address:new Address(town:'york', country:'UK')).save()
              def fred = new Person(name:'fred', address:new Address(town:'leeds', country:'UK')).save()
              def bjork = new Person(name:'helen', address:new Address(town:'helsinki', country:'finland')).save()
              def gustav = new Person(name:'john', address:new Address(town:'helsinki', country:'finland')).save()

              List ukAddresses = Address.findAllByCountry('UK') // find all by country

              println "num addresses-" + ukAddresses.size()

              for (int i in 0..<ukAddresses.size())
              {
                println "found person:" + ukAddresses[i].person
              }

              assertNotNull "bill can not ne null", bill
              assertTrue bill.validate() && !bill.hasErrors()
              assertTrue fred.validate() && !fred.hasErrors()
              assertTrue bjork.validate() && !bjork.hasErrors()
              assertTrue gustav.validate() && !gustav.hasErrors()

              assertEquals 2, ukAddresses.size()
            }
        }

I am new to grails but why do you have to use the searchable plugin ?

what is wrong with a simple 1:1 or 1:n relationship ?

    package com.test
class Person {

        static constraints = {
          name (nullable:false)
          address (nullable:true)
        }

        String name
        Address address

        String toString() {
          "name[" + name + "]. address[" + address + "]"
        }

        static mapping = {
          address lazy:false
        }
    }


    class Address {

        static constraints = {
          country (nullable:false)
          town   (nullable:false)
        }
        String country
        String town
        //Person person

        static  belongsTo = [person:Person]
    //    static  belongsTo = Person

        String toString(){
          "town[" + town + "], country[" + country + "]"
        }
    }

        package com.test

        import grails.test.*

        class PersonIntegrationTestTests extends GrailsUnitTestCase {
            protected void setUp() {
                super.setUp()
            }

            protected void tearDown() {
                super.tearDown()
            }

            void testSomething() {
              def bill = new Person(name:'bill', address:new Address(town:'york', country:'UK')).save()
              def fred = new Person(name:'fred', address:new Address(town:'leeds', country:'UK')).save()
              def bjork = new Person(name:'helen', address:new Address(town:'helsinki', country:'finland')).save()
              def gustav = new Person(name:'john', address:new Address(town:'helsinki', country:'finland')).save()

              List ukAddresses = Address.findAllByCountry('UK') // find all by country

              println "num addresses-" + ukAddresses.size()

              for (int i in 0..<ukAddresses.size())
              {
                println "found person:" + ukAddresses[i].person
              }

              assertNotNull "bill can not ne null", bill
              assertTrue bill.validate() && !bill.hasErrors()
              assertTrue fred.validate() && !fred.hasErrors()
              assertTrue bjork.validate() && !bjork.hasErrors()
              assertTrue gustav.validate() && !gustav.hasErrors()

              assertEquals 2, ukAddresses.size()
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文