如何使用标准组合构建弹性查询?

时间:2014-09-23 13:19:41

标签: elasticsearch

我刚接触过elatic搜索,不幸的是,elasticearch.com的文档还不够好。我知道如何使用术语:

{
    "term" : { "user" : "kimchy" }
}

请您帮忙用这些标准构建我的查询? (user =" ehsan"或user =" afsaneh")和((年龄在10到20岁之间)或(年龄在40到50岁之间))

1 个答案:

答案 0 :(得分:1)

你应该看看:

  • bool查询(documentation):它将帮助您使用条款must / should / {{1}将不同的标准与AND / OR / NO结合起来}
  • must_not过滤器(documentation):其目标是排除与您设置的值间隔不匹配的文档。

以下查询应该可以解决问题:

range

然而,imho,Elasticsearch文档非常好。 您应该花时间阅读更多内容以了解如何构建其他查询:像这样(特别是关于difference between query and filters)。

修改:两个{ "query": { "filtered": { "query": { "bool": { "should": [ { "term": { "user": "ehsan" } }, { "term": { "user": "afsaneh" } } ] } }, "filter": { "or": { "filters": [ { "range": { "age": { "from": 10, "to": 20 } } }, { "range": { "age": { "from": 40, "to": 50 } } } ] } } } } } 查询可以替换为单个term,如下所示:

terms
相关问题