2
votes

I have a parent/child relationship and I need to filter children based on parent ID and child properties. However, I can filter children by parent ID but I can not filter by any child properties.

Here is my search:

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

But I was given results like this:

{
    "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
                }
            }
        ]
    }
}

What am I doing wrong? I'm using Elasticsearch 2.4.

NOTE: I tried to put all filters inside should query, but I was given drinks from another pub. Changing to must query nothing is returned.

2

2 Answers

0
votes

Query

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

I needed a query that filters my drinks with this condition: parent_id AND (name OR description OR...). Based on @user3775217 answer and elasticsearch documentation I did this query:

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

Using only must query I filter my drinks only with AND condition, so I added should query within must query to add OR condition.