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

前端之家收集整理的这篇文章主要介绍了nosql – 在弹性搜索上查询多级嵌套字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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).

原文链接:https://www.f2er.com/nosql/203294.html

猜你在找的NoSQL相关文章