Elasticsearch 更喜欢这个查询

Elasticsearch More Like This Query

我正在努力思考 more like this 查询的工作原理,但我似乎遗漏了什么。我看了文档,但是ES的文档经常有点……欠缺。

目标是能够按词频限制结果,正如所尝试的那样 here

所以我设置了一个简单的索引,包括用于调试的术语向量,然后添加了两个简单的文档。

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0
   },
   "mappings": {
      "doc": {
         "properties": {
            "text": {
               "type": "string",
               "term_vector": "yes"
            }
         }
      }
   }
}

PUT /test_index/doc/1
{
    "text": "apple, apple, apple, apple, apple"
}

PUT /test_index/doc/2
{
    "text": "apple, apple"
}

当我查看术语向量时,我看到了我所期望的:

GET /test_index/doc/1/_termvector
...
{
   "_index": "test_index",
   "_type": "doc",
   "_id": "1",
   "_version": 1,
   "found": true,
   "term_vectors": {
      "text": {
         "field_statistics": {
            "sum_doc_freq": 2,
            "doc_count": 2,
            "sum_ttf": 7
         },
         "terms": {
            "apple": {
               "term_freq": 5
            }
         }
      }
   }
}

GET /test_index/doc/2/_termvector
{
   "_index": "test_index",
   "_type": "doc",
   "_id": "2",
   "_version": 1,
   "found": true,
   "term_vectors": {
      "text": {
         "field_statistics": {
            "sum_doc_freq": 2,
            "doc_count": 2,
            "sum_ttf": 7
         },
         "terms": {
            "apple": {
               "term_freq": 2
            }
         }
      }
   }
}

当我 运行 使用 "min_term_freq": 1 进行以下查询时,我得到了两个文档:

POST /test_index/_search
{
   "query": {
      "more_like_this": {
         "fields": [
            "text"
         ],
         "like_text": "apple",
         "min_term_freq": 1,
         "percent_terms_to_match": 1,
         "min_doc_freq": 1
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.5816214,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.5816214,
            "_source": {
               "text": "apple, apple, apple, apple, apple"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 0.5254995,
            "_source": {
               "text": "apple, apple"
            }
         }
      ]
   }
}

但是,如果我将 "min_term_freq" 增加到 2(或更多),我什么也得不到,尽管我希望两个文档都被 returned:

POST /test_index/_search
{
   "query": {
      "more_like_this": {
         "fields": [
            "text"
         ],
         "like_text": "apple",
         "min_term_freq": 2,
         "percent_terms_to_match": 1,
         "min_doc_freq": 1
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

为什么?我错过了什么?

如果我想设置一个查询,只 return "apple" 出现 5 次的文档,而不是出现 2 次的文档,有没有更好的方法?

这里是代码,为了方便起见:

http://sense.qbox.io/gist/341f9f77a6bd081debdcaa9e367f5a39be9359cc

最小术语频率和最小文档频率实际上是在进行 MLT 之前应用于输入的。 这意味着由于您在输入文本中只出现了一次 apple,因此 apple 从未符合 MLT 的条件,因为最小词频设置为 2。 如果您将输入更改为 "apple apple",如下所示,一切正常 -

POST /test_index/_search
{
   "query": {
      "more_like_this": {
         "fields": [
            "text"
         ],
         "like_text": "apple apple",
         "min_term_freq": 2,
         "percent_terms_to_match": 1,
         "min_doc_freq": 1
      }
   }
}

最小文档频率也是如此。 Apple 在至少 2 个文档中找到,因此 min_doc_freq 最多 2 个将符合从输入文本申请 MLT 操作的条件。

作为这个问题的发布者,我也试图围绕 more_like_this 查询进行思考...

我费了点劲才在网上找到好的信息来源,但(在大多数情况下)文档似乎最有帮助,所以,这里是 the link to the documentation,以及一些更重要的术语 (and/or有点难懂,所以加了我的解释):

max_query_terms - 将选择的最大查询词数(来自每个输入文档)。增加此值可提供更高的准确性,但会降低查询执行速度。默认为 25。

min_term_freq - 最小词频,输入文档中将忽略低于该词频的词。默认为 2。

If the term appears in the input document less than 2 (default) times, it will be ignored from the input document, i.e. not be searched for in other possible more_like_this documents.

min_doc_freq - 最小文档频率,低于该频率的条目将从输入文档中忽略。默认为 5。

这个花了我一秒钟的时间,所以,这是我的解释:

In how many documents a term from the input document must appear in order to be selected as a query term.

就是这样,我希望我能挽救某人几分钟的生命。 :)

干杯!