Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Last active September 9, 2023 09:00
Show Gist options
  • Save hkulekci/53d15322dd83b18d14e126207c8b51ff to your computer and use it in GitHub Desktop.
Save hkulekci/53d15322dd83b18d14e126207c8b51ff to your computer and use it in GitHub Desktop.
terms_set Query
DELETE job-candidates

PUT job-candidates
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "programming_languages": {
        "type": "keyword"
      },
      "required_matches": {
        "type": "long"
      }
    }
  }
}


PUT job-candidates/_doc/1?refresh
{
  "name": "Jane Smith",
  "programming_languages": [ "c++", "java" ],
  "required_matches": 2
  
}


PUT job-candidates/_doc/2?refresh
{
  "name": "Jason Response",
  "programming_languages": [ "java", "php" ],
  "required_matches": 2
}

PUT /job-candidates/_doc/3?refresh
{
  "name": "Jason Request",
  "programming_languages": [ "rust", "php" ],
  "required_matches": 1
}

PUT /job-candidates/_doc/4?refresh
{
  "name": "Mason Difficult",
  "programming_languages": [ "javascript", "php", "html"],
  "required_matches": 3
}


GET job-candidates/_search

GET job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": ["c++", "java", "php"],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}


GET job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": ["java", "php"],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}



PUT /job-candidates/_doc/5?refresh
{
  "name": "Mason Difficult",
  "programming_languages": [ "java", "php"]
}

GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "java", "php" ],
        "minimum_should_match_script": {
          "source": 
"""
  if (doc['required_matches'].size() != 0) {
    params['min_requires'] = doc['required_matches'].value
  }
  return params['min_requires'];
""",
          "params": {
            "min_requires": 2
          }
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment