在过滤的elasticSearch查询中使用minimum_should_match

时间:2015-05-30 21:25:39

标签: elasticsearch

我有一个有效的过滤弹性搜索查询,但我想使用minimum_should_match来指示ES只返回至少有3个匹配的结果。但我似乎无法弄清楚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
      }
    }
  }
}

1 个答案:

答案 0 :(得分:5)

为了使用minimum_should_match,您需要稍微重写过滤后的查询,即您需要将should子句移动到已过滤查询的query部分,将must_not保留在filter部分(因为missing是一个过滤器)。然后,您可以在minimum_should_match: 3查询部分添加bool,如下所示:

{
  "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
      }
    }
  }
}