Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search MUST + at least one SHOULD in percolator query

Im trying to make suggestions to users based on several factors:

•Suggestions MUST only be students from the same college •Suggestions MUST match at least one other field

I thought I had it but the problem is this query will return ALL students from the same school regardless of everything else:

PUT /user/.percolator/4
{
  "query": {
    "bool": {
    "must": [
        { "match": { "college":{
            "query" : "Oakland University",
            "type" : "phrase"
        }}}
    ],
      "should": [

            { "match": { "hashtag": "#chipotle" }},
            { "match": { "hashtag": "#running"}},
            { "match": { "college_course": "ART-160" }}

      ]
    }
  }
}

POST /user/stuff/_percolate/
{  
  "doc":{  

    "college":"Oakland University",
    "college_course": "WRT BIO MTH-400"


  }
}
like image 876
ChuckKelly Avatar asked Aug 26 '15 08:08

ChuckKelly


1 Answers

This is because the behavior of should and must in the same bool query. By default none of the "should" clauses are required to match, unless your bool contains only the "should" clause then it's required to match at least one.

To solve you problem, you just need to add "minimum_should_match" : 1 inside your bool query :)

like image 51
Ryan Huynh. Avatar answered Oct 22 '22 06:10

Ryan Huynh.