2016-04-12 25 views
0

我試圖編寫一個查詢,有利於(更高分數)儘可能匹配更多的嵌套項目的文件,也有最低限度匹配。嵌套查詢與應該子條件和「min_should_match」

這是我到目前爲止的查詢,但它返回0匹配。

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "jobPost.role", 
      "score_mode": "sum", 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "term": { 
         "role.id": { 
         "value": "12", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "18", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "24", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "28", 
         "boost": 1 
         } 
        } 
        } 
       ], 
       "minimum_number_should_match": "-40%" 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

如前所述,這是不行的,但是,如果我刪除了min_should_match要求,它返回匹配。

問題我在尋找的是我需要確保一定的準確度,並且我不能在min_score條款上繼電器。

任何簡單的解決方案?我雖然已經把min_should_match帶到了第一個must,並且它與nested的條件相同。只是不知道這是我要做什麼或者如果是另一種解決方案。

回答

0

作爲一個初步的答案,等待有人來一個更好的(如果有的話)的方法:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "jobPost.role", 
      "score_mode": "sum", 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "term": { 
         "role.id": { 
         "value": "12", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "18", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "24", 
         "boost": 1 
         } 
        } 
        }, 
        { 
        "term": { 
         "role.id": { 
         "value": "28", 
         "boost": 1 
         } 
        } 
        } 
       ] 
       } 
      } 
      } 
     }, 
     { 
      "bool": { 
      "should": [ 
       { 
       "term": { 
        "role.id": { 
        "value": "12", 
        "boost": 1 
        } 
       } 
       }, 
       { 
       "term": { 
        "role.id": { 
        "value": "18", 
        "boost": 1 
        } 
       } 
       }, 
       { 
       "term": { 
        "role.id": { 
        "value": "24", 
        "boost": 1 
        } 
       } 
       }, 
       { 
       "term": { 
        "role.id": { 
        "value": "28", 
        "boost": 1 
        } 
       } 
       } 
      ], 
      "minimum_number_should_match": "-40%" 
      } 
     } 
     ] 
    } 
    } 
} 

該做的工作對我來說,但感覺像複製的代碼是不是最好的解決辦法。