I'm doing a webpage that has products. All of products are in an elasticsearch index and I'm retriving using Java.
Now I want to put similar products there that respect a date condition, so I started to search and found the "More like this". So what I did was:
FilteredQueryBuilder queryBuilder = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), FilterBuilders.rangeFilter("finish_date").gt("now"));
SearchSourceBuilder query = SearchSourceBuilder.searchSource().query(queryBuilder);
SearchResponse response = esClient.prepareMoreLikeThis("auction", "product", productId).setSearchSize(size).setField("name").setMinTermFreq(1).setMinWordLen(2).setSearchSource(query).execute().actionGet();
This returned some values (but I don't know if it was right). So to test I indexed two products:
{"_index":"auction","_type":"product","_id":"2","_version":3,"found":true,"_source":
{"name" : "Compro Portátil Asus x552cl-sx150h", "product_suggestions" : {"input":["compro portátil Asus x552clsx033h","compro","portátil","asus", "x552cl-sx033h","asus"]}, "description" : "Compro portátil usado mas com garantia.", "brand" : "Asus","brand_facet" : "Asus", "state_id" : "2", "user_state_description" : "Used", "product_type_id" : "1", "photos" : [""], "current_price" : 450, "finish_date" : "2014/09/20 17:20"}}
and also
{"_index":"auction","_type":"product","_id":"1000","_version":3,"found":true,"_source":
{"name" : "Compro Portátil Asus x552cl-sx150h", "product_suggestions" : {"input":["compro portátil Asus x552clsx033h","compro","portátil","asus", "x552cl-sx033h","asus"]}, "description" : "Compro portátil usado mas com garantia.", "brand" : "Asus","brand_facet" : "Asus", "state_id" : "2", "user_state_description" : "Used", "product_type_id" : "1", "photos" : [""], "current_price" : 450, "finish_date" : "2015/09/20 17:20"}}
So opening the product with id=2 I was expecting to get as similiar product the other (with id 1000), but that wasn't the case. Is this right, or am I doing something wrong?
Thanks
As you have only a small about of documents , you need to set min_doc_freq to 0. More like this takes each term to consider and see in how many documents this word have appeared. This is called the inverse document frequency. Now if this term is less than 5 ( By default ), that word is not considered. This means that if you have an index with little amount of documents , mostly your MLT wont work by default. Hence change the min doc frequency to 0 or 1 to make your code work. So , the following java code should work -
SearchResponse response = esClient.prepareMoreLikeThis("auction", "product", productId)
.setSearchSize(size)
.setField("name")
.setMinDocFreq(0)
.setMinTermFreq(1)
.setMinWordLen(2)
.setSearchSource(query)
.execute().actionGet();