3
votes

I'm fairly new to ES so forgive me if this is a common scenario that I haven't discovered yet.

I've got an index which contains documents and each document can be related to a specific event, so I have a nested event object in each document. Event -> Document is a one-to-many relationship.

When I am displaying a single document, I want to show "More like this" where more-like-this actually represents more documents from the same meeting, by the same author, or on the same topic. Author and Topic work fine, but although mlt_fields accepts "event.title" as a fieldname, it doesn't ever find any documents from the same event.

My mapping:

{
   "myindex": {
      "mappings": {
         "myitem": {
            "properties": {
               "authors": {
                  "type": "string",
                  "analyzer": "keyword"
               },
               "id": {
                  "type": "integer"
               },
               "title": {
                  "type": "string"
               },
               "topics": {
                  "type": "string",
                  "analyzer": "keyword"
               },
               "event": {
                  "type": "nested",
                  "properties": {
                     "title": {
                        "type": "string",
                        "analyzer": "keyword"
                     },
                     ...
                  }
               }
            }
         }
      }
   }
}

My Query:

GET /myindex/mydoc/7/_mlt?mlt_fields=event.title&min_doc_freq=1&min_term_freq=1&percent_terms_to_match=0
{
  "from": 0,
  "size": 5
}

My Results:

{
   ...
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

I suspect this is a nesting thing, so do I have to create a nested filtered query with "event = my_event" OR more-like-this = "author or topic" ? Or am I just missing something really stupid ?

1
Having a similar issue, how did you solve it?Panthro
Scrapped the idea of using "more-like-this" and just did a separate query which excluded the current document from the results.Dave R
that's what I was afraid of, damn... I had everything working with non nested, moved to nested for better performance and indexing...such a regret.Panthro
You can add the include_in_parent: true property to the event field to "flatten" the nested data in addition to having a separate document.likeitlikeit

1 Answers

1
votes

Yes, you have to do a nested query to query nested fields:

Because nested docs are always masked to the parent doc, the nested docs can never be accessed outside the scope of the nested query.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html