Search code examples
elasticsearchsimilaritymorelikethis

ElasticSearch more_like_this with restricted result set


I want to run a more_like_this query, but only get the top results within a specific set of documents, so I would provide the IDs of these documents. Is there any way to do this? Docs indicate no.


Solution

  • One way would be to use a filtered query and use the id filter to specify the set of documents you want the more_like_this query to work on

    Example:

    {
       "query": {
          "filtered": {
             "query": {
                "more_like_this": {
                   "fields": [
                      "ticker.whitespace"
                   ],
                   "like_text": "WFC",
                   "min_term_freq": 1,
                   "max_query_terms": 12
                }
             },
             "filter": {
                "ids": {
                   "values": [
                      "7667"
                   ]
                }
             }
          }
       }
    }