【问题标题】:How do I have to write a Search Query in ElasticSearch?如何在 ElasticSearch 中编写搜索查询?
【发布时间】:2013-08-12 17:46:44
【问题描述】:

我使用 Grails ElasticSearch 插件并希望使用以下查询:

"bool" : {
    "must" : {
        "term" : { "user" : "kimchy" }
    },
    "must_not" : {
        "range" : {
            "age" : { "from" : 10, "to" : 20 }
        }
    },
    "should" : [
        {
            "term" : { "tag" : "wow" }
        },
        {
            "term" : { "tag" : "elasticsearch" }
        }
    ],
    "minimum_should_match" : 1,
    "boost" : 1.0
}

使用 Grails 插件中的 groovy api 我会写如下内容:

def res = userAgentIdentService.search() {
    "bool" {
        "must" {
            term("user" : "kimchy" )
        }
        "must_not"  {
            "range"  {
                age("from" : 10, "to" : 20 }
            }
        }
        "should" : [
            {
                term( "tag" : "wow" )
            }
            {
                term("tag" : "elasticsearch" )
            }
        ]
        "minimum_should_match" = 1
        "boost" = 1.0
    } 

}

我的查询不起作用!

  1. 必须在哪里定义 minimum_should_match 以及如何定义它?

  2. 如何以 grails / groovy 的方式编写“应该”:[ ... ] 方括号符号?

【问题讨论】:

    标签: spring grails groovy elasticsearch grails-2.0


    【解决方案1】:

    我认为您的搜索请求中缺少几个 json 级别。我认为您不能在不指定它是查询的情况下使用该查询(它也可以是一个过滤器,甚至是其他东西)。看看 groovy api 参考中的this example

    def search = node.client.search {
        indices "test"
        types "type1"
        source {
            query {
                terms(test: ["value1", "value2"])
            }
        }
    }
    

    【讨论】:

    • 可以用插件的搜索功能写查询吗?
    • 我用我提到的例子更新了我的答案。抱歉耽搁了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多