Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using minimum_should_match in filtered elasticSearch query

I have a filtered elasticsearch query that works, but I want to use minimum_should_match to instruct ES to return only results that have at least 3 should matches. But I can't seem to figure out where to put minimum_should_match. Where should I put it?

{
  "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
      }
    }
  }
}
like image 641
Marcel Emblazoned Avatar asked May 30 '15 21:05

Marcel Emblazoned


People also ask

Should minimum should match?

Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

Should and must not Elasticsearch?

Using must_not tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause. should – It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included. Scoring is used to rank the matches.

What is bool Elasticsearch query?

Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.


1 Answers

In order to use minimum_should_match, you need to rewrite your filtered query a little bit, i.e. you need to move your should clause to the query part of the filtered query and just keep must_not in the filter part (because missing is a filter). Then you can add minimum_should_match: 3 in the bool query part as shown below:

{
  "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
      }
    }
  }
}
like image 136
Val Avatar answered Sep 28 '22 13:09

Val