dr.calix
dr.calix

Reputation: 727

Elasticsearch: Rank by most number of should matches

I have an indexed job description field. I am trying to rank or order the results by number of matches.

Example, I am searching for:

Records with most number of matches will be ranked highest. I tried the suggested here https://stackoverflow.com/a/45319822/2445717 but did not work as what I expected.

Below is my current query:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "job_description": {
                    "query": "friendly",
                    "operator": "and"
                    
                  }
                }
              },
              {
                "match": {
                  "job_description": {
                    "query": "honest personality",
                    "operator": "and"
                    
                  }
                }
              },
              {
                "match": {
                  "job_description": {
                    "query": "excellent communication skills",
                    "operator": "and"
                    
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 0

Views: 227

Answers (1)

dr.calix
dr.calix

Reputation: 727

match_query & minimum_should_match did the trick.

posting example code in case someone will need it.

{
  "query": {
    "bool": {
      "should": [
        {
          "match_query": {
            "job_description": {
              "query": "friendly",
              "boost": 1
            }
          }
        },
        {
          "match_query": {
            "job_description": {
              "query": "honest personality",
              "boost": 1
            }
          }
        },
        {
          "match_query": {
            "job_description": {
              "query": "excellent communication skills",
              "boost": 1
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

Upvotes: 0

Related Questions