javaelasticsearchmorelikethis

Elasticsearch Java API MoreLikeThis not returning documents compared to "_search" rest endpoint


Intention: Elasticsearch Java MoreLikeThis query in Java to do exactly what the below raw more_like_this filtered query via the /_search rest endpoint is doing.

GET /index/type/_search
{
  "query": {
    "filtered": {
      "query": {
        "more_like_this": {
          "fields": [
            "title",
            "body",
            "description",
            "organisations",
            "locations"
          ],
          "min_term_freq": 2,
          "max_query_terms": 25,
          "ids": [
            "http://xxx/doc/doc"
          ]
        }
      },
      "filter": {
        "range": {
          "datePublished": {
            "gte": "2016-01-01T12:30:00+01:00"
          }
        }
      }
    }
  },
  "fields": [
    "title",
    "description",
    "datePublished"
  ]
}

And this is my Java implementation for the above:

FilteredQueryBuilder queryBuilder = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(),FilterBuilders.rangeFilter("datePublished").gte(("2016-01-01T12:30:00+01:00")));
SearchSourceBuilder query = SearchSourceBuilder.searchSource().query(queryBuilder);
return client.prepareMoreLikeThis("index", "type", "http://xxx/doc/doc")
    .setField("title", "description", "body", "organisations","locations")
    .setMinTermFreq(2)
    .maxQueryTerms(25)
    .setSearchSource(query);

However, the results far differ from the more_like_this rest endpoint was returning. I am getting matches of about 4/5th of my whole documents in the index. As if none of the filters are being applied

Targeting ES v1.4.2 and v1.6.2

Any advice please.Thanks


Solution

  • I got the desire results with QueryBuilders.moreLikeThisQuery(). Inspirations from this post here.

     FilterBuilder filterBuilder = FilterBuilders.rangeFilter("datePublished")
            .gte("2016-01-01T12:30:00+01:00")
            .includeLower(false).includeUpper(false);
    
        MoreLikeThisQueryBuilder mltQueryBuilder = QueryBuilders.moreLikeThisQuery("title", "description", "body", "organisations","locations")
            .minTermFreq(2)
            .maxQueryTerms(25)
            .ids("http://xxx/doc/doc");
    
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("index");
    
        searchRequestBuilder.setTypes("type");
        searchRequestBuilder.addFields("title","description","datePublished");
        searchRequestBuilder.setQuery(mltQueryBuilder).setPostFilter(filterBuilder);
    
        searchRequestBuilder.execute().actionGet()
    

    Notes: