Sumatan
Sumatan

Reputation: 106

More Like This Query Not Getting Serialized - NEST

I am trying to create an Elasticsearch MLT query using NEST's object initializer syntax. However, the final query when serialized, is ONLY missing the MLT part of it. Every other query is present though.

When inspecting the query object, the MLT is present. It's just not getting serialized.

I wonder what I may be doing wrong.

I also noticed that when I add Fields it works. But I don't believe fields is a mandatory property here that when it is not set, then the MLT query is ignored.

The MLT query is initialized like this;

new MoreLikeThisQuery
        {
            Like = new[]
            {
                new Like(new MLTDocProvider
                {
                    Id = parameters.Id
                }), 
            }
        }

MLTDocProvider implements the ILikeDocument interface.

I expect the serialized query to contain the MLT part, but it is the only part that is missing.

Upvotes: 1

Views: 86

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

This looks like a bug in the conditionless behaviour of more like this query in NEST; I've opened an issue to address. In the meantime, you can get the desired behaviour by marking the MoreLikeThisQuery as verbatim, which will override NEST's conditionless behaviour

var client = new ElasticClient();

var parameters = new 
{
    Id = 1
};


var searchRequest = new SearchRequest<Document>
{
    Query = new MoreLikeThisQuery
    {
        Like = new[]
        {
            new Like(new MLTDocProvider
            {
                Id = parameters.Id
            }),
        },
        IsVerbatim = true
    }
};

var searchResponse = client.Search<Document>(searchRequest);

which serializes as

{
  "query": {
    "more_like_this": {
      "like": [
        {
          "_id": 1
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions