小编典典

如何只检索匹配的子?

elasticsearch

考虑一个非常简单的模型,其中我们有位置,每个位置可以有零个或多个事件。位置将具有名称,描述和地理位置数据(经度/纬度)之类的属性。事件应附加到一个位置(其父级),并应具有名称和描述。

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

我想要做的是查询子文档(事件),以对其名称和描述进行全文搜索。我想找回匹配的事件,还可以获取其父位置的名称。我还想通过位置坐标缩小结果集。我不想获取与查询不匹配的任何事件。在Elastic
Search中有可能吗?我应该使用哪种类型的查询?

我曾尝试将事件作为数组属性放置在location下(请参见上文)并使用nested查询,但它不会返回我想要的结果类型(我认为它会返回整个位置,包括所有事件,甚至是不匹配的事件我的查询)。我尝试将事件放入提供_parent属性的单独索引(映射?)中,然后top_children对位置执行查询,但是没有任何结果。

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

谁能给我一些启示?我不知道从哪里开始


阅读 250

收藏
2020-06-22

共1个答案

小编典典

这是解决我的问题的可行方法,也许对某人有用。

位置映射:

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

展品图:

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

查询:

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

您应该使用该_parent字段进行查询,并将该字段传递给您要将展览品限制到的位置的ID数组。

2020-06-22