通过has_parent搜索和另一个查询elasticsearch

时间:2017-06-05 21:32:13

标签: elasticsearch

我有父/子关系,我需要根据父ID和子属性过滤子项。但是,我可以按父ID过滤子项,但我不能按任何子属性进行过滤。

这是我的搜索:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": "beer"
                    }
                }
            ],
            "must": [
                {
                    "has_parent": {
                        "type": "pubs",
                        "score_mode": "score",
                        "query": {
                            "ids": {
                                "values": ["AVx3Ruk03JWWfjjJokHs"]
                            }
                        }
                    }
                }
            ]
        }
    }
}

但是我得到了这样的结果:

{
    "took": 98,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.5,
        "hits": [
            {
                "_index": "wildwest",
                "_type": "drinks",
                "_id": "AVx3Xc8g3JWWfjjJokHx",
                "_score": 0.5,
                "_routing": "AVx3Ruk03JWWfjjJokHs",
                "_parent": "AVx3Ruk03JWWfjjJokHs",
                "_source": {
                    "name": "Milk",
                    "description": "Milk description",
                    "unit_price": 2.9,
                    "amount": 20
                }
            }
        ]
    }
}

我做错了什么? 我正在使用Elasticsearch 2.4。

注意:我试图将所有过滤器放入内部应该查询,但我从另一家酒吧获得饮料。更改为必须不查询任何内容。

2 个答案:

答案 0 :(得分:0)

查询

POST parent_child_index/drinks/_search
    {
        "query": {
            "bool": {
                "must": [{
                        "term": {
                            "name": {
                                "value": "milk"
                            }
                        }
                    },
                    {
                        "has_parent": {
                            "type": "pubs",
                            "score_mode": "score",
                            "query": {
                                "ids": {
                                    "values": ["AVx8F96XkBWZrYmFvTpK"]
                                }
                            }
                        }
                    }
                ]
            }
        }
    }

答案 1 :(得分:0)

我需要一个过滤我的饮料的查询条件:parent_id AND(名称或描述或...)。基于@user3775217回答和elasticsearch文档,我做了这个查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "query": {
                        "bool": {
                            "should": [
                                {
                                    "term": {
                                        "name": "milk"
                                    }
                                },
                                {
                                    "term": {
                                        "description": "milk"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "has_parent": {
                        "parent_type": "pubs",
                        "score_mode": "score",
                        "query": {
                            "ids": {
                                "values": ["AVx3Ruk03JWWfjjJokHs"]
                            }
                        }
                    }
                }
            ]
        }
    }
}

仅使用必须查询我只使用AND条件过滤我的饮料,所以我添加了必须在查询中查询添加OR条件。