How can I retrieve matching children only?

后端 未结 1 2111
囚心锁ツ
囚心锁ツ 2021-02-15 14:19

Consider a very simple model where we have locations and each location can have zero or more events. A location would have properties such as name, description and geo point dat

1条回答
  •  野的像风
    2021-02-15 14:51

    Here's the working solution to my problem, perhaps it will be useful to somebody.

    Location mapping:

    {
        "location" : {
            "properties": {
                "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" },
                "description": { "type": "string", "analyzer": "snowball" },
                "geo": { "type": "geo_point" }
            }
        }
    }
    

    Exhibit mapping:

    {
        "exhibit": {
            "_parent": { "type": "locations" },
            "properties": {
                "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" },
                "description": { "type": "string", "analyzer": "snowball" }
            }
        }
    }
    

    Query:

    {
        "fields": [ "_parent", "name", "_source" ],
        "query": {
            "bool": {
                "should": [ 
                    { "text": { "name": "candy" } },
                    { "text": { "description": "candy" } } 
                ]
            }
        },
        "filter": { 
            "and": [
                {
                    "terms" : {
                        "_parent": [ "4e7089a9b97d640b30695b7a", "4e7089eeb97d640b30695b7b" ]
                    }
                },
                { "range": { "start": { "lte": "2011-09-22" } } },
                { "range": { "end": { "gte": "2011-09-22" } } }
            ]
        }
    }
    

    You should query using the _parent field and passing it an array of IDs of locations to which you want to limit the exhibits.

    0 讨论(0)
提交回复
热议问题