如何检索匹配的孩子? [英] How can I retrieve matching children only?

查看:143
本文介绍了如何检索匹配的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个非常简单的模型,我们有位置,每个位置可以有零个或多个事件。一个位置将具有诸如名称,描述和地理点数据(lon / lat)的属性。事件应附在一个位置(其父母),并应有一个名称和描述。

  {
位置:{
properties:{
name:{type:string,boost:2.0,analyzer:snowball},
描述:{type:string,analyzer:snowball},
geo:{type:geo_point},
展示:{
type:nested,
properties:{
name:{type:string,boost:2.0,analyzer:snowball ,
description:{type:string,analyzer:snowball}
}
}
}
}

我想要做的是查询执行的子文档(事件)对其名称和说明进行全文搜索。我想要获得匹配的事件,并能够得到他们的父位置的名字。我也想缩小位置坐标的结果集。我不想得到任何不符合查询的事件。是否可能在弹性搜索?我应该使用什么类型的查询?



我已经尝试将事件作为数组属性放置在位置(见上文),并使用嵌套查询,但它不返回我想要的结果种类(我认为它返回整个位置,包括所有的事件,甚至不符合我的查询)。我已经尝试将事件放在单独的索引(映射?)中,提供_parent属性,然后在位置执行 top_children 查询,但是我没有得到任何结果。 p>

  {
展览:{
_parent:{type:locations},
properties:{
name:{type:string,boost:2.0,analyzer:snowball},
description type:string,analyzer:snowball}
}
}
}

任何人都可以轻看一下吗?我不知道从哪里开始...

解决方案

这是我的问题的工作解决方案,也许这将是有用的有人。



位置图:

  {
位置:{
properties:{
name:{type:string,boost:2.0,analyzer:snowball},
描述:{type:string,analyzer:snowball},
geo:{type:geo_point}
}
}
}

展览地图:

  {
展览:{
_parent:{type:locations},
properties:{
name:{type:string,boost:2.0,analyzer:snowball},
description:{type:string,analyzer


}

查询: / p>

  {
fields:[_p $$$$$$$$$$$$名称:糖果}},
{text:{description:candy}}
]
}
},
:{
and:[
{
terms:{
_parent:[4e7089a9b97d640b30695b7a,4e7089eeb97d640b30695b7b]
}
$,
{range:{start:{lte:2011-09-22}}},
{range:{end gte:2011-09-22}}}
]
}
}

您应该使用 _parent 字段查询并传递一个要限制展览​​的位置ID数组。


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 data (lon/lat). An event should be attached to one location (its parent) and should have a name and description.

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

What I want to be able to do, is to query for the child documents (events) performing a full text search on their names and descriptions. I would like to get the matching events back and be able to also get their parent location's name. I would also like to narrow down the result set by location's coordinates. I don't want to get any events that do not match the query. Is that possible in Elastic Search? What types of queries should I use?

I have tried putting events as an array property under location (see above) and using the nested query but it does not return the kind of results I want (I think it returns the whole location, including all events, even the ones that do not match my query). I have tried putting events into a separate index (mapping?) providing the _parent property and then performing the top_children query on locations, but I don't get any results.

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

Could anyone shed some light? I don't know where to begin...

解决方案

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.

这篇关于如何检索匹配的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆