Elastic Search MUST + at least one SHOULD in percolator query
Asked Answered
H

1

6

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"


  }
}
Healy answered 26/8, 2015 at 8:43 Comment(0)
S
12

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 :)

Spermophile answered 10/9, 2015 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.