我正在努力思考这个查询是如何工作的,但我似乎遗漏了一些东西。我阅读了文档,但 ES 文档通常有点……缺乏。
目标是能够通过术语频率来限制结果,如此处所尝试的。
所以我建立了一个简单的索引,包括用于调试的术语向量,然后添加了两个简单的文档。
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"
}
当我查看 termvectors 时,我看到了我的期望:
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