nosql - 在 Elastic Search 上查询多级嵌套字段

标签 nosql nested elasticsearch pyes

我是 Elastic Search 和非 SQL 范式的新手。 我一直在学习 ES 教程,但有一件事我无法付诸实践。

在下面的代码中(我使用 PyES 与 ES 交互)我创建了一个文档,它有一个嵌套字段(主题),其中包含另一个嵌套字段(概念)。

from pyes import *

conn = ES('127.0.0.1:9200')  # Use HTTP

# Delete and Create a new index.
conn.indices.delete_index("documents-index")
conn.create_index("documents-index")

# Create a single document.
document = {
    "docid": 123456789,
    "title": "This is the doc title.",
    "description": "This is the doc description.",
    "datepublished": 2005,
    "author": ["Joe", "John", "Charles"],
    "subjects": [{
                    "subjectname": 'subject1',
                    "subjectid": [210, 311, 1012, 784, 568],
                    "subjectkey": 2,
                    "concepts": [
                                    {"name": "concept1", "score": 75},
                                    {"name": "concept2", "score": 55}
                                  ]
                },
                {
                    "subjectname": 'subject2',
                    "subjectid": [111, 300, 141, 457, 748],
                    "subjectkey": 0,
                    "concepts": [
                                    {"name": "concept3", "score": 88},
                                    {"name": "concept4", "score": 55},
                                    {"name": "concept5", "score": 66}
                                  ]
                }],
    }


# Define the nested elements.
mapping1 = {
            'subjects': {
                'type': 'nested'
            }
        }
mapping2 = {
            'concepts': {
                'type': 'nested'
            }
        }
conn.put_mapping("document", {'properties': mapping1}, ["documents-index"])
conn.put_mapping("subjects", {'properties': mapping2}, ["documents-index"])


# Insert document in 'documents-index' index.
conn.index(document, "documents-index", "document", 1)

# Refresh connection to make queries.
conn.refresh()

我能够查询subjects 嵌套字段:

query1 = {
    "nested": {
        "path": "subjects",
        "score_mode": "avg",
        "query": {
            "bool": {
                "must": [
                    {
                        "text": {"subjects.subjectname": "subject1"}
                    },
                    {
                        "range": {"subjects.subjectkey": {"gt": 1}}
                    }
                ]
            }
        }
    }
}


results = conn.search(query=query1)
for r in results:
    print r  # as expected, it returns the entire document.

但我不知道如何根据概念嵌套字段进行查询。

ES documentation 指的是

Multi level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level (and not root) if it exists within another nested query.

因此,我尝试使用以下格式构建查询:

query2 = {
        "nested": {
            "path": "concepts",
            "score_mode": "avg",
            "query": {
                "bool": {
                    "must": [
                        {
                            "text": {"concepts.name": "concept1"}
                        },
                        {
                           "range": {"concepts.score": {"gt": 0}}
                        }
                    ]
                }
            }
        }
}

返回 0 个结果。

我不知道遗漏了什么,也没有找到任何基于两层嵌套的查询示例。

最佳答案

好的,在尝试了一些组合之后,我终于使用以下查询得到了它:

query3 = {
    "nested": {
        "path": "subjects",
        "score_mode": "avg",
        "query": {
            "bool": {
                "must": [
                    {
                        "text": {"subjects.concepts.name": "concept1"}
                    }
                ]
            }
        }
    }
}

因此,无论嵌套属性级别如何,嵌套路径 属性 (subjects) 始终相同,并且在查询定义中我使用了属性的完整路径 ( subject.concepts.name)。

关于nosql - 在 Elastic Search 上查询多级嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14633952/

相关文章:

php - Redis(键/值数据库)和关联数组之间的区别?

nested - 大查询将重复字段中的结构更新为 null

r - 嵌套列表 r 中的子集

nested - 创建一个包含嵌套环境的新环境

python - Elasticsearch 阻止对 Markdown 超链接建立索引

mongodb - 哪些 NoSQL 数据库(如果有)可以为查询结果集提供 *changes* 流?

java - Couchbase:net.spy.memcached.internal.CheckedOperationTimeoutException

python - 处理 ElasticSearch 最 Pythonic 的方法是什么?

elasticsearch - 字段名称约定

java - 产品的 HBase 架构文件