文章 八月 26, 2016

elasticsearch5.x取消了query/filtered结构

文章字数 2.3k 阅读约需 2 mins. 阅读次数 0

升级es5.x之后,执行搜索是遇到了如下错误

查看了官方文档后发现,在es2.x时,我们可以使用如下的DSL结构

1.search API中只能包含 query 语句,所以我们需要用 filtered 来同时包含 “query” 和 “filter” 子句:

{
    "filtered": {
        "query":  { "match": { "email": "business opportunity" }},
        "filter": { "term":  { "folder": "inbox" }}
    }
}

2.我们在外层再加入 query 的上下文关系:

GET /_search
{
    "query": {
        "filtered": {
            "query":  { "match": { "email": "business opportunity" }},
            "filter": { "term": { "folder": "inbox" }}
        }
    }
}

但是值得注意的是,在es5.x中已经抛弃了这一种结构,取而代之的是query/bool/(must/should/filter…)结构

POST _search
{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "user": "kimchy"
                }
            },
            "filter": {
                "term": {
                    "tag": "tech"
                }
            },
            "must_not": {
                "range": {
                    "age": {
                        "gte": 10,
                        "lte": 20
                    }
                }
            },
            "should": [{
                    "term": {
                        "tag": "wow"
                    }
                },
                {
                    "term": {
                        "tag": "elasticsearch"
                    }
                }
            ],
            "minimum_should_match": 1,
            "boost": 1.0
        }
    }
}

参考地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html


上一篇:
下一篇:
0%