2017-09-18 29 views
2

假设我有4个查询:A,B,C,D。 我想要做的是对于查询A的给定结果集来计算每个其他查询的结果集的交集计数。计算给定结果集上查询的文档数

基本上我想要数A AND BA AND CA AND D但我不想每次重新计算A。 我使用ES V2.4

实例 - 比方说,我有以下映射

{ 
    "mappings": { 
    "doc": { 
     "properties": { 
     "type": { 
      "type": "string" 
     }, 
     "gender": { 
      "type": "string" 
     }, 
     "color": { 
      "type": "string" 
     }, 
     "material": { 
      "type": "string" 
     } 
     } 
    } 
    } 
} 

而且我A查询匹配是Shirt类型,这样的所有项目索引:

{ 
    "query": { 
    "match": { 
     "type": "Shirt" 
    } 
    } 
} 

现在从结果集(“所有类型的衬衫项目”)我想要得到的数字也是“蓝色”,或者是“男性” 我可以通过创建分开查询:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "type": "Shirt" 
      } 
     }, 
     { 
      "match": { 
      "color": "Blue" 
      } 
     } 
     ] 
    } 
    } 
} 

而且

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "type": "Shirt" 
      } 
     }, 
     { 
      "match": { 
      "gender": "Male" 
      } 
     } 
     ] 
    } 
    } 
} 

但是这将导致搜索type: "Shirt"每个我真的想避免的查询。

更新:我已经找到了我一直在寻找 - “过滤器”聚集 - 这样我就可以建立我的查询如下:

{ 
    "query": { 
    "match": { 
     "type": "Shirt" 
    } 
    }, 
    "aggs": { 
    "gender_male": { 
     "filter": { 
     "match": { 
      "gender": "Male" 
     } 
     } 
    }, 
    "color_blue": { 
     "filter": { 
     "match": { 
      "color": "Blue" 
     } 
     } 
    } 
    } 
} 
+0

能否请您用一个例子更好地解释一下。 –

+0

@HatimStovewala更新,以示例 –

+0

您只需要计数? –

回答

1

试试这个。将4个查询定义为aggs并立即获取。

GET test/shirts/_search 
{ 
    "query": { 
    "bool": { 
     "filter": { 
     "type": { 
      "value": "shirts" 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "count_by_color": { 
     "terms": { 
     "field": "color", 
     "size": 100 
     } 
    }, 
    "count_by_gender":{ 
     "terms": { 
     "field": "gender", 
     "size": 100 
     } 
    }, 
    "count_by_material":{ 
     "terms": { 
     "field": "material", 
     "size": 100 
     } 
    }, 
    "count_by_gender_color":{ 
     "terms": { 
     "field": "gender", 
     "size": 100 
     }, 
     "aggs": { 
     "color": { 
      "terms": { 
      "field": "color", 
      "size": 100 
      } 
     } 
     } 
    } 
    } 
} 
+0

谢谢,但这不是我所寻找的,因为这会返回很多值,我需要通过搜索来找到相关的值。我已经用一个答案更新了原始问题。 –

相关问题