0

As a simplified example, I have "book" documents stored in elastic search so that a request GET /myindex/book/1 returns something like

{
  "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"}
  ]
}

What I'd like to do is run some sort of query that will get me records with individual matching pages. That is something like GET /myindex/book/_mySpecialQuery?q=hello would have results [{"page_id": 1, "contents": "hello, world . . . this story is very well written", "_parent": 1}] and GET /myindex/book/_mySpecialQuery?q=world would have results [{"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}] where _parent is the book's ID.

I cannot easily denormalize the data since it is coming from Mongo (via mongo-connector).


(This seems like it should be simple but I haven't seen any good ways to do this -- please do comment with a link if I'm just looking at the wrong terminology etc.)

1 Answer 1

1

You try a match query with requesting fields; it should do the job:

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

Your result will be returned in the hits and is going to look like this:

[
    {
    "_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
        ]
    }
    }
]

NOTE: I'm afraid the book's id is going to be still called id (in the fields, as part of the source), and _id (as id of the document), but not _parent (like you wished)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.