2
votes

I'd like to use moreLikeThis query on Elasticsearch using NEST library and give different boost values for each match.

var moreLikeThis = _elastic.Search<Report>(s => s
    .From(0)
    .Size(10)
    .Query(q => q
        .Filtered(f => f
            .Query(fq => fq
                .Dismax(dmx => dmx
                    .TieBreaker(0.7)
                    .Queries(qr => qr
                        .MoreLikeThis(mlt => mlt
                            .OnFields(of => of.Title.Suffix("stemmed"))
                            .MinTermFrequency(1)
                            .MaxQueryTerms(12)
                            .Boost(20)
                            .Documents(docs => docs
                                .Document()          // This is the part where I'm stuck
                            )
                        ), qr => qr
                        .MoreLikeThis(mlt => mlt
                            .OnFields(of => of.Title.Suffix("synonym"))
                            .MinTermFrequency(1)
                            .MaxQueryTerms(12)
                            .Boost(10)
                        )
                    )
                )
            )
        )
    )
);

This the query I'm stuck at. I can write this easily in raw JSON format, but that's not what I'm aiming for. I'm quite new in both C# and NEST and do not know how to pass documentId there.

That's my class, if it helps at all :

[ElasticType(IdProperty = "_id", Name = "reports")]
public class Report
{
    [ElasticProperty(Name = "_id", Type = FieldType.String)]
    public string _id { get; set; }

    [ElasticProperty(Name = "Title", Type = FieldType.String)]
    public string Title { get; set; }
}

And that's the query I'm using as JSON and it works just fine.

{
  "from": 0,
  "size": 10,
  "fields": ["Title"],
  "query": {
    "filtered": {
      "query": {
        "dis_max": {
          "tie_breaker": 0.7,
          "queries": [
            {
              "more_like_this": {
                "fields": ["Title.stemmed"],
                "docs": [
                  {
                    "_index": "test",
                    "_type": "reports",
                    "_id": "68753"
                  }
                ],
                "min_term_freq": 1,
                "max_query_terms": 12
              }
            }, {
              "more_like_this": {
                "fields": ["Title.synonym"],
                "docs": [
                  {
                    "_index": "test",
                    "_type": "reports",
                    "_id": "68753"
                  }
                ],
                "min_term_freq": 1,
                "max_query_terms": 12
              }
            }
          ]
        }
      }
    }
  }
}

Documentation in NEST doesn't explain or give a basic idea how this is done.

Thank you.

1
Looks like a bug. PR for this issue is here github.com/elastic/elasticsearch-net/pull/1431 - Rob
@Rob Well thank you sir, hopefully dev will fix it. :) - Evaldas Buinauskas
@Rob Perhaps you could submit your comment as an answer so I could upvote it and mark it as accepted? - Evaldas Buinauskas
It's okay. Thanks :) - Rob

1 Answers

1
votes

As stated in comments this was a bug and will be fixed together with 1.5.2 NEST release.