Skip to content

Instantly share code, notes, and snippets.

@LolWalid
Last active October 17, 2019 21:22
Show Gist options
  • Save LolWalid/c81b3e6251b7ae084b00c53a94f3061c to your computer and use it in GitHub Desktop.
Save LolWalid/c81b3e6251b7ae084b00c53a94f3061c to your computer and use it in GitHub Desktop.
ElasticSearch queries

where (condition1 && condition2) || (condition3 && condition4)

Example: where (place.type = 'Restaurant' and place.kind = 'Chinese') or (place.type = 'Bar' and place.kind in ('beer','wine'))

{
  query: {
    bool: {
      should: [
        {
          bool: {
            must: [
              {match: { 'place.type': 'Restaurant' }},
              {match: { 'place.kind': 'Chinese' }}
            ],
          }
        },
        {
          bool: {
            must: [
              {match: { 'place.type': 'Bar' }},
              {match: { 'place.kind': ['beer', 'wine'].join(' OR') }}
            ]
          }
        }
      ]
    }
  }
}

Must

must: [a, b] same as a && b

Must Not

must_not: [a, b] same as !a && !b

Should

should: [a, b] same as a || b

Should Not

should: must_not: [a, b] same as should: must: [!a, !b] same as !a && !b

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