加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

nosql – 在弹性搜索上查询多级嵌套字段

发布时间:2020-12-13 13:30:59 所属栏目:百科 来源:网络整理
导读:我是Elastic Search和非SQL范例的新手. 我一直在关注ES教程,但有一件事我无法工作. 在下面的代码中(我使用PyES与ES进行交互)我创建了一个带有嵌套字段(主题)的单个文档,其中包含另一个嵌套字段(概念). from pyes import *conn = ES('127.0.0.1:9200') # Use
我是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()

我能够查询主题嵌套字段:

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","query": {
                "bool": {
                    "must": [
                        {
                            "text": {"concepts.name": "concept1"}
                        },{
                           "range": {"concepts.score": {"gt": 0}}
                        }
                    ]
                }
            }
        }
}

返回0结果.

我无法弄清楚缺少什么,我没有找到任何基于两个嵌套级别的查询示例.

好吧,在尝试组合音后,我终于使用以下查询得到它:
query3 = {
    "nested": {
        "path": "subjects","query": {
            "bool": {
                "must": [
                    {
                        "text": {"subjects.concepts.name": "concept1"}
                    }
                ]
            }
        }
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读