Grails 搜索机制

发布于 2024-11-16 03:22:10 字数 573 浏览 2 评论 0原文

对于我的网站,我需要做一个搜索机制,其中一些输入字段是:国家/地区、城市、日期之间(有或没有年份字段)、关键字等。

我的问题是,用户必须决定什么他们想要寻找。例如,如果他们只想介绍日期,或日期和城市,或城市和关键字..等。我真的不知道该怎么做,我的意思是,我知道如何一次搜索一件事,但我不知道如何才能做到这一点。

a)我是否需要像这样的东西:(if-else,if-else)然后为每个组合编写代码,或者有更简单的方法来做到这一点?

b)顺便说一句,我的搜索机制是按以下方式完成的(我以前从未做过搜索机制,所以我不知道这是否是最好的方法,也希望在这里得到一些评论和建议):

  class book{
    String a
    String b
    ...
    Date z

    String allAttributesTogether() {
    a + b + c + ... + z
    }

    }

然后在我的控制器中,我执行 double for 语句并交叉匹配搜索引入的单词和 allAttributesTogether() 的结果。

提前致谢,VA

For my website, i need to do a search mechanism, in which some of the entry field would be: Country, City, Between Dates (with or without year field), Keywords, etc etc.

My problem is, the user must decide what they wanna search for. For example, if they want to introduce just date, or date and city, or city and keyword.. etc. I dont really know how to do that, i mean, i know how to search for one thing at a time, but i'm not sure how can do this all-in-one.

a) Would i need like something like this: (if-else, if-else) and than write the code for each combination, or there is an easier way to do that?

b )Bytheway, my search mechanism is done the folowing way (i'v never done a search mechanism before, so i dont know if it is the best aproach, would apreciate some comments here also and suggestions):

  class book{
    String a
    String b
    ...
    Date z

    String allAttributesTogether() {
    a + b + c + ... + z
    }

    }

then in my controller, i do a double for statment and cross-match the introduced words for the search and the result of allAttributesTogether().

Thanks in advanced, VA

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

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

发布评论

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

评论(2

天荒地未老 2024-11-23 03:22:10

查看过滤器窗格插件。

Check out the filter pane plugin.

凉城凉梦凉人心 2024-11-23 03:22:10

当你说“搜索”时,我想到的是搜索引擎。但我认为你问的是查询数据库,对吧?

如果您谈论搜索机制,搜索引擎是一个很棒的工具。您可以看看 Lucene、Compass 和 ElasticSearch (ES) 等。 Compass和ES都是基于lucene的,但是抽象层次要高得多(更容易使用)。
我一直非常满意地使用 ElasticSearch。

如果您正在谈论查询数据库,那么您可以动态构建 HQL 查询。下面的方法应该位于控制器中,因为它使用 params 属性。没测试好么?

List allAttributesTogether() {
    def query = " select book from Book book "
    def queryParams = [:]
    def needsAnd = false

    if(params.a || params.b || params.z ){
       query += " where "
    }
    if(params.a){
       query += " book.a = :a "
       queryParams['a'] = params.a
       needsAnd = true
    }
    if(params.b){
       if(needsAnd) query += " and "
       query += " book.b = :b "
       queryParams['b'] = params.b
       needsAnd = true
    }
    if(params.a){
       if(needsAnd) query += " and "
       query += " book.z = :z "
       queryParams['z'] = params.z
    }

    return Book.executeQuery(query, queryParams)

}

还有一种选择是使用条件生成器 。您还可以使用“if”将子句添加到条件子句中。

When you say "search", comes to my mind search engines. But I think you are asking about querying the database, right?

If you are talking about search mechanisms, search engines are a great tool. You can take a look at Lucene, Compass, and ElasticSearch (ES) to name a few. Compass and ES are based on lucene, but are much higher in the abstraction level (easier to use).
I have been using ElasticSearch with great satisfaction.

If you are talking about querying the database, then you can just build a HQL query dynamically. The method bellow should be in a Controller, as it uses the params attribute. It is not tested ok?

List allAttributesTogether() {
    def query = " select book from Book book "
    def queryParams = [:]
    def needsAnd = false

    if(params.a || params.b || params.z ){
       query += " where "
    }
    if(params.a){
       query += " book.a = :a "
       queryParams['a'] = params.a
       needsAnd = true
    }
    if(params.b){
       if(needsAnd) query += " and "
       query += " book.b = :b "
       queryParams['b'] = params.b
       needsAnd = true
    }
    if(params.a){
       if(needsAnd) query += " and "
       query += " book.z = :z "
       queryParams['z'] = params.z
    }

    return Book.executeQuery(query, queryParams)

}

There is also the alternative of using Criteria builder. You can also use "if" to add clauses to your Criteria clauses.

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