Search code examples
elasticsearchboosting

Boosting query is not working properly


{
  "sort": [
    {
      "is_active": "asc"
    }
  ],
  "fields": [
    "is_job_seeking", "is_active"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": {
              "term": {
                "is_job_seeking": 1
              }
            }
          }
        }
      ]
    }
  }
}

this query return me all document which has is_job_seeking=1, and is_active=0 and is_active=1 and that's fine, now when I want to boost score for document which has is_active=1 I have add boosting like

{
  "sort": [
    {
      "is_active": "asc"
    }
  ],
  "fields": [
    "is_job_seeking", "is_active"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": {
              "term": {
                "is_job_seeking": 1
              }
            }
          }
        },
        {
          "boosting": {
            "positive": {
              "term": {
                "is_active": 1
              }
            },
            "negative": {
              "term": {
                "is_active": 0
              }
            },
            "negative_boost": 0.3
          }
        }
      ]
    }
  }
}

but this give me results only with is_active=1


Solution

  • Try this:

    {
      "sort": [
        {
          "is_active": "asc"
        }
      ],
      "fields": [
        "is_job_seeking",
        "is_active"
      ],
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": {
                  "term": {
                    "is_job_seeking": 1
                  }
                }
              }
            }
          ],
          "should": [
            {
              "boosting": {
                "positive": {
                  "term": {
                    "is_active": 1
                  }
                },
                "negative": {
                  "term": {
                    "is_active": 0
                  }
                },
                "negative_boost": 0.3
              }
            }
          ]
        }
      }
    }