【发布时间】:2015-04-03 04:43:27
【问题描述】:
我正在努力思考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(或更多),我将一无所获,尽管我希望这两个文档都会返回:
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": []
}
}
为什么?我错过了什么?
如果我想设置一个查询,只返回 "apple" 出现 5 次的文档,而不是出现 2 次的文档,有没有更好的方法?
为了方便,这里是代码:
http://sense.qbox.io/gist/341f9f77a6bd081debdcaa9e367f5a39be9359cc
【问题讨论】:
标签: elasticsearch morelikethis