保留过滤查询结果

时间:2017-04-28 18:31:26

标签: arrays elasticsearch filter

我使用以下查询查询索引:

GET /dbpedia201510/entity/_search
{
    "from": 0,
    "size": 10000,
    "query": {
        "bool": {
            "must": {
                "query_string":{
                    "fields": ["name","alias","pseudonyme"],
                    "query": "Elias~ Franck~",
                    "default_operator": "OR",
                    "fuzziness": "auto"
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "job.keyword":["Architect","Politician","Tailor"]
                            }
                        }
                    ]
                }
            }
        }
    }
}

job字段是一个字符串数组,查询的工作方式与预期的一样。然而,有些文档没有job字段,我也希望得到这些文档。

我该怎么做?

1 个答案:

答案 0 :(得分:4)

您需要在此使用should clause,当job字段存在并匹配字词查询 job字段不存在时,匹配GET /dbpedia201510/entity/_search { "from": 0, "size": 10000, "query": { "bool": { "must": { "query_string": { "fields": ["name","alias","pseudonyme"], "query": "Elias~ Franck~", "default_operator": "OR", "fuzziness": "auto" } }, "filter": { "bool": { "should": [ { "terms": { "job.keyword": ["Architect","Politician","Tailor"] } }, { "bool": { "must_not": { "exists": { "field": "job" } } } } ] } } } } }

您的最终查询应如下所示:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">