根据内容对facet结果中的文档进行惩罚

时间:2013-11-28 10:51:51

标签: search elasticsearch faceted-search facet

如果我们在elasticsearch中有以下文件:

[
    {'name': 'John', 'time': '2013-01-01 12:01:00'},
    {'name': 'John', 'time': '2013-01-01 12:02:00'},
    {'name': 'John', 'time': '2013-01-01 12:03:00'},
    {'name': 'John', 'time': '2013-01-01 12:04:00'},
    {'name': 'Harry', 'time': '2013-01-01 12:05:00'},
    {'name': 'Fred', 'time': '2013-01-01 12:06:00'},
    {'name': 'Fred', 'time': '2013-01-01 12:07:00'}
]

我们面对'名称'字段,我们会得到这样的结果:

 "facets": {
   "count_per_name": {
      "_type": "terms",
      "missing": 0,
      "total": 7,
      "other": 0,
      "terms": [
         {
            "term": "John",
            "count": 4
         },
         {
            "term": "Fred",
            "count": 2
         },
         {
            "term": "Harry",
            "count": 1
         }
      ]
   }
}

我的问题是:是否可以在elasticsearch中执行分面查询,其中name“John”的文档计为“half”文档?这将导致约翰的数量从4下降到2,但弗雷德和哈利保持不变:

 "facets": {
   "count_per_name": {
      "_type": "terms",
      "missing": 0,
      "total": 5,
      "other": 0,
      "terms": [
         {
            "term": "John",
            "count": 2
         },
         {
            "term": "Fred",
            "count": 2
         },
         {
            "term": "Harry",
            "count": 1
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:0)

你可以玩http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html

并指定value_script,其中John为John返回0.5,其他人返回1,并使用SUM facet结果进行操作。虽然这种方法受性能影响