Elasticsearch更像是这样的结果

时间:2015-01-18 22:18:05

标签: java elasticsearch morelikethis

我在做一个有产品的网页。所有产品都在弹性搜索索引中,我正在使用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),但事实并非如此。这是对的,还是我做错了什么?

由于

1 个答案:

答案 0 :(得分:3)

由于您只有一小部分文档,因此需要将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();