在已过滤的ElasticSearch查询中使用minimum_should_match

Mar*_*ned 4 elasticsearch

我有一个经过过滤的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
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Val*_*Val 8

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

{
  "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
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)