2
votes

I chose nested documents to realize a multilingual book search with common book data in root of the doc and edition data in nested docs. The mapping:

{
  "book": {
    "properties": {
      "bookinfo": {
        ...
      },
      "editions": {
        "type": "nested",
        "properties": {
          "editionid": {
            "type": "long",
            "store": "yes",
            "index": "no"
          },
          "title_author": {
            "type": "string",
            "store": "no",
            "index": "analyzed"
          },
          "title": {
            "type": "string",
            "store": "yes",
            "index": "not_analyzed"
          },
          "languageid": {
            "type": "short",
            "store": "yes",
            "index": "no"
          },
          "ratings": {
            "type": "integer",
            "store": "no"
          }
        }
      }
    }
  }
}

Different editions of one book go in the nested doc - that can be different languages but also just different publishers, isbn and so on. Sometimes even the title differs from editions in the same language.

When searching the document (on the title_author field) I need to know the other nested doc information like languageid and ratings to boost the matching score according to the users language skills and relevance of the edition.

The reason why I don't put every edition in a separate document is that I only want to have one hit (the best matching one) per book. And ElasticSearch doesn't have a UNIQUE functionality. And I need pagination. So whenever I change a result set after querying with double books inside, pagination of ElasticSearch breaks.

Nested sorting functionality doesn't seem to help here, since it sorts over all nested documents of one book.

How do I access the information of the matching nested doc?

And if that is not achievable, how could I solve this by a multi search?

1
The following may help, but I'm not entirely sure (haven't used top-children myself) elasticsearch.org/guide/en/elasticsearch/reference/current/… - Geert-Jan
To my understanding the top-children query only gives me aggregated information of all matching children, but not the information of the best matching one. - fisch
It's more a workaround but now I solved it like that: I created a second index with only editions where I include the bookid. Now I fetch only book related information in the first step without knowing what edition matched. In the second step I search on the editions index with a filter on the bookid and a limit (size) of 1 to get the best matching edition for every hit. All needed requests for editions I put in a multi search (elasticsearch.org/guide/en/elasticsearch/reference/current/…) It is a lot slower, but the best I found. - fisch

1 Answers

0
votes

In order to access the nested documents fields you can use:

doc['editions. languageid'].value

And for the boosting part, try some of the examples here:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

Is that what you're looking for?