2014-11-06 51 views
4

对于一些我的查询ElasticSearch我想要的查询结果而言三条信息反馈:检索文档频率与聚合

  • 哪些项中T的结果文档集发生?
  • T的每个元素在结果文档集中出现多久?
  • T的每个元素在整个索引( - >文档频率)中出现的频率如何?

第一点很容易使用默认术语facet或现在的术语聚合方法来确定。所以我的问题实际上是关于第三点。 在ElasticSearch 1.x之前,即在切换到“聚合”范例之前,我可以使用“全局”选项设置为trueQueryFilter的术语方面来获取确切的文档频率(“全局计数”)在由QueryFilter指定的文档集中出现的术语。 起初我以为我可以用global aggregation做同样的事情,但看起来我不行。原因是 - 如果我理解正确 - 原始的facet机制是以术语为中心的,而聚合桶是由属于每个桶的一组文档定义的。 也就是说指定一个term facetglobal选项,QueryFilter首先确定由过滤器命中的术语,然后确定计算的方面值。由于方面是global我会收到文件计数。

对于聚合,它是不同的。 global聚合只能用作顶层聚合,导致聚合忽略当前查询结果并计算聚合 - 例如,一个terms aggregation - 索引中的所有文档。所以对我来说,这太多了,因为我想限制返回的术语('桶')到文档结果集中的术语。但是,如果我使用具有词汇子聚合的过滤器子聚合,我会再次将术语桶限制到过滤器,因此不检索文档频率,而是检测正常的面数。原因在于水桶是在过滤器之后确定的,因此它们“太小”。但我不想限制桶大小,我想限制桶查询结果集中的条款。

如何在使用聚合的查询结果集中获取这些术语的文档频率(因为方面已被弃用并将被删除)?

谢谢你的时间!

编辑:这里是我如何试图达到所需行为的一个例子。 我会定义两个聚合:

  • global_agg_with_filter_and_terms
  • global_agg_with_terms_and_filter

两者有一个global聚集在其顶部,因为它是唯一有效的位置。然后,在第一次聚合中,我首先将结果过滤为原始查询,然后应用术语子聚合。 在第二次聚合中,我做的大部分是相同的,只不过在这里过滤器聚合是术语聚合的子聚合。因此,类似的名字,只有聚合顺序不同。

{ 
    "query": { 
     "query_string": { 
      "query": "text: my query string" 
     } 
    }, 
    "aggs": { 
     "global_agg_with_filter_and_terms": { 
      "global": {}, 
      "aggs": { 
       "filter_agg": { 
        "filter": { 
         "query": { 
          "query_string": { 
           "query": "text: my query string" 
          } 
         } 
        }, 
        "aggs": { 
         "terms_agg": { 
          "terms": { 
           "field": "facets" 
          } 
         } 
        } 
       } 
      } 
     }, 
     "global_agg_with_terms_and_filter": { 
      "global": {}, 
      "aggs": { 
       "document_frequency": { 
        "terms": { 
         "field": "facets" 
        }, 
        "aggs": { 
         "term_count": { 
          "filter": { 
           "query": { 
            "query_string": { 
             "query": "text: my query string" 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

响应:

{ 
    "took": 18, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 221, 
     "max_score": 0.9839197, 
     "hits": <omitted> 
    }, 
    "aggregations": { 
     "global_agg_with_filter_and_terms": { 
      "doc_count": 1978, 
      "filter_agg": { 
       "doc_count": 221, 
       "terms_agg": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
         { 
          "key": "fid8", 
          "doc_count": 155 
         }, 
         { 
          "key": "fid6", 
          "doc_count": 40 
         }, 
         { 
          "key": "fid9", 
          "doc_count": 10 
         }, 
         { 
          "key": "fid5", 
          "doc_count": 9 
         }, 
         { 
          "key": "fid13", 
          "doc_count": 5 
         }, 
         { 
          "key": "fid7", 
          "doc_count": 2 
         } 
        ] 
       } 
      } 
     }, 
     "global_agg_with_terms_and_filter": { 
      "doc_count": 1978, 
      "document_frequency": { 
       "doc_count_error_upper_bound": 0, 
       "sum_other_doc_count": 0, 
       "buckets": [ 
        { 
         "key": "fid8", 
         "doc_count": 1050, 
         "term_count": { 
          "doc_count": 155 
         } 
        }, 
        { 
         "key": "fid6", 
         "doc_count": 668, 
         "term_count": { 
          "doc_count": 40 
         } 
        }, 
        { 
         "key": "fid9", 
         "doc_count": 67, 
         "term_count": { 
          "doc_count": 10 
         } 
        }, 
        { 
         "key": "fid5", 
         "doc_count": 65, 
         "term_count": { 
          "doc_count": 9 
         } 
        }, 
        { 
         "key": "fid7", 
         "doc_count": 63, 
         "term_count": { 
          "doc_count": 2 
         } 
        }, 
        { 
         "key": "fid13", 
         "doc_count": 55, 
         "term_count": { 
          "doc_count": 5 
         } 
        }, 
        { 
         "key": "fid10", 
         "doc_count": 11, 
         "term_count": { 
          "doc_count": 0 
         } 
        }, 
        { 
         "key": "fid11", 
         "doc_count": 9, 
         "term_count": { 
          "doc_count": 0 
         } 
        }, 
        { 
         "key": "fid12", 
         "doc_count": 5, 
         "term_count": { 
          "doc_count": 0 
         } 
        } 
       ] 
      } 
     } 
    } 
} 

首先,请看看前两个返回两个集合的长期桶,用钥匙fid8fid6。我们可以很容易地看到,这些术语分别出现在结果集155和40次。现在请看第二个汇总,global_agg_with_terms_and_filter。术语聚合是在全局聚合的范围内,所以我们可以在这里分别看到文档频率1050和668。所以这部分看起来不错。当您进一步扫描术语桶的列表时,会出现问题,并使用密钥fid10fid12对桶进行扫描。虽然我们收到他们的文档频率,但我们也可以看到他们的term_count为0.这是由于这些条件不在我们的查询中发生,我们也用于过滤器子聚合。所以问题在于对于所有术语(全局范围!),它们的文档频率和它们关于实际查询结果的方面数被返回。但是我需要对查询结果中出现的条款做出准确的描述,即对于第一个聚合global_agg_with_filter_and_terms返回的确切条款。

也许有可能定义某种类型的过滤器,删除其子过滤器汇总term_count为零的所有存储桶doc_count

+0

你能有一个例证查询和结果集,你已经尝试了什么,到目前为止,什么行不通,什么是所需的行为?我知道你已经描述过这种行为,但我希望看到一个例子。 – 2014-11-07 07:33:20

+0

感谢您的评论。你是对的,例子总是让事情变得更容易理解。我希望编辑我的帖子使情况更容易理解。 – khituras 2014-11-07 09:05:08

回答

1

如果答案迟了,你好,非常抱歉。

你应该看看Significant Terms聚集的,就像术语聚集,它返回一个桶与可通过doc_count出现次数的数量设定结果每学期的现象发生,但你也得出现在数通过bg_count设置的背景。这意味着它只会为出现在查询结果集的文档中的术语创建分区。

默认背景集合包含查询范围中的所有文档,但可以使用background_filter过滤到您想要的任何子集。

您可以使用脚本桶计分功能通过结合多种指标来排名的桶,你想要的方式:

  • _subset_freq:文件数项出现在结果集,
  • _superset_freq:数该术语出现在背景集中,
  • _subset_size:结果集中文档的数量,
  • _superset_size:背景集中文档的数量。

请求:

{ 
    "query": { 
    "query_string": { 
     "query": "text: my query string" 
    } 
    }, 
    "aggs": { 
    "terms": { 
     "significant_terms": { 
     "script": "_subset_freq", 
     "size": 100 
     } 
    } 
    } 
} 
+0

非常感谢你回来在这个:-)使用背景设置好主意!它作为一种范围限制,目前不可能。非常感谢你的指针! – khituras 2015-06-18 07:45:14