OpenSearch Java client - Find other documents like this one?

236 Views Asked by At

I have a number of documents indexed in OpenSearch. I've been told it is possible to have a "related documents" search, where I can find documents similar to one with an ID of X, but haven't been able to locate any examples or documentation for it.

Is it possible to take a document ID and then find N number of documents that are similar to it? Any examples or documentation you can point me to using the Java client would be very helpful!

1

There are 1 best solutions below

0
On

Yes, you can do it with a more_like_this query and as it's not really explained in the OpenSearch documentation, you can refer to the Elasticsearch one on the more_like_this query.

It goes as shown below where you can search my-index for documents that are similar to another specific document with ID 1 from my-other-index:

GET my-index/_search
{
  "query": {
    "more_like_this": {
      "fields": [ "title", "description" ],
      "like": [
        {
          "_index": "my-other-index",
          "_id": "1"
        }
      ],
      "min_term_freq": 1,
      "max_query_terms": 12
    }
  }
}

like is an array where you can specify as many documents as you want and also free text.

Regarding doing this with the OpenSearch Java client, it should go something like this:

MoreLikeThisQuery moreLikeThisQuery = MoreLikeThisQuery.of(mlt ->
    mlt.fields("title", "description")
        .minTermFreq(1)
        .maxQueryTerms(12)           
        .like(List.of(Like.of(l -> 
               l.document(LikeDocument.of(ld -> 
                     ld.id("1").index("my-other-index")
               ))
        )))
);