在Elasticsearch中查询子文档

时间:2015-08-21 19:15:46

标签: elasticsearch

作为一个简单的例子,我有" book"存储在弹性搜索中的文档,以便请求GET /myindex/book/1返回类似

的内容
{
  "id": 1,
  "title": "Hamlet",
  "author": "William Shakespeare",
  "pages": [
    {"page_id": 1, "contents": "hello, world . . . this story is very well written"},
    {"page_id": 5, "contents": "goodbye, world . . . i am done writing this book"}
  ]
}

我想要做的是运行某种查询,这将使我获得具有单独匹配页面的记录。这就像GET /myindex/book/_mySpecialQuery?q=hello会有结果[{"page_id": 1, "contents": "hello, world . . . this story is very well written", "_parent": 1}]GET /myindex/book/_mySpecialQuery?q=world会有结果[{"page_id": 1, "contents": "hello, world . . . this story is very well written", "_parent": 1}, {"page_id": 5, "contents": "goodbye, world . . . i am done writing this book", "_parent": 1}],其中_parent是图书的ID。

我不能轻易地对数据进行非规范化,因为它来自Mongo(通过mongo-connector)。

(这看起来应该很简单,但我还没有看到任何好方法 - 如果我只是在查看错误的术语等,请用链接做评论。)

1 个答案:

答案 0 :(得分:1)

您使用match query尝试requesting fields;它应该做的工作:

{
    "query": {
        "match": {
              "pages.contents": "hello"
         }
    },
    "fields": [
        "pages.page_id",
        "pages.contents",
        "id"
    ]
}

您的结果将在hits中返回,并且看起来像这样:

[
    {
    "_index": <YOUR_INDEX_NAME>,
    "_type": <YOUR_TYPE_NAME>,
    "_id": 1,
    "_score": <SOME SCORE VALUE>,
    "fields": {
        "pages.page_id": [
            1
        ],
        "pages.contents": [
            "hello, world . . . this story is very well written"
        ],
        "id": [
              1
        ]
    }
    }
]

注意:我担心这本书的id仍将被称为id(在字段中,作为源的一部分)和_id(作为文档的id),但是不是_parent(就像你所希望的那样)