elasticsearch - 我怎样才能只检索匹配的 child ?

标签 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 中可能吗?我应该使用什么类型的查询?

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

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

谁能解释一下?我不知道从哪里开始...

最佳答案

这是我的问题的有效解决方案,也许对某些人有用。

位置映射:

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

关于elasticsearch - 我怎样才能只检索匹配的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7431889/

相关文章:

c# - Elasticsearch/Nest - 将 MatchPhrase 与 OnFieldsWithBoost 结合使用

elasticsearch - 弹性搜寻(DSL):字词不符任何内容

C# NEST Elastic Search 查询多个条件

elasticsearch - 在elasticsearch中,在映射创建期间完全定义映射有多重要?

elasticsearch - 安装后 Elasticsearch 服务无法启动

ElasticSearch 删除查询 - 使用术语和范围进行过滤

mongodb - 从MongoDB到Elasticsearch的多语言文本搜索

elasticsearch - 用Elasticsearch搜索完全匹配的文本

database - 我们可以通过替换Postgres来使用Elastic Search作为后端数据存储吗?

java - 创建一个简单的工作spring boot