查询“深度”嵌套字段

时间:2018-02-20 10:12:40

标签: elasticsearch nested

我正在尝试在嵌套在属性document.sections[].content下的字段中查找包含特定术语的所有文档,但我无法弄清楚如何查询它。

我正在使用elasticsearch 5.2.0。

(对不起Json的Big Walls,我试图保持简短,没有删除有用的东西)

我的映射看起来像这样:

//curl http://es/my_documents/_mappings
{
    "my_documents": {
        "mappings": {
            "docs": {
                "dynamic": "strict",
                "properties": {
                    "document": {
                        "type": "nested",
                        "include_in_root": true,
                        "properties": {
                            // other stuff
                            "sections": {
                                "type": "nested",
                                "properties": {
                                    "content": {
                                        "type": "text",
                                        "similarity": "BM25",
                                        "analyzer": "french"
                                    },
                                    "section_id": {
                                        "type": "keyword"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我正在尝试根据document.sections.content属性查找文档,但elasticsearch永远不会返回任何内容。

举个例子,我有这个文件:

//curl http://es/my_documents/docs/doc_id/
{
    "_index": "my_documents",
    "_type": "docs",
    "_id": "doc_id",
    "_version": 1,
    "found": true,
    "_source": {
        "document": {
            // other stuff
            "sections": [
                {
                    "content": "  \n  \nI am a [POTATO]  \na potato is what i am",
                    "section_id": "main"
                }
            ]
            // more stuff
        }
    }
}

我正在进行此查询(使用_explain API):

//http://es/my_documents/docs/doc_id/_explain
    {
    "query":{
        "nested": {
            "path": "document.sections",
            "query": {
                "term": {
                    "sections.content": "Potato"
                }
            }
        }
    }
}

我收到了这个回复:

    {
    "_index": "my_documents",
    "_type": "docs",
    "_id": "doc_id",
    "matched": false,
    "explanation": {
        "value": 0,
        "description": "Failure to meet condition(s) of required/prohibited clause(s)",
        "details": [
            {
                "value": 0,
                "description": "no match on required clause (ToParentBlockJoinQuery (+sections.content:Potato #_type:__document.sections))",
                "details": [
                    {
                        "value": 0,
                        "description": "Not a match",
                        "details": []
                    }
                ]
            },
            {
                "value": 0,
                "description": "match on required clause, product of:",
                "details": [
                    {
                        "value": 0,
                        "description": "# clause",
                        "details": []
                    },
                    {
                        "value": 1,
                        "description": "_type:docs, product of:",
                        "details": [
                            {
                                "value": 1,
                                "description": "boost",
                                "details": []
                            },
                            {
                                "value": 1,
                                "description": "queryNorm",
                                "details": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

那么,为什么会失败,以及它是如何运作的?

1 个答案:

答案 0 :(得分:0)

我认为问题是您需要包含2级嵌套查询。像这样:

{
    "query":{
        "nested": {
            "path": "document",
            "query": {
                "nested": {
                    "path": "document.sections",
                    "query": {
                        "term": {
                            "document.sections.content": "Potato"
                        }
                    }
                }
            }
        }
    }
}