2015-05-30 156 views
2

我有一个过滤的弹性搜索查询可以工作,但我想使用minimum_should_match来指示ES只返回至少有3个应该匹配的结果。但我似乎无法弄清楚在哪里放置minimum_should_match。我应该把它放在哪里?在过滤的elasticSearch查询中使用minimum_should_match

{ 
    "size": 100, 
    "sort": { 
    "price_monthly": "asc" 
    }, 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": [] 
     }, 
     "filter": { 
     "bool": { 
      "must": [], 
      "should": [ 
      [ 
       { 
       "range": { 
        "mb.untouched": { 
        "gte": "0", 
        "lt": "500" 
        } 
       } 
       }, 
       { 
       "range": { 
        "mb.untouched": { 
        "gte": "500", 
        "lt": "1000" 
        } 
       } 
       } 
      ], 
      [ 
       { 
       "range": { 
        "minutes.untouched": { 
        "gte": "0", 
        "lt": "100" 
        } 
       } 
       }, 
       { 
       "range": { 
        "minutes.untouched": { 
        "gte": "200", 
        "lt": "300" 
        } 
       } 
       } 
      ], 
      [ 
       { 
       "range": { 
        "sms.untouched": { 
        "gte": "750", 
        "lt": "1000" 
        } 
       } 
       } 
      ] 
      ], 
      "must_not": { 
      "missing": { 
       "field": "provider.untouched" 
      } 
      } 
     } 
     }, 
     "strategy": "query_first" 
    } 
    }, 
    "aggs": { 
    "provider.untouched": { 
     "terms": { 
     "field": "provider.untouched" 
     } 
    }, 
    "prolong.untouched": { 
     "terms": { 
     "field": "prolong.untouched" 
     } 
    }, 
    "duration.untouched": { 
     "terms": { 
     "field": "duration.untouched" 
     } 
    }, 
    "mb.untouched": { 
     "histogram": { 
     "field": "mb.untouched", 
     "interval": 500, 
     "min_doc_count": 1 
     } 
    }, 
    "sms.untouched": { 
     "histogram": { 
     "field": "sms.untouched", 
     "interval": 250, 
     "min_doc_count": 1 
     } 
    }, 
    "minutes.untouched": { 
     "histogram": { 
     "field": "minutes.untouched", 
     "interval": 100, 
     "min_doc_count": 1 
     } 
    }, 
    "price_monthly.untouched": { 
     "histogram": { 
     "field": "price_monthly.untouched", 
     "interval": 5, 
     "min_doc_count": 1 
     } 
    } 
    } 
} 
+0

您可以在minimum_should_match条款未添加到过滤器:然后你就可以如下图所示在bool查询部分添加minimum_should_match: 3

回答

3

为了使用minimum_should_match,你需要重写你的过滤查询一点点,即你需要在你should条款移到过滤查询的query部分,只保留must_notfilter部分(因为missing是一个过滤器)。

{ 
    "size": 100, 
    "sort": { 
    "price_monthly": "asc" 
    }, 
    "query": { 
    "filtered": { 
     "query": { 
     "bool": { 
      "minimum_should_match": 3, 
      "must": [], 
      "should": [ 
      [ 
       { 
       "range": { 
        "mb.untouched": { 
        "gte": "0", 
        "lt": "500" 
        } 
       } 
       }, 
       { 
       "range": { 
        "mb.untouched": { 
        "gte": "500", 
        "lt": "1000" 
        } 
       } 
       } 
      ], 
      [ 
       { 
       "range": { 
        "minutes.untouched": { 
        "gte": "0", 
        "lt": "100" 
        } 
       } 
       }, 
       { 
       "range": { 
        "minutes.untouched": { 
        "gte": "200", 
        "lt": "300" 
        } 
       } 
       } 
      ], 
      [ 
       { 
       "range": { 
        "sms.untouched": { 
        "gte": "750", 
        "lt": "1000" 
        } 
       } 
       } 
      ] 
      ] 
     } 
     }, 
     "filter": { 
     "bool": { 
      "must_not": { 
      "missing": { 
       "field": "provider.untouched" 
      } 
      } 
     } 
     }, 
     "strategy": "query_first" 
    } 
    }, 
    "aggs": { 
    "provider.untouched": { 
     "terms": { 
     "field": "provider.untouched" 
     } 
    }, 
    "prolong.untouched": { 
     "terms": { 
     "field": "prolong.untouched" 
     } 
    }, 
    "duration.untouched": { 
     "terms": { 
     "field": "duration.untouched" 
     } 
    }, 
    "mb.untouched": { 
     "histogram": { 
     "field": "mb.untouched", 
     "interval": 500, 
     "min_doc_count": 1 
     } 
    }, 
    "sms.untouched": { 
     "histogram": { 
     "field": "sms.untouched", 
     "interval": 250, 
     "min_doc_count": 1 
     } 
    }, 
    "minutes.untouched": { 
     "histogram": { 
     "field": "minutes.untouched", 
     "interval": 100, 
     "min_doc_count": 1 
     } 
    }, 
    "price_monthly.untouched": { 
     "histogram": { 
     "field": "price_monthly.untouched", 
     "interval": 5, 
     "min_doc_count": 1 
     } 
    } 
    } 
} 
相关问题