【问题标题】:Query subdocuments in Elasticsearch在 Elasticsearch 中查询子文档
【发布时间】:2015-08-21 19:15:46
【问题描述】:

作为一个简化的示例,我将“书”文档存储在弹性搜索中,以便请求 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)。


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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您尝试使用match queryrequesting 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 (如你所愿)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      相关资源
      最近更新 更多