我正在做一个包含产品的网页。所有产品都在Elasticsearch索引中,我正在使用Java进行检索。

现在,我想在那儿放置类似日期条件的类似产品,因此我开始搜索并找到“类似这样的产品”。所以我做的是:

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();

这返回了一些值(但我不知道它是否正确)。因此,为了测试,我为两个产品建立了索引:
{"_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"}}

并且
{"_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"}}

因此,打开ID = 2的产品时,我期望得到与其他产品(ID为1000)类似的产品,但事实并非如此。这是对的,还是我做错了什么?

谢谢

最佳答案

由于文档的数量很少,因此需要将min_doc_freq设置为0。
像这样的单词更需要每个术语来考虑,并查看出现在多少文档中。这称为反文档频率。现在,如果该术语小于5(默认情况下),则不考虑该词。这意味着,如果您的索引中包含少量文档,则默认情况下,大多数MLT将无法正常工作。因此,将最小文档频率更改为0或1以使代码正常工作。
因此,以下Java代码应该可以工作-

SearchResponse response = esClient.prepareMoreLikeThis("auction", "product", productId)
            .setSearchSize(size)
            .setField("name")
            .setMinDocFreq(0)
            .setMinTermFreq(1)
            .setMinWordLen(2)
            .setSearchSource(query)
            .execute().actionGet();

10-07 17:12